尝试将闪亮的应用数据(csv)写入桌面上的文件夹。闪亮警告:文件错误:无法打开连接

时间:2018-04-27 15:09:26

标签: shiny

我是个新手,我正在努力创建一个调查,将提交的数据下载到我桌面上的文件夹中。

我使用这篇文章作为指南,基本上有完全相同的代码,但使用了"本地存储"保存数据的选项。

当我运行app时,文件被保存到桌面上的正确文件夹中,但是当我在shinyapps.io上发布并尝试使用url提交回复时,我收到错误: 文件中的警告(文件,ifelse(追加," a"," w") 和 无法打开文件&c; /Users/tolson/Desktop/Shiny/1524841166_57ededaa8d9c46a94f5bb4a352412699.csv':没有这样的文件或目录 2018-04-27T14:59:26.847383 + 00:00 shinyapps [331397]:警告:文件错误:无法打开连接

这是否与写入闪亮的权限有关?我已经花了很多时间研究但没有运气。任何帮助或见解将不胜感激。提前谢谢。

Code: 
library(shiny)

# Define the fields we want to save from the form
fields <- c("name", "dog", "food")

# Save a response
# ---- This is one of the two functions we will change for every storage 
type ----
saveData <- function(data) {
  data <- as.data.frame(t(data))
  if (exists("responses")) {
    responses <<- rbind(responses, data)
  } else {
    responses <<- data
  }

}

# Load all previous responses
# ---- This is one of the two functions we will change for every storage 
type ----
loadData <- function() {
  if (exists("responses")) {
    responses
  }
}

# Shiny app with 3 fields that the user can submit data for
shinyApp(
  ui = fluidPage(
    DT::dataTableOutput("responses", width = 300), tags$hr(),
    textInput("name", "Name", ""),
    textInput("dog", "what is your favorite dog?"),
    textInput("food", "favorite food?"),
    actionButton("submit", "Submit")
  ),
  server = function(input, output, session) {

# Whenever a field is filled, aggregate all form data
    formData <- reactive({
      data <- sapply(fields, function(x) input[[x]])
      data
    })

# When the Submit button is clicked, save the form data
    observeEvent(input$submit, {
      saveData(formData())
    })

# Show the previous responses
# (update with current response when Submit is clicked)
    output$responses <- DT::renderDataTable({
      input$submit
      loadData()
    })     
    outputDir <- "C:/Users/tolson/Desktop/Shiny"

    saveData <- function(data) {
      data <- t(data)
      # Create a unique file name
      fileName <- sprintf("%s_%s.csv", as.integer(Sys.time()), 
digest::digest(data))
      # Write the file to the local system
      write.csv(
        x = data,
        file = file.path(outputDir, fileName), 
        row.names = FALSE, quote = TRUE
      )
    }

    loadData <- function() {
       # Read all the files into a list
      files <- list.files(outputDir, full.names = TRUE)
      data <- lapply(files, read.csv, stringsAsFactors = FALSE) 
      # Concatenate all data together into one data.frame
      data <- do.call(rbind, data)
      data
    }





     }
 )

0 个答案:

没有答案