RShiny下载RMarkdown内的按钮

时间:2017-03-17 19:54:07

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

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

2 个答案:

答案 0 :(得分:2)

我认为你要找的是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中进行测试时,您的文件名不会受到尊重,但在浏览器中运行时,它会。

答案 1 :(得分:0)

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(我确信这可能是设计使光泽,但是如果有人可以解释为什么我想知道)。您可以通过编写新函数来更改此行为:

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


 ```
相关问题