简体   繁体   English

将RHandsontable的编辑存储在Shiny应用程序中

[英]Store edits of RHandsontable in a Shiny application

I have a Shiny application that produces reads in a .csv file and produces an editable table. 我有一个Shiny应用程序,该程序在.csv文件中产生读取并产生可编辑的表。

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. 但是,我要执行的操作是使用“保存”按钮来存储在表中所做的编辑,然后编写一个.csv文件。

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. 但是,这给我留下了一个空的.csv文件。 Any thoughts on what goes wrong here? 对这里出什么问题有任何想法吗?

To obtain data from edited rhandsontable one needs to call hot_to_r() function. 要从已编辑的rhandsontable获取数据,需要调用hot_to_r()函数。 Applying this in above case, code should looks 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")
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM