Shiny to Markdown PDF在第二次及以后的运行中均失败

时间:2018-09-12 19:34:23

标签: r shiny r-markdown

我一直在尝试遵循本教程:generating reports

我已经能够获得建议的代码以成功运行,并且可以毫无问题地生成PDF。当我尝试使用自己的.Rmd文件运行Shiny应用程序时,第一次运行良好,并在需要的地方生成PDF。在第二次及以后的运行中,文件生成失败,并显示! LaTeX Error: Unknown float option 'H'.

我尝试在markdown文件的YAML标头中提到{float}包,但没有任何改进。唯一的成功似乎是删除了所有r代码块,这使我的文档显得很稀疏。

这是 app.R

shinyApp(
  ui = fluidPage(
    sliderInput("slider", "Slider", 1, 100, 50),
    dateInput("dateinput", "Select a date"),
    downloadButton("report.pdf", "Generate report")
  ),
  server = function(input, output) {
    output$report.pdf <- downloadHandler(
      filename = "report.pdf",
      content = function(file) {
        tempReport <- file.path(tempdir(), "deviants.Rmd")
        file.copy("deviants.Rmd", tempReport, overwrite = TRUE)

        params <- list(n = input$slider, d = input$dateinput)
        print(class(params$n))
        print(class(params$d))
        print(params)

        rmarkdown::render(tempReport, output_file = file,
                          params = params,
                          envir = new.env(parent = globalenv())
        )
      }
    )
  }
)

deviants.Rmd

---
output: pdf_document
always_allow_html: yes
params:
  n: NA
  d: NA
header-includes:
- \usepackage{booktabs}
- \usepackage{sectsty} \subsectionfont{\centering}
- \usepackage{sectsty} \sectionfont{\centering}
- \usepackage{float{
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(grid)
library(png)
library(dplyr)
```

```{r echo=FALSE, results='asis'}
cat(params$n)
cat(params$d)
```

```{r echo=FALSE}
sampleMetrics <- data.frame(
  Sample = c("Threshold","A","2","4","11","C","DEF"),
  Length = c(">= 6", 10, 11, 11, 11, 11, 12),
  Experiment1 = c(">= 10000",5696,8006,6675,9477,5028,7093),
  Experiment2 = c(">= 10000", 21223, 27890, 34623, 24152, 25716, 45187),
  Sum = c(">=20000", 28409, 41895, 46181, 34129, 12244, 51910),
  Total1 = c("N/A", 41382, 132670, 78271, 89930, 98788, 13015),
  Total2 = c("N/A", 43170, 53280, 57568, 46584, 51156, 55045),
  stringsAsFactors = FALSE
)

super_cell_spec <- function(data, threshold) {
  cell_spec(as.numeric(data), "latex", background = ifelse(
    as.numeric(data) >= threshold, "#45ff41", "#ff4242"))
}
sampleMetrics$Length[-1] <- super_cell_spec(sampleMetrics$Length[-1], 6)
sampleMetrics$Experiment1[-1] <- super_cell_spec(
   sampleMetrics$Experiment1[-1], 10000)
sampleMetrics$Experiment2[-1] <- super_cell_spec(
   sampleMetrics$Experiment2[-1], 10000)
sampleMetrics$Sum[-1] <- super_cell_spec(sampleMetrics$Sum[-1], 20000)

sampleMetrics[1,] <- cell_spec(sampleMetrics[1,], "latex", 
   background = "#afafaf")

sampleMetrics%>%
  kable("latex", booktabs = F, escape = F, 
        col.names = linebreak(c("Sample",
          "Length", "Experiment 1", 
          "Experiment 2","Sum",
          "Total\n1", "Total\n2"), 
          align = "c")) %>%
  kable_styling(latex_options = "scale_down")
```

如果第一次不能完美运行,我绝对不会感到困惑。也许与 app.R 中的envir = new.env(parent = globalenv())有关?感谢您的任何想法。

0 个答案:

没有答案