【问题标题】:how to print a report in shiny using r markdown?如何使用 r markdown 以闪亮的方式打印报告?
【发布时间】:2020-01-17 16:43:41
【问题描述】:

我需要使用 r markdown 从闪亮的应用程序打印报告。我一直在尝试按照示例进行操作,但经过几个小时后,我需要一些帮助。

有4个文件:app.R、report.Rmd、calculation.R和datos.xlsx datos.xlsx 是一个 excel 文件,其中包含由计算.R 中定义的函数使用并由 app.R 使用的信息

app.R 应在屏幕上提供结果和可下载的报告。我没有得到后者,过去两天我一直在为此苦苦挣扎。

非常感谢!

app.R:


ui <- fluidPage(

    titlePanel("Calculations"),
    sidebarLayout(
      sidebarPanel(
        fileInput("file1","Select excel file with data", accept=c("excel",".xlsx",".xls")),

        "When are available on the screen, you can download the report",
        radioButtons('format', 'Document format', c('PDF', 'HTML', 'Word'),
                     inline = TRUE),
        downloadButton('downloadReport')
        ),

    mainPanel(
      tableOutput(outputId = "tabla")
      )
    ))


server <- function(input, output) {

  source("./calculations.R")

  output$tabla<-renderTable({
    infile<-input$file1
    if(is.null(infile))return(NULL)
    calculo(infile$datapath)})


  output$downloadReport <- downloadHandler(
    filename = function() {
      paste('my-report', sep = '.', switch(
        input$format, PDF = 'pdf', HTML = 'html', Word = 'docx'
      ))
    },

    content = function(file) {
      src <- normalizePath('report.Rmd')

      # temporarily switch to the temp dir, in case you do not have write
      # permission to the current working directory
      owd <- setwd(tempdir())
      on.exit(setwd(owd))
      file.copy(src, 'report.Rmd', overwrite = TRUE)

      library(rmarkdown)
      out <- render('report.Rmd', switch(
        input$format,
        PDF = pdf_document(), HTML = html_document(), Word = word_document()
      ))
      file.rename(out, file)
    }
  )

}

# Run the application 
shinyApp(ui = ui, server = server)

report.Rmd:


title: "动态报告" 输出:word_document


knitr::opts_chunk$set(echo = FALSE,warning=FALSE,message = FALSE)

结果

print(output$tabla)

计算.R

library(xlsx)
calculo<-function(archivo){
  incrementos_descuentos<-read.xlsx(archivo,sheetName="incrementos - descuentos",  check.names = FALSE)  
  incrementos_descuentos$`Desde / mm`<-as.numeric(as.character(incrementos_descuentos$`Desde / mm`))
  incrementos_descuentos$`A  / mm`<-as.numeric(as.character(incrementos_descuentos$`A  / mm`))
  incrementos_descuentos$`Volumen / L`<-as.numeric(as.character(incrementos_descuentos$`Volumen / L`))
  incrementos_descuentos$`Resultado L/m`<-as.numeric(as.character(incrementos_descuentos$`Resultado L/m`))
  incrementos_descuentos$`Resultado L/m`<-incrementos_descuentos$`Volumen / L`/(incrementos_descuentos$`A  / mm`-incrementos_descuentos$`Desde / mm`)
Resutado<-incrementos_descuentos
Resutado
}

【问题讨论】:

  • 在 Rstudio 中下载文件可能会出现问题。您是否尝试过从浏览器运行?
  • 嗨!我将应用程序上传到 www.shinyapps.io 以使用浏览器,但结果是一样的。我想知道.Rmd文件中的脚本至少是错误的。
  • R 控制台或应用程序中是否出现任何错误?
  • 在控制台上。
  • 您的 Rmd 块似乎缺少反引号 (```{r} code ```),除非在此处粘贴时出现问题。

标签: r function shiny report r-markdown


【解决方案1】:

似乎我找到了解决办法,经过很多天。

我在 app.R 中添加了一个响应式对象:

informe <- reactive({
    infile<-input$file1
    if(is.null(infile))return(NULL)
    calculo(infile$datapath)
  })

我从 report.Rmd 中将其称为 informe()

【讨论】:

    猜你喜欢
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    • 2021-01-18
    • 1970-01-01
    • 2017-05-11
    相关资源
    最近更新 更多