简体   繁体   English

R-Shiny下载按钮说明

[英]R-shiny download button explained

I'm trying to get my head arround the following. 我正在努力使我的头脑在以下方面。 Trying to understand the workings of R-shiny more deeply. 试图更深入地了解R-shiny的工作原理。

In creating a download button, R shiny advises to use the following code-snipped: 在创建下载按钮时,R Shiny建议使用以下代码片段:

output$downloadData <- downloadHandler(
  filename = function() {
    paste('data-', Sys.Date(), '.csv', sep='')
  },
  content = function(con) {
    write.csv(data, con)
  }
)

A filename is created with the function named: filename. 使用名为:filename的函数创建文件名。 And, content is written using a function created named content. 并且,使用创建的名为内容的函数编写内容。

The content function takes con as input. 内容功能将con作为输入。 This, in my understanding, just sets the filename/filepath parameter of the write.csv function inside the content function. 以我的理解,这只是在content函数内部设置了write.csv函数的filename / filepath参数。

Why is the filename (function) not used as an input argument, and why does this nontheless sets the path for the filename/filepath argument in the conent function/ write.csv? 为什么不将文件名(函数)用作输入参数,为什么仍要在conent函数/ write.csv中设置文件名/文件路径参数的路径?

Actually, the current documentation of downloadHandler uses the following in the examples 实际上, downloadHandler的当前文档在示例中使用以下内容

content = function(file) {
  write.csv(data, file)
}

The documentation of the content argument also mentions file paths, not connections content参数的文档还提到了文件路径,而不是连接

content 内容
A function that takes a single argument file that is a file path (string) of a nonexistent temp file, and writes the content to that file path. 该函数接受单个参数文件,该参数文件是不存在的临时文件的文件路径 (字符串),并将内容写入该文件路径。 (Reactive values and functions may be used from this function.) (可以从此功能使用反应性值和功能。)

The reason both filename and content are functions is so they can both change based on the state your application is currently in. If you want content to be dependent on the input your user, you might use for examle this. filenamecontent都是函数的原因是,它们都可以根据应用程序当前所处的状态进行更改。如果希望内容取决于用户的输入,则可以使用它作为示例。

content = function(file) {
  write.csv(get_current_data(input$choose_dataset), file)
}

The same is true for filename filename也是如此

filename = function() { paste0(input$choose_dataset, ".csv") }

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

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