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)
2
Answers
Following ismirsehregal's comment, the below appears to work. Changes from OP code is replacing
NA
in data frame setup withNA_character_
, and addition of thehtmlwidgets::onRender(...)
to theoutput$hottable
:Below is solution using
Javascript
which should do it:We use an afterInit event where updateSettings is called. This defines the dropdown settings in the last row.
Notice here that
select_option
is the vector defined inR
which I passed below to therhandsontable
object such that I can use it inJS
by accessinginstance.params
. However, I wrapped the code into a smallsetTimeout
because it seems that directly whenafterInit
is invoked,params
is not available. The only reason for using this is that you can define the options inR
, if you have no problem to define them directly inJS
, you can drop it below. I also tried other events but had several problems which may occur due to theshiny
environment.rhandsontable
is not maintained since several years and in particular relies onhandsontable 6.2.2
. There was a bug in an older version (see handsontable/handsontable#7689) where the column headers were rendered wrong after usingupdateSettings
. This is at least similar to an issue which I also had and so I used what was committed in order to solve this issue in anafterRenderer
event:It would look like this: