r语言 - RMarkdown中的RShiny下载按钮



我似乎无法让闪亮的downloadButton在带有runtime: shiny的 rmarkdown 文档中工作。这是一个类似于我正在做的事情的例子。

 ---
 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),]
 })
 ```

我希望下载按钮下载RandomSample(),但我什至无法显示downloadButton

我认为您正在寻找的是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),]
 })
 ```

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

downloadButton有点奇怪。如果你看一下函数,它是:

function (outputId, label = "Download", class = NULL, ...)  {
aTag <- tags$a(id = outputId, class = paste("btn btn-default shiny-download-link", 
    class), href = "", target = "_blank", download = NA, 
    icon("download"), label, ...)
}

downloadLink是:

function (outputId, label = "Download", class = NULL, ...) {
    tags$a(id = outputId, class = paste(c("shiny-download-link", 
        class), collapse = " "), href = "", target = "_blank", 
        download = NA, label, ...)
}

这意味着downloadButton的返回值是不可见的,因此它不会生成在 markdown 中工作的 html(我敢肯定这可能是 shiny 的设计,但如果有人可以解释为什么我想知道(。您可以通过编写一个新函数来更改此行为:

downloadButtonRmd <- function (outputId, label = "Download", class = NULL, ...)  {
     tags$a(id = outputId, class = paste("btn btn-default shiny-download-link", 
        class), href = "", target = "_blank", download = NA, 
        icon("download"), label, ...)
 }

其中有可见的输出。当您从一个文档进行多次下载时,这可能很有用(这就是导致我来到这里的原因(

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

```{r}
downloadButtonRmd <- function (outputId, label = "Download", class = NULL, ...)  {
     tags$a(id = outputId, class = paste("btn btn-default shiny-download-link", 
        class), href = "", target = "_blank", download = NA, 
        icon("download"), label, ...)
 }
```

```{r, echo=FALSE}
numericInput("SS1", "Select SS1", min = 1, max = 100, value = 1)
numericInput("SS2", "Select SS2", min = 1, max = 100, value = 1)
downloadButtonRmd("down1", label = "Download1")
downloadLink("down2", label = "Download2")

RandomSample <- reactive({
   data.frame(X = rnorm(100), Y = rnorm(100))
 })
output$down1 <- downloadHandler(filename = function() { 
    return(paste('Example', input$SS1, '.csv', sep=''))
 }, content = function(file) {
   write.csv(RandomSample(), file)
 })
output$down2 <- downloadHandler(filename = function() { 
    return(paste('Example', input$SS2, '.csv', sep=''))
 }, content = function(file) {
   write.csv(RandomSample(), file)
 })
renderPlot({
   plot(RandomSample()[(1:input$SS1), "X"], RandomSample()[(1:input$SS1), "Y"])
 })
renderPlot({
   plot(RandomSample()[(1:input$SS2), "X"], RandomSample()[(1:input$SS2), "Y"])
 })
renderTable({
   RandomSample()[(1:input$SS1),]
 })
renderTable({
   RandomSample()[(1:input$SS2),]
 })

 ```

最新更新