I'm trying to adapt the answer to post Is there a way to have different dropdown options for different rows in an rhandsontable? to my situation in the below code. I would like the first two rows of the rendered table to require the user to input numeric values manually (in the fuller code this is derived from, the user is able to manually input numeric values in place of the rendered default values), and the last row (row name "select_option") to require a selection in the dropdown. Currently, the below code only offers dropdowns for all rows in the column.
Is this possible? (First 2 rows manual inputs of numeric values, last row dropdown).
From my review of the package documentation for rhandsontable, the function hot_row()
is far more limited than hot_col()
. Also, an added twist is the larger App this is intended for allows the addition of columns via actionButton()
and I would need the added columns to also have the same dropdown for this one "selection_option" row.
Code:
library(shiny)
library(rhandsontable)
ui <- fluidPage(hr(),mainPanel(rHandsontableOutput("ExampleTable")))
server <- function(input, output) {
DF <- reactiveVal(data.frame(Object = c("enter_data", "enter_data", "select_option"), Needs = NA_character_, stringsAsFactors = FALSE))
observeEvent(input$ExampleTable, {
DF(hot_to_r(input$ExampleTable))
})
output$ExampleTable <- renderRHandsontable({
select_optionOptions <- c(NA_character_, "dog", "cat") # defines the dropdown options
tmpExampleTable <- rhandsontable(DF(), rowHeaders = NULL, stretchH = "all", selectCallback = TRUE, width = 300, height = 300) %>%
hot_col("Object", readOnly = TRUE) %>%
hot_col("Needs", allowInvalid = FALSE, type = "dropdown", source = NA_character_, readOnly = TRUE)
if(!is.null(input$ExampleTable_select$select$r)){
selectedObject <- DF()[input$ExampleTable_select$select$r, "Object"]
if(selectedObject == "select_option"){
tmpExampleTable <- hot_col(tmpExampleTable, col = "Needs", allowInvalid = FALSE, type = "dropdown", source = select_optionOptions) %>% hot_cell(row = input$ExampleTable_select$select$r, col = "Needs", readOnly = FALSE)
}
}
tmpExampleTable
})
}
shinyApp(ui = ui, server = server)