Rhandsontable does not save reactive input to dataframe


#1

Hello,

I am new to R and R Shiny. I would like to have different input tables be saved in different dataframes a and b which could be reused later. Also, how could I scale it if I wanted to create many such tables and save to many different variables. I would highly appreciate insights how to do it. Thank you.

Here is my code, but it does not save reactive value to dataframe save_var:

library(shiny)
library(rhandsontable)

DF <-  data.frame(matrix(0.0, ncol = 5, nrow = 3))
col_names <- c("2018", "2019", "2020", "2021", "2022")
row_names <- c("Base", "Worst", "Best")
colnames(DF) <- col_names
rownames(DF) <- row_names

ui<-fluidPage(
  titlePanel("Handsontable"),
  sidebarLayout(
    sidebarPanel(
      helpText("Handsontable to save output to variable")
      
    ),
    mainPanel(
      rHandsontableOutput("hot", width = 350),
      rHandsontableOutput("cold", width = 350)
    )
  )
)

server<-function(input, output, session) {

  values = reactiveValues()
  
  data = reactive({
    if (!is.null(input$hot)) {
      DF = hot_to_r(input$hot)
    } else {
      if (is.null(values[["DF"]]))
        DF = DF
      else
        DF = values[["DF"]]
    }
    
    values[["DF"]] = DF
    DF
  })
  
  save_var <- renderDataTable({
    DF = data()
  if (!is.null(DF)) DF
    DF
    })
  
  print(save_var)
  
  output$hot <- renderRHandsontable({
    DF = data()
    if (!is.null(DF))
      rhandsontable(DF, stretchH = "all")
  })
}

shinyApp(ui = ui, server = server)