R shiny 中的下载处理程序不生成 PDF 文件(使用 rmarkdown::render())

Download handler in R shiny does not produce a PDF file (using rmarkdown::render())

这是我闪亮的应用程序的一个非常崩溃的版本......按下“开始!”按钮你会得到两个图。我想根据这个问题的最佳答案将它们下载到 PDF unsing rmarkdown::render():

当按下下载按钮时,下载 window 打开,但文件名为“报告”而不是“结果”,文件类型为“所有文件”。按保存时没有生成文件,或者至少 none 我可以在任何地方找到。

我使用与 R 帮助网站略有不同的解决方案遇到了完全相同的问题,但我无法弄清楚是什么原因以及如何解决它。

这个问题让我觉得这可能只是一个问题 运行 RStudio 中的代码:Download handler does not save file shiny R 然而,当尝试从浏览器下载时,它说“report.html 无法下载”- 所以仍然是错误的名称、错误的文件类型并且没有成功下载。

谁能帮我解决这个问题?

#LIBRARIES
library (shiny)
library(shinydashboard)
library (shinyjs)
library (ggplot2)
library (dplyr)
library(rmarkdown)
library (knitr)



blues <- c( "#013560", "#0065AD", "#007BD3", "#0091F9", "#9FD4F9",  "#EEEEEE")



#sidebar vordefinieren
Codepan <-   div( 
  id = 'sidebar_cr',
  actionButton (inputId = "Button", label = "Go!"),
                  "some intro text and then the download button",
                                   downloadButton("report", "Meine Ergebnisse als PDF speichern")
                                   )
                  



sidebar <- 
  dashboardSidebar(Codepan)


#Body vordefinieren

body <- dashboardBody(
  
          
          
          
          fluidRow(
            
            box(title = "Deine Mediennutzung",
                status= "success", solidHeader = TRUE, height=400,width = 11,
               plotOutput(outputId= "PlotM", height= 300))
          
          
        ),
      
      
      
          fluidRow(
            box(
              width = 11, title = "Deine Ergebnisse",  solidHeader = TRUE, status = "success",
              column(width= 6, plotOutput("plotEAT", height = 250))
            ))
)
             



# hier beginnt die eigentliche App:
ui <- 
  dashboardPage(
    dashboardHeader(title = "title", disable = FALSE),
    sidebar,
    body
  )



server <- function(input, output) {
  

  
  #2 Plot comparison Feedback
 
  
  
  MediaCompare3 <- eventReactive(input$Button, {
    det <-data.frame(group = c("a-du", "a-ges", "b-sport", "c-age"),
                     means = c(16, 22, 31, 15)
                     )
    
    
    
    
    
  })
  
  output$PlotM <- renderPlot({
    ggplot(MediaCompare3(), aes(x = group, y = means)) + 
      geom_bar(stat = "identity", fill = "#013560", color = "#013560") +
     # scale_x_discrete(labels=c("Du", "Gesamtdurchschnitt",paste("Durchschnitt",Daten[toupper(input$Code), "Sportart_zurueckkodiert"]), paste("Durchschnitt", (Daten[toupper(input$Code), "SP01_01"]-1),"-",(Daten[toupper(input$Code), "SP01_01"]+1), "Jahre" )))+ 
      xlab(NULL) + 
      ylab("Mediennutzung in Minuten")+
      #geom_hline(yintercept = as.numeric(as.character(Daten[toupper(input$Code), "Nutzungsdauer_DM_Gesamt"])), lty = 8, col = "Red")+
      geom_text(aes(label = round(means, 0)), vjust =2, colour = "white", size= 8)#+
      #geom_label(label="Du", x = 0.5,y = as.numeric(as.character(Daten[toupper(input$Code), "Nutzungsdauer_DM_Gesamt"])), color ="red")
  })
  
  
  
  
  
  
  
 
  
  forplotEAT <- eventReactive (input$Button, {
    df<- data.frame(Komp = rep(c("Einstellungen zu Essen", "Sozialer Vergleich"), 3), 
                    groupW = c("ADu", "ADu", 
                               "AGesamt", "AGesamt",
                               "ZGeschlecht","ZGeschlecht"),
                    valuesW = c (19, 20, 21, 34, 12, 17
                                 
                                 
                    )) 
    
    
    
  })
  
  
  output$plotEAT <-renderPlot ({
    Geschlecht <- "girls"
    ggplot(forplotEAT(), aes(x = Komp, y = valuesW, fill = groupW)) + 
      geom_bar(position = "dodge", stat = "identity",color = "#404040", show.legend = TRUE)+
      #scale_fill_discrete(name = "Vergleichsgruppen", labels=c("Du", "Gesamtdurchschnitt", Geschlecht))+
      xlab("Gruppe")+
      ylab("Ergebnis")+
      geom_text(aes(label = round(valuesW, 1)), vjust =2, colour = "white", size= 5, position = position_dodge(width= 0.9))+
      scale_fill_manual(values= blues, name = "Vergleichsgruppen", labels=c("Du", "Gesamtdurchschnitt", Geschlecht))#+
    #coord_flip()+
    #geom_hline(yintercept = 10, lty = 8, col = "Red")
  })

当前版本依赖于下面的 Rmd 文件:

  
  output$report<-
    
    downloadHandler(
      "Result.pdf",
      content = 
        function(file){
          rmarkdwon::render(
            input= "report_file.Rmd",
            output_file = "built_report.pdf",
            params = list (plotM = plotM(),
                           plotEAT= plotEAT())
          )
          readBin (con = "built_report.pdf",
                   what = "raw",
                   n = file.info ("built_report.pdf")[, "size"])%>%
            writeBin (con = file)
        } )
  
  
  
  
  
  
  
  
  
}

shinyApp(ui=ui, server=server)

RMD 文件:

---
title: "Individuelles Ergebnis"
output: pdf_document
params:
  plotM: "NULL"
  plotEAT: "NULL"


---




```{r}
params[["plotM"]]```

```{r}
params[["plotEAT"]]```



这是我试过的另一个版本,直接尝试将正文传递给下载处理程序:

  
  output$report<-
    
    downloadHandler(
      "Ergebnisse.pdf",
      content = 
        function(file){
          rmarkdwon::render(
            input= "report_file.Rmd",
            output_file = "built_report.pdf",
            params = list (plotM = plotM(),
                           plotEAT= plotEAT())
          )
          readBin (con = "built_report.pdf",
                   what = "raw",
                   n = file.info ("built_report.pdf")[, "size"])%>%
            writeBin (con = file)
         } )
        

如果您有 latex installed:

,这里有一个可行的解决方案
#LIBRARIES
library (shiny)
library(shinydashboard)
library (ggplot2)
library (dplyr)



blues <- c( "#013560", "#0065AD", "#007BD3", "#0091F9", "#9FD4F9",  "#EEEEEE")



#sidebar vordefinieren
Codepan <-   div( 
  id = 'sidebar_cr',
  actionButton (inputId = "Button", label = "Go!"),
  "some intro text and then the download button",
  downloadButton("report", "Meine Ergebnisse als PDF speichern")
)




sidebar <- 
  dashboardSidebar(Codepan)


#Body vordefinieren

body <- dashboardBody(
  
  
  
  
  fluidRow(
    
    box(title = "Deine Mediennutzung",
        status= "success", solidHeader = TRUE, height=400,width = 11,
        plotOutput(outputId= "PlotM", height= 300))
    
    
  ),
  
  
  
  fluidRow(
    box(
      width = 11, title = "Deine Ergebnisse",  solidHeader = TRUE, status = "success",
      column(width= 6, plotOutput("plotEAT", height = 250))
    ))
)




# hier beginnt die eigentliche App:
ui <- 
  dashboardPage(
    dashboardHeader(title = "title", disable = FALSE),
    sidebar,
    body
  )



server <- function(input, output) {
  
  
  
  #2 Plot comparison Feedback
  
  
  
  MediaCompare3 <- eventReactive(input$Button, {
    det <-data.frame(group = c("a-du", "a-ges", "b-sport", "c-age"),
                     means = c(16, 22, 31, 15)
    )
    
    
    
    
    
  })
  
  plotM <- reactive({
    ggplot(MediaCompare3(), aes(x = group, y = means)) + 
      geom_bar(stat = "identity", fill = "#013560", color = "#013560") +
      # scale_x_discrete(labels=c("Du", "Gesamtdurchschnitt",paste("Durchschnitt",Daten[toupper(input$Code), "Sportart_zurueckkodiert"]), paste("Durchschnitt", (Daten[toupper(input$Code), "SP01_01"]-1),"-",(Daten[toupper(input$Code), "SP01_01"]+1), "Jahre" )))+ 
      xlab(NULL) + 
      ylab("Mediennutzung in Minuten")+
      #geom_hline(yintercept = as.numeric(as.character(Daten[toupper(input$Code), "Nutzungsdauer_DM_Gesamt"])), lty = 8, col = "Red")+
      geom_text(aes(label = round(means, 0)), vjust =2, colour = "white", size= 8)#+
    #geom_label(label="Du", x = 0.5,y = as.numeric(as.character(Daten[toupper(input$Code), "Nutzungsdauer_DM_Gesamt"])), color ="red")
  })
  
  output$PlotM <- renderPlot({
    plotM()
  })
  
  
  
  
  
  
  
  
  forplotEAT <- eventReactive (input$Button, {
    df<- data.frame(Komp = rep(c("Einstellungen zu Essen", "Sozialer Vergleich"), 3), 
                    groupW = c("ADu", "ADu", 
                               "AGesamt", "AGesamt",
                               "ZGeschlecht","ZGeschlecht"),
                    valuesW = c (19, 20, 21, 34, 12, 17
                                 
                                 
                    )) 
    
    
    
  })
  
  plotEAT <- reactive({
    Geschlecht <- "girls"
    ggplot(forplotEAT(), aes(x = Komp, y = valuesW, fill = groupW)) + 
      geom_bar(position = "dodge", stat = "identity",color = "#404040", show.legend = TRUE)+
      #scale_fill_discrete(name = "Vergleichsgruppen", labels=c("Du", "Gesamtdurchschnitt", Geschlecht))+
      xlab("Gruppe")+
      ylab("Ergebnis")+
      geom_text(aes(label = round(valuesW, 1)), vjust =2, colour = "white", size= 5, position = position_dodge(width= 0.9))+
      scale_fill_manual(values= blues, name = "Vergleichsgruppen", labels=c("Du", "Gesamtdurchschnitt", Geschlecht))#+
    #coord_flip()+
    #geom_hline(yintercept = 10, lty = 8, col = "Red")
  })
  
  output$plotEAT <-renderPlot ({
    plotEAT()
  })
  
  output$report<-
    
    downloadHandler(
      "Result.pdf",
      content = 
        function(file){
          rmarkdown::render(
            input= "report_file.Rmd",
            output_file = "built_report.pdf",
            params = list (plotM = plotM(),
                           plotEAT= plotEAT())
          )
          readBin (con = "built_report.pdf",
                   what = "raw",
                   n = file.info ("built_report.pdf")[, "size"])%>%
            writeBin (con = file)
        } )
  
  
  
  
  
  
  
  
  
}

shinyApp(ui=ui, server=server)