RMarkdown 中的 RShiny 下载按钮

ZBa*_*auc 5 r rstudio r-markdown shiny

我似乎无法downloadButton在 rmarkdown 文档中使用runtime: shiny. 这是一个与我正在做的类似的例子。

 ---
 title: "R Document"
 runtime: shiny
 ---

 ```{r, echo = FALSE}
 numericInput("SS", "Selecr SS", min = 1, max = 100, value = 1)

 RandomSample <- reactive({
   data.frame(X = rnorm(100), Y = rnorm(100))
 })

 downloadButton("download", "Download")

 renderPlot({
   plot(RandomSample()[(1:input$SS), "X"], RandomSample()[(1:input$SS), "Y"])
 })

 renderTable({
   RandomSample()[(1:input$SS),]
 })
 ```
Run Code Online (Sandbox Code Playgroud)

我想要下载按钮来下载RandomSample(),但我什至无法downloadButton显示。

Jer*_*ell 5

我认为您正在寻找的是downloadHandler

这是您的示例,它可以正常工作:

---
title: "R Document"
runtime: shiny
output: html_document
---
```{r, echo=FALSE}

 numericInput("SS", "Selecr SS", min = 1, max = 100, value = 1)

 RandomSample <- reactive({
   data.frame(X = rnorm(100), Y = rnorm(100))
 })

 downloadHandler(filename = function() { 
    return(paste('Example', input$SS, '.csv', sep=''))

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

 renderPlot({
   plot(RandomSample()[(1:input$SS), "X"], RandomSample()[(1:input$SS), "Y"])
 })

 renderTable({
   RandomSample()[(1:input$SS),]
 })
 ```
Run Code Online (Sandbox Code Playgroud)

请注意,在 RStudio 中测试时不会考虑您的文件名,但在浏览器中运行时会考虑。