I am dynamically generating a couple of tables in an R shiny app using uiOutput/renderUI. These tables are editable in that they present retrieved data, which the user can then change and send it for further processing. I am using rhandsontable, but essentially DT, excelR, or any other table can be used in theory- however none of them have an ID property.
This is how the rendered page looks like as of now(for each new entry in var3, a new fluidrow of 3 tables is rendered)
Every tables's label is unique and can be used as an identifier during table creation, just like how I'm using it for creating label on top of tables. If I inspect element and look for a table's div ID, I am able to get content of the tables. eg here using input$outbd32eabbd01a4806 gives me the data.
However this ID is unknown inside the application and the number of tables are unknown. If I wrap each table with a div whose ID is known(eg Sample2_Alpha) at creation, it creates another div element on top of this dynamic div id and I still can't get hold of this table
I think there might be couple ways to go about it but following approaches come to mind. I'm not well versed with Javascript, but are any of these possible?
- Assigning the div ID while creating dynamic table? In this approach, directly input$varName will give me table content(need to use hot_to_r(input$varName) to read here)
- If we wrap each element with a known div ID, then the child div of that component? In this approach, I can maintain a matrix of each known div and their child div that I can use to retrieve data just as above.
Thank you
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,{
#We should be able to get the input$ID of all dynamically generated tables here to retrieve any changes
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)