I am trying to make an app which can dynamically create multiple tabs with tables in the 1st tab (Input tab), and use the dynamically created tables to do calculations in the 2nd tab (Results tab). However, I am not sure how to use the hot_to_r function to access the values from the tables created in the Input tab to do calculations in the Results tab. As an example, I want to take column i+1 divide by column i in the tables in the Input tab and display the results in the Results tab.
Below are the sample codes:
library(shiny)
library(rhandsontable)
ui <- navbarPage("App",
tabPanel("Input",
numericInput('num_of_table', "Number of sub tabs: ", value = 1, min = 1, max = 10),
uiOutput("input")),
tabPanel("Results",
uiOutput("results"))
)
server <- function(input, output,session) {
### Input ###
input_table<- reactive({
list_of_input_table = list()
for (i in c(1:input$num_of_table)){
mat <- matrix(c(1:25) * i, ncol = 5, nrow = 5)
list_of_input_table[[i]] = mat
}
index = c(1:i)
list_of_input_table[index]
})
observeEvent(input$num_of_table, {
lapply(seq_len(input$num_of_table), function(i) {
output[[paste0('input_table_', i)]] <- renderRHandsontable({
rhandsontable(input_table()[[i]])
})
})
})
output$input <- renderUI({
nTabs = input$num_of_table
myTabs1 = lapply(seq_len(nTabs), function(x){
tabPanel(paste("Tab", x),
column(12,
rHandsontableOutput(paste0("input_table_", x))))
})
do.call(tabsetPanel, myTabs1)
})
### Results ###
results_table<- reactive({
list_of_results_table = list()
list_of_input_table = list()
for (i in c(1:input$num_of_table)){
for (j in c(1:5)) {
list_of_input_table[[i]] <- as.matrix(hot_to_r(input[[paste0("input_table_",i)]]))
list_of_results_table[[i]] <- matrix(as.numeric(NA), ncol = 4, nrow = 5)
list_of_results_table[[i]][,j] <- list_of_input_table[[i]][,j+1][!is.null(list_of_input_table[[i]][,j+1])] / list_of_input_table[[i]][,j]
}}
index = c(1:i)
list_of_results_table[index]
})
observeEvent(input$num_of_table, {
lapply(seq_len(input$num_of_table), function(i) {
output[[paste0('results_table_', i)]] <- renderRHandsontable({
rhandsontable(results_table()[[i]])
})
})
})
output$results <- renderUI({
nTabs = input$num_of_table
myTabs2 = lapply(seq_len(nTabs), function(x){
tabPanel(paste("Tab", x),
column(12,
rHandsontableOutput(paste0("results_table_", x))))
})
do.call(tabsetPanel, myTabs2)
})
}
shinyApp(ui,server)
Please help!!