Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion R/cheetah.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#' @param search Whether to enable a search field on top of the table.
#' Default to `disabled`. Use `exact` for exact matching
#' or `contains` to get larger matches.
#' @param sortable Logical. Whether to enable sorting on all columns. Defaults to TRUE.
#'
#' @return An HTML widget object of class 'cheetah' that can be:
#' \itemize{
Expand All @@ -33,7 +34,8 @@ cheetah <- function(
height = NULL,
elementId = NULL,
rownames = TRUE,
search = c("disabled", "exact", "contains")
search = c("disabled", "exact", "contains"),
sortable = TRUE
) {
search <- match.arg(search)
# Only show rownames if they are character strings (meaningful) and rownames is TRUE
Expand All @@ -49,6 +51,7 @@ cheetah <- function(

columns <-
update_col_list_with_classes(data, columns) %>%
make_table_sortable(sortable = sortable) %>%
add_field_to_list()

data_json <- toJSON(data, dataframe = "rows")
Expand Down
13 changes: 13 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,16 @@ check_action_type <- function(action = NULL, column_type = NULL) {
stop("'inline_menu' action can only be used with 'menu' column type")
}
}

make_table_sortable <- function(columns, sortable = TRUE) {
if (!sortable) {
return(columns)
} else {
for (col_name in names(columns)) {
if (is.null(columns[[col_name]]$sort)) {
columns[[col_name]]$sort <- TRUE
}
}
}
columns
}
5 changes: 4 additions & 1 deletion man/cheetah.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 17 additions & 9 deletions vignettes/cheetahR.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -121,23 +121,20 @@ cheetah(
)
```

## Filtering data

You can filter data by setting `search` to either `exact` or `contains` when you call `cheetah()` like so:
## Sorting option in cheetahR

```{r}
cheetah(penguins, search = "contains")
By default a cheetahR table is sortable. Otherwise, set `sortable = FALSE` in `cheetah()` to disable this functionality:
```{r, eval=TRUE}
cheetah(mtcars, rownames = FALSE, sortable = FALSE)
```

<br/>

## Sortable columns

To make a column sortable, you can pass `sort = TRUE` to the `column_def()` function:
However, to indivdually control the sorting option of each columns in the table, pass `sort = TRUE` to the `column_def()`:

```{r}
cheetah(
mtcars,
sortable = FALSE,
columns = list(
rownames = column_def(
width = 150,
Expand All @@ -154,6 +151,7 @@ If you want finer control over the sorting logic and provide your own, you can p
```{r}
cheetah(
mtcars,
sortable = FALSE,
columns = list(
rownames = column_def(
width = 150,
Expand All @@ -167,6 +165,16 @@ cheetah(
)
```

## Filtering data

You can filter data by setting `search` to either `exact` or `contains` when you call `cheetah()` like so:

```{r}
cheetah(penguins, search = "contains")
```

<br/>


## `cheetah()` usage in Shiny
cheetahR works seamlessly in a Shiny app. You can use it in both the UI and server components. In the UI, simply call `cheetahR::cheetahOutput()` to create a placeholder for the grid. In the server, use `cheetahR::renderCheetah()` to render the grid with your data and options.
Expand Down