Reputation: 259
I have a data frame that I am passing to a rhandsontable. Let's say it has 10 columns. I have a drop-down that has 3 choices:
Be default, when the rhandsontable loads, it will show all 10 columns. When the use selects one of the three options in the drop-down, I want to hide certain columns using the hot_col(col = col_name, width = 0.5)
For example, if the user selects option 1 - Show columns 1 through 5, hiding columns 6 through 10 would look something like:
rhandsontable(df) %>%
hot_col(col = column_6, width = 0.5) %>%
hot_col(col = column_7, width = 0.5) %>%
hot_col(col = column_8, width = 0.5) %>%
hot_col(col = column_9, width = 0.5) %>%
hot_col(col = column_10, width = 0.5)
I attempted filtering the data set with something like:
df <- if (input$dropdown == "Show columns 1 through 5") {df %>% select(1:5)}
else if (input$dropdown == "Show columns 1, 5 and 10") {df %>% select(1, 5, 10)}
else if (input$dropdown == "Show columns 6 through 10") {df %>% select(6:10)}
else {df %>% select(1:10)}
which works for only showing specific columns, but I have hot_col
rules specific to different columns which bombs because if I have a rule saying that column_6 is of date type, it won't find column_6 if "Show columns 1 through 5" is chosen.
Sorry I don't have a working example, but hope this makes sense. Thanks!
Upvotes: 2
Views: 764
Reputation: 10365
You can determine from the selectInput
which columns should be hidden and then use this information in the renderRHandsontable
call to dynamically hide columns:
library(shiny)
library(rhandsontable)
ui <- fluidPage(
fluidRow(
column(width = 6,
selectInput( inputId = "columns",
label = "select columns",
choices = c("all", "first half", "second half")
)
),
rHandsontableOutput("table")
)
)
server <- function(input, output, session) {
output$table <- renderRHandsontable({
output <- rhandsontable(mtcars[, 1:4])
# determine the columns to show
if (input$columns == "all") columns_to_hide <- NULL
if (input$columns == "first half") columns_to_hide <- 3:4
if (input$columns == "second half") columns_to_hide <- 1:2
used_colnames <- colnames(mtcars[, 1:4])
if (!is.null(columns_to_hide)) {
for (col_name in used_colnames[columns_to_hide]) {
output <- output %>% hot_col(col = col_name, width = 0.5)
}
}
output
})
}
shinyApp(ui, server)
Upvotes: 1