Downloading table using Rhandsontable in shiny

Viewed 177

I have created a table in shiny using Rhandsontable package in R. I am able to download the table in csv using the below code, however, i am looking to create a functionality in such a way that the input validations and checkboxes remain available to the user in the downloaded file as well. Is it possible to do this in shiny ?

library(rhandsontable)
library(shiny)

ui <- fluidPage(
  titlePanel("Handsontable"),
  sidebarLayout(
    sidebarPanel(
      helpText("Handsontable demo output. Column add/delete does work ",
               "for tables with defined column properties, including type."),
      radioButtons("useType", "Use Data Types", c("TRUE", "FALSE"))
    ),
    mainPanel(
      rHandsontableOutput("hot", width = 350),
      actionButton("savefile", "Save", width = '100%')
    )
  )
)

server <- function(input, output, session) {
  # this caching step is no longer necessary
  # it was left as an example
  values = reactiveValues()
  
  data = reactive({
    if (!is.null(input$hot)) {
      DF = hot_to_r(input$hot)
    } else {
      if (is.null(values[["DF"]]))
        DF = data.frame(val = 1:10, bool = TRUE, big = LETTERS[1:10],
                        small = letters[1:10],
                        stringsAsFactors = F)
      else
        DF = values[["DF"]]
    }
    
    values[["DF"]] = DF
    DF
  })
  
  output$hot <- renderRHandsontable({
    DF = data()
    if (!is.null(DF))
      rhandsontable(DF, useTypes = as.logical(input$useType), stretchH = "all") %>%
      hot_col(col = "big", type = "dropdown", source = LETTERS) %>%
      hot_col(col = "small", type = "autocomplete", source = letters,
              strict = FALSE)
  })
  
  observeEvent(input$savefile,
               {
                 if (!is.null(isolate(input$hot)))
                 {
                   #Convert to R object
                   x <- hot_to_r(isolate(input$hot))
                   write.csv(x, file = '~/employee_input.csv', row.names=FALSE)
                 }
               }
  )
}

shinyApp(ui, server)

Please change the directory in "write.csv" in case the downloaded file is not located.
0 Answers
Related