【问题标题】:Shiny R, Using the downloadButton functionShiny R,使用 downloadButton 功能
【发布时间】:2015-02-26 01:37:44
【问题描述】:

我正在尝试弄清楚如何使用 downloadButton 将多个内容下载到一个 pdf 文件中。我在 ui 中创建了下载按钮:

downloadButton('downloadData', 'Download Data'))

我正在尝试将多个绘图和表格放入一个 pdf 文件中。我在服务器文件中有以下

 output$downloadData <- downloadHandler(
    filename = function() {
      paste('data-', Sys.Date(), '.pdf', sep=&rdquo;)
    }

但我认为这会保存多个 csv 文件。如何从渲染中下载多个绘图和表格,例如

output$table<-renderTable({
        P.Value<- c(lev.p,bart.p,turn.p,shap.p,jar.p,linm.p) 
        Test.Statistic<-c(lev.s,bart.s,turn.s,shap.s,jar.s,linm.s) 
        df<-data.frame(P.Value,Test.Statistic)
        rownames(df, do.NULL = TRUE, prefix = "row")
        rownames(df) <- c("Levene Test","Bartlett Test","Turning Point Test","Shapiro-Wilk Test","Jarque Bera Test","Linear Model Constant Drift Test")

        df
  })

,

  output$qq.line<-renderPlot({

    index<-1:length(lg.ret.vec())
    model<-lm((lg.ret.vec())~ index) 
    plot(model,which=2,main="QQ plot of the residuals",xlab="Theoretical Quantiles")             
    qqline(rstandard(model),col="black")
  })

,

  output$histogram<-renderPlot({

      hist(lg.ret(),main="Histogram of log returns",xlab="Log returns")
  })

仅举几例。

【问题讨论】:

    标签: r download shiny


    【解决方案1】:

    您可以利用rmarkdown 为您的所有地块生成报告,here is one example

    在您的情况下,您可以使用以下downloadHandler(代码改编自链接):

    output$downloadData <- downloadHandler(
        filename = function() {
          paste('report', sep = '.','html')
        },
    
        content = function(file) {
          src <- normalizePath('report.Rmd')
    
          owd <- setwd(tempdir())
          on.exit(setwd(owd))
          file.copy(src, 'report.Rmd')
    
          library(rmarkdown)
          out <- render('report.Rmd',html_document())
          file.rename(out, file)
        }
      )
    

    并在与您的 server.R 和 ui.R 相同的文件夹中创建一个名为 report.Rmd 的文件:

    report.Rmd

    ---
    title: "Your report"
    output: html_document
    ---
    
    This is your plot
    
    ```{r echo=FALSE}
        index<-1:length(lg.ret.vec())
        model<-lm((lg.ret.vec())~ index) 
        plot(model,which=2,main="QQ plot of the residuals",xlab="Theoretical Quantiles")             
        qqline(rstandard(model),col="black")
    
    ```
    
    This is your histogram
    
    ```{r echo=FALSE}
    hist(lg.ret(),main="Histogram of log returns",xlab="Log returns")
    ```
    

    【讨论】:

    • 我已经尝试过这个并试图摆弄它,但它只是创建了一个标题为“报告”的 html 文档。
    • 好的,你已经安装了库?,当我尝试它时,每次更改report.Rmd文件时我都必须重新启动R,也许它被缓存了
    • 对于我所有的情节它都有效,但对于问题中所述的表格示例,它不起作用。 P.Value 和 test.statistic 只是数值的向量。您知道为什么它不适用于数据框吗?
    【解决方案2】:

    您需要为 downloadHandler 定义要下载的内容。见http://www.inside-r.org/packages/cran/shiny/docs/downloadHandler

    您的意思是您试图将绘图和表格下载到单个 csv 文件中?你的意思是合并数据?您可能对 csv 文件是什么感到困惑。

    【讨论】:

    • 我了解如何定义内容,但它只是试图定义多个内容以放入一个文件中。我在 csv 部分的错误,我的意思是任何可以显示表格、图表和结果的知名文件(例如 .pdf 或 .xls),
    • 也许尝试定义您的内容并使用 c() ...downloadHandler(filename, content = c(data1, data2, data3))
    • 您说您认为您提供的代码会下载多个 ... 文件。当你运行它时实际发生了什么?
    • 我尝试使用 output$downloadData
    猜你喜欢
    • 2021-09-04
    • 2021-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多