-
Notifications
You must be signed in to change notification settings - Fork 1
19 inline menu editor #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
58dabe9
dbd29c8
241a5f1
24f1a72
478b5d8
08371d4
d64c32d
1e3ed38
b1b5d5f
daf018a
aa53325
1c524b4
f26b963
3d24983
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,24 +7,32 @@ | |
#' @param width Column width. | ||
#' @param min_width Column minimal width. | ||
#' @param max_width Column max width. | ||
#' @param column_type Column type. There are 6 possible options: | ||
#' @param column_type Column type. By default, the column type is inferred from the data type of the column. | ||
#' There are 7 possible options: | ||
#' \itemize{ | ||
#' \item \code{"text"} for text columns. | ||
#' \item \code{"number"} for numeric columns. | ||
#' \item \code{"check"} for check columns. | ||
#' \item \code{"image"} for image columns. | ||
#' \item \code{"radio"} for radio columns. | ||
#' \item \code{"multilinetext"} for multiline text in columns. | ||
#' \item \code{"multilinetext"} for multiline text columns. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using markdown, \code{} becomes |
||
#' \item \code{"menu"} for menu selection columns. If \code{column_type == "menu"}, | ||
#' action parameter must be set to "inline_menu" and menu_options must be provided. | ||
#' Note: Works efficiently only in shiny. | ||
#' } | ||
#' @param action The action property defines column actions. Select | ||
#' the appropriate Action class for the column type. For instance, | ||
#' if the column type is \code{"text"}, the action can be \code{"input"}. | ||
#' There are 3 supported actions: | ||
#' the appropriate Action class for the column type. | ||
#' \itemize{ | ||
#' \item \code{"input"} for input action columns. | ||
#' \item \code{"check"} for check action columns. | ||
#' \item \code{"radio"} for radio action columns. | ||
#' \item \code{"inline_menu"} for menu selection columns. | ||
#' } | ||
#' @param menu_options A list of menu options when using \code{column_type = "menu"}. | ||
#' Each option should be a list with \code{value} and \code{label} elements. | ||
#' The menu options must be a list of lists, each containing a \code{value} | ||
#' and \code{label} element. | ||
#' The \code{label} element is the label that will be displayed in the menu. | ||
#' @param style Column style. | ||
#' @param message Cell message. Expect a [htmlwidgets::JS()] function that | ||
#' takes `rec` as argument. It must return an object with two properties: `type` for the message | ||
|
@@ -61,21 +69,46 @@ column_def <- function( | |
max_width = NULL, | ||
column_type = NULL, | ||
action = NULL, | ||
menu_options = NULL, | ||
style = NULL, | ||
message = NULL, | ||
sort = FALSE | ||
) { | ||
check_column_type(column_type) | ||
check_action_type(action, column_type) | ||
in_shiny <- shiny::isRunning() | ||
|
||
if (all(!is.null(column_type), column_type == "menu", !in_shiny)) { | ||
warning( | ||
"Dropdown menu action does not work properly outside a shiny environment" | ||
) | ||
} | ||
|
||
if ( | ||
all(!is.null(column_type), column_type == "menu", is.null(menu_options)) | ||
) { | ||
stop("menu_options must be provided when column_type is 'menu'") | ||
} | ||
|
||
if (!is.null(message) && !inherits(message, "JS_EVAL")) | ||
stop("message must be a JavaScript function wrapped by htmlwidgets::JS().") | ||
|
||
list( | ||
caption = name, | ||
width = width, | ||
minWidth = min_width, | ||
maxWidth = max_width, | ||
columnType = column_type, | ||
action = action, | ||
style = style, | ||
action = if (!is.null(action)) { | ||
if (action == "inline_menu") { | ||
list( | ||
type = action, | ||
options = menu_options | ||
) | ||
} else { | ||
action | ||
} | ||
}, | ||
message = message, | ||
sort = sort | ||
) | ||
|
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,8 +67,9 @@ The `column_type` parameter in `column_def()` allows you to specify different ty | |
- `"image"`: For image columns | ||
- `"radio"`: For radio button columns | ||
- `"multilinetext"`: For multiline text columns | ||
- `"menu"`: For dropdown menu selection columns | ||
|
||
The `column_type` parameter is optional. If it is not specified, the column type will be inferred from the data type. | ||
If `column_type` parameter is optional. If it is not specified, the column type will be inferred from the data type. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo? |
||
|
||
```{r} | ||
# Using checkbox column type to indicate NA values | ||
|
@@ -165,3 +166,64 @@ cheetah( | |
) | ||
) | ||
``` | ||
|
||
|
||
## `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. | ||
|
||
The grid will automatically update when the underlying data changes, making it perfect for reactive applications. All features like filtering, sorting, and custom column definitions work exactly the same way as in standalone R usage. | ||
|
||
|
||
One special feature that works particularly well in Shiny is the `menu` column type, which allows users to select from predefined options in a dropdown menu. This is ideal for interactive data editing workflows. | ||
|
||
## Menu column in Shiny | ||
By default, `cheetah()` automatically detects any "factor" columns in your data and converts them into menu columns. A menu column displays a dropdown menu with predefined options that users can select from. This is particularly useful when you want to restrict input to a specific set of valid choices. For example, if you have a factor column with levels "Low", "Medium", and "High", it will be displayed as a dropdown menu with these three options. | ||
```{r, eval=FALSE} | ||
library(shiny) | ||
library(bslib) | ||
library(cheetahR) | ||
|
||
|
||
ui <- page_fluid(cheetahOutput("grid")) | ||
|
||
server <- function(input, output) { | ||
output$grid <- renderCheetah({ | ||
cheetah(data = iris) | ||
}) | ||
} | ||
|
||
shinyApp(ui = ui, server = server) | ||
``` | ||
 | ||
|
||
 | ||
|
||
### Customizing the 'menu options' | ||
```{r, eval=FALSE} | ||
library(shiny) | ||
library(bslib) | ||
library(cheetahR) | ||
|
||
ui <- page_fluid(cheetahOutput("grid")) | ||
|
||
server <- function(input, output) { | ||
output$grid <- renderCheetah({ | ||
cheetah(data = iris, | ||
columns = list( | ||
Species = column_def( | ||
column_type = "menu", | ||
action = "inline_menu", | ||
menu_options = list( | ||
setosa = "Option Setosa", | ||
versicolor = "Option Vericolor" , | ||
virginica = "Option Virginica" | ||
) | ||
) | ||
) | ||
) | ||
}) | ||
} | ||
|
||
shinyApp(ui = ui, server = server) | ||
``` | ||
 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use markdown syntax in roxygen2. It's easier than having \itemize ...
Instead it can be: