1

我有一个 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())

}

然而,我想做的是使用保存按钮来存储在表中所做的编辑,然后编写一个 .csv 文件。

因此,我创建了一个保存按钮:

observeEvent(input$writeBtn, filterData())

这会触发以下功能:

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

然而,这给我留下了一个空的 .csv 文件。关于这里出了什么问题的任何想法?

4

1 回答 1

5

要从已编辑的数据中获取数据,rhandsontable需要调用hot_to_r()函数。在上述情况下应用此代码,代码应如下所示:

saveData <- function(){
  finalDF <- hot_to_r( input$contents )
  write.csv(finalDF, file = "C:/Users/Marc/Dropbox/PROJECTEN/Lopend/visualistation_training/shiny_examples/MyData.csv")
  }
于 2018-01-01T22:38:10.933 回答