Shiny R - 下载一张表的结果 [英] Shiny R - download the result of a table

查看:251
本文介绍了Shiny R - 下载一张表的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的,我创建了一个非常简单的闪亮的应用程序:

 库(闪亮)

ui< - fluidPage(
fluidRow(column(7,dataTableOutput('dto')))


服务器< - 函数(输入,输出){

输出$ dto< - renderDataTable({MYTABLE})


}

runApp(list(ui = ui ,server = server))

有什么办法可以选择下载表的结果(无论是CSV,XLSX ...)



欢呼

解决方案

使用 downloadButton() downloadLink() downloadHandler 如果您使数据本身成为一个反应式表达式。然后,您可以使用相同的反应表达式下载任何您发送到输出的内容。



一个小例子:



<$ p $

ui< - fluidPage(
#这个由id'下载'链接
downloadButton('download' ,下载数据),
fluidRow(列(7,dataTableOutput('dto')))


服务器< - 函数(输入,输出){
#用数据反应表达式,在这种情况下,iris
thedata< - reactive(iris)

输出$ dto< - renderDataTable({thedata()})
输出$ download< - downloadHandler(
filename = function(){thename.csv},
content = function(fname){
write.csv(thedata() ,fname)
}


}

runApp(list(ui = ui,server = server))

请记住:




  • 参数 content downloadHandler 必须是一个生成文件的函数!连接/文件名称应该有一个参数。在这个例子中,它是一个csv文件,但是对于例如图像,您可以使用 png() dev.off(),对于ggplots,您可以使用 ggsave(),...

  • 参数 filename 不一定是一个函数,但是我发现它的工作原理是
    。特别是当使用文件名的反应式表达式时。

  • 你链接 downloadHandler downloadButton 通过输出列表:的id = downloadButton downloadHandler 返回的输出元素的名称。



编辑:



有些人尝试使用 download.file()为此,但这也是错误的。功能 download.file()在用户端而不是服务器端使用时工作。它允许您将文件从互联网下载到正在调用该功能的计算机。如果您在Shiny应用程序中使用它,则在本地运行时会起作用。这是因为用户和服务器是同一台机器。但是,在Shiny Server上部署应用程序时, download.file()本质上是将文件下载到服务器,而不是从。


I am new to Shiny and I have created a really simple shiny app:

library(shiny)

ui <- fluidPage(
  fluidRow(column(7,dataTableOutput('dto')))
)

server <- function(input,output){

  output$dto <- renderDataTable({MYTABLE})


}

runApp(list(ui=ui,server=server))

Is there any way to put an option to download the result of the table (doesn't matter if is CSV, XLSX...)

cheers

解决方案

That's pretty easy with downloadButton() or downloadLink() in combination with downloadHandler if you make the data itself a reactive expression. Then you can download whatever you send to output using the same reactive expression.

A small example:

library(shiny)

ui <- fluidPage(
  # This one is linked by the id 'download'
  downloadButton('download',"Download the data"),
  fluidRow(column(7,dataTableOutput('dto')))
)

server <- function(input,output){
  # Reactive expression with the data, in this case iris
  thedata <- reactive(iris)

  output$dto <- renderDataTable({thedata()})
  output$download <- downloadHandler(
    filename = function(){"thename.csv"}, 
    content = function(fname){
      write.csv(thedata(), fname)
    }
  )

}

runApp(list(ui=ui,server=server))

Keep in mind:

  • the argument content of downloadHandler must be a function producing a file! It should take one argument for the connection/file name. In this example it is a csv file, but for eg images you can use png() and dev.off(), for ggplots you can use ggsave(), ...
  • the argument filename does not have to be a function, but I found it works better that way. Especially when working with reactive expressions for the file name.
  • you link the downloadHandler and the downloadButton through the output list: the id of downloadButton is the name of the output element returned by downloadHandler.

EDIT:

Some people try to use download.file() for this, but that's wrong as well. The function download.file() works when used on the user side, not the server side. It lets you download files from the internet to the computer that is calling the function. If you'd use that in a Shiny application, it would work when run locally. That's because user and server are the same machine. However, when deploying your app on a Shiny Server, download.file() would essentially be downloading files TO the server, not from.

这篇关于Shiny R - 下载一张表的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆