1

I am dynamically generating an unknown number of rhandsontable by querying the databased and rendering them using renderUI.

However, I can't find a way how to consume these tables after user might have made some modification on them since there's no input ID field in them. Can a custom ID be assigned to a rhandsontable using which we can get it's contents later?

Many thanks! Here's code to reproduce the issue. I'm generating some tables, and clicking on button once page has rendered will help browse where we can test variables using input$ etc.

library(shiny)
library(rhandsontable)
library(shinyWidgets)
ui <- fluidPage(
  fluidRow(
    uiOutput('test'),
    actionBttn(
      inputId = "Id107",
      label = "button",
      style = "unite", 
      color = "danger"
    )
  )
)
server <- function(input, output, session) {
  var1 <-c(1,2,3)
  var2 <-c('X','Y','Z')
  var3 <-c('Sample1','Sample2')
  observeEvent(input$Id107,{
    browser()
  })
  output$test = renderUI({
    table_names<-c('Alpha', 'Beta', 'Gamma')
    t<- matrix(data = 0, nrow = length(var2), ncol = length(var1)) %>%
      `rownames<-`(c(var2)) %>%
      `colnames<-`(c(var1))
    t1<-t
    t2<-t
    input_list <- lapply(1:length(var3), function(i) {
      new_list <- lapply(1:length(table_names),function(j) paste(var3[i] ," ", table_names[j], sep = "") )
      list(
        column(12,
               column(5,align='left',withTags(div(h5(b(new_list[1]))))),
               column(4,align='left',withTags(div(h5(b(new_list[2]))))),
               column(3,align='left',withTags(div(h5(b(new_list[3]))))),
        ),
        column(12,
               column(5,div(id = gsub("[^[:alnum:]]", "_", new_list[1]),renderRHandsontable(
                 rhandsontable(t, overflow='hidden',maxRows=nrow(t), minRows=nrow(t)) %>% 
                   hot_validate_numeric(c(1:ncol(t))) %>%
                   hot_table(stretchH = "all") %>%
                   hot_col(c(1:ncol(t)),format = "$0,0")))
               ),
               column(4,div(id = gsub("[^[:alnum:]]", "_", new_list[2]),renderRHandsontable(#
                 rhandsontable(t1, overflow='hidden',maxRows=nrow(t1), minRows=nrow(t1)) %>% 
                   hot_validate_numeric(c(1:ncol(t1))) %>%
                   hot_table(stretchH = "all") %>%
                   hot_col(c(1:ncol(t1)),format = "0.00%")))
               ),
               column(3,div(id = gsub("[^[:alnum:]]", "_", new_list[3]),renderRHandsontable(#
                 rhandsontable(t2, overflow='hidden',maxRows=nrow(t2), minRows=nrow(t2)) %>% 
                   hot_validate_numeric(c(1:ncol(t2))) %>%
                   hot_table(stretchH = "all") %>%
                   hot_col(c(1:ncol(t2)),format = "0")))
               )
        )
      )
    })
    do.call(tagList,input_list)
  })
}
shinyApp(ui, server)
LBZR
  • 161
  • 12
  • I know that most components of this package are based on handsontable and apparently there is a ID column but I can't seem to use it anywhere https://handsontable.com/docs/vue-custom-id-class-style/ – LBZR Nov 17 '21 at 20:34
  • What about [this thread](https://stackoverflow.com/a/55849347/9406040)? If this doesn't help, could you post a minimal example for easier reproduction of your problem? – Roman Nov 17 '21 at 21:09
  • thank you for your response. In the link shared the name of table 'hot' is known and hence can be referenced. I updated the code where I am generating some tables but ID of each rhandsontable is unknown- appreciate any help on this – LBZR Nov 17 '21 at 21:33

1 Answers1

0

Posted it in different thread but we essentially don't need any ID field if we generate tables as mentioned in this post

Assign/change div ID of dynamically generated UI components (R Shiny/Javascript)

LBZR
  • 161
  • 12