2018-10-26
阅读量:
1688
在R里构建的shiny应用程序如何下载数据

Shiny也提供了下载计算结果文件的特性,这能可以很容易地构建报告系统。
要运行下面的例子,键入:
> library(shiny)
> runExample("10_download")
要定义文件下载的功能,在服务端用downloadHandler
使用函数,在UI端使用downloadButton
或 downloadLink
。
shinyUI(pageWithSidebar(
headerPanel('Download Example'),
sidebarPanel(
selectInput("dataset",
"Choose a dataset:",
choices
=
c("rock",
"pressure",
"cars")),
downloadButton('downloadData',
'Download')
),
mainPanel(
tableOutput('table')
)
))
shinyServer(function(input,
output)
{
datasetInput
<-
reactive({
switch(input$dataset,
"rock"
=
rock,
"pressure"
=
pressure,
"cars"
=
cars)
})
output$table
<-
renderTable({
datasetInput()
})
output$downloadData
<-
downloadHandler(
filename
=
function()
{
paste(input$dataset,
'.csv',
sep='')
},
content
=
function(file)
{
write.csv(datasetInput(),
file)
}
)
})
正如你看的那样,downloadHandler
接收参数filename
,这个参数告诉浏览器保存时默认的文件名,它可以是简单的字符串,也可以是返回字符串的函数(如上例)。
content
参数必须是包含一个参数的函数,这个参数是临时文件的文件名。content
函数的作用是往要下载的临时文件里写内容。
filename
和 content
参数可以使用反应值或表达式(尽管在上面例子中的filename
,参数是实际的函数,filename = paste(input$dataset, '.csv')
不会按你想的那样运行,因为当下载处理器定义时,它只计算一次)。
一般来说,只有两个参数你需要。有个可选参数contentType
,如果它是NA
或 NULL
,shiny会基于文件名猜测一个合适的值。如果你想改变这一行为,可以提供一个类型的字符串(比如,"text/plain"
)






评论(0)


暂无数据
推荐帖子
0条评论
0条评论
1条评论