Question
How to add a dropdown menu to a single row in a table rendered with rhandsontable while other rows contain numeric values?
In the below R Shiny code, I am trying to add a dropdown menu to only the last row of the table rendered with rhandsontable
. Note that the table is expandable by the user, column-wise, via the action button "Add series". How do I apply the dropdown to only the last row of the table, and not every row of the table as currently occurs with the below code? I've tried hot_row
, hot_rows
, and hot_cell
, but I'm not sure they support this. See the explanatory illustration below. Note that the dropdown needs to render with every added column too, which currently works but the dropdowns should not render in Row_A and Row_B. The dropdown should only render in Row_C.
Code:
library(rhandsontable)
library(shiny)
ui <-
fluidPage(
rHandsontableOutput('hottable_1'),
actionButton("addSeries","Add series")
)
server <- function(input,output,session)({
seriesTbl_1 <- reactiveVal(
data.frame(
'Series 1' = c(1,24,NA),
row.names = c("Row_A_numeric","Row_B_numeric","Row_C_dropdown")
)
)
observeEvent(input$hottable_1, {seriesTbl_1(hot_to_r(input$hottable_1))})
output$hottable_1 <- renderRHandsontable({
tbl <- seriesTbl_1()
select_option <- c(NA_character_, "Item A", "Item B")
rhandsontable(
tbl,
rowHeaderWidth = 200,
useTypes = TRUE,
selectCallback = TRUE,
overflow = "visible"
) %>%
hot_table(id = "hottable_1") %>%
hot_col(
col = names(tbl),
allowInvalid = FALSE,
type = "dropdown",
source = select_option
)
})
observeEvent(input$addSeries, {
newSeriesCol_1 <- data.frame(c(1,24,NA))
names(newSeriesCol_1) <- paste("Series", ncol(hot_to_r(input$hottable_1)) + 1)
seriesTbl_1(cbind(seriesTbl_1(), newSeriesCol_1))
})
seriesTbl_1_DF <- reactive({seriesTbl_1()})
})
shinyApp(ui, server)