2

I have a Shiny application that produces reads in a .csv file and produces an editable table.

library(dplyr)
library(rhandsontable)
options(shiny.maxRequestSize = 9*1024^2)

function(input, output) {

  values <- reactiveValues()

  Post <- c("Bank", "Bank")
  list2 <- c(12,13)
  df <- data.frame(Post, list2)

  Post <- c("Ba", "Ba")
  list2 <- c(12,13)
  df2 <- data.frame(Post, list2)

  performTextMining <- reactive({
    df$Post <- as.character(df$Post)
    df <- df %>% filter(Post == "Bank")   
    return(df)
  })

  output$contents <- renderRHandsontable({

    items <- c("Boodschappen", "Noodzakelijk")
    inFile <- input$file1

    if (is.null(inFile))
      return(NULL)

    df <- read.csv(inFile$datapath, header = input$header,
             sep = input$sep, quote = input$quote)
    #performTextMining()
    rhandsontable(df, width = 550, height = 300) %>%    
      hot_col(col = "Post", type = "dropdown", source = items)
  })
  filterData <- function(){
    setwd("C:/Users/Marc/Dropbox/PROJECTEN/Lopend/visualistation_training/shiny_examples")
    write.csv(df, file = "MyData.csv")
  }
  saveData <- function(){
    finalDF <- isolate(values[["df"]])
    setwd("C:/Users/Marc/Dropbox/PROJECTEN/Lopend/visualistation_training/shiny_examples")
    write.csv(finalDF, file = "MyData.csv")
  }
  observeEvent(input$saveBtn, saveData())
  observeEvent(input$writeBtn, filterData())

}

What I would like to do however is use the save button to store edit made in the table and then write an .csv file.

I therefore created a save button:

observeEvent(input$writeBtn, filterData())

Which triggers the following function:

saveData <- function(){
    finalDF <- isolate(values[["df"]])
    setwd("C:/Users/Marc/Dropbox/PROJECTEN/Lopend/visualistation_training/shiny_examples")
    write.csv(finalDF, file = "MyData.csv")
  }

This however leaves me with an empty .csv file. Any thoughts on what goes wrong here?

Henk Straten
  • 1,365
  • 18
  • 39

1 Answers1

7

To obtain the data back from edited rhandsontable one needs to call hot_to_r() function. Applying this in above case - code should look like following:

saveData <- function() {
  finalDF <- hot_to_r(input$contents)
  write.csv(
    finalDF, 
    file = "C:/Users/Marc/Dropbox/PROJECTEN/Lopend/visualistation_training/shiny_examples/MyData.csv"
  )
}
GoGonzo
  • 2,637
  • 1
  • 18
  • 25