【发布时间】:2018-09-12 19:34:22
【问题描述】:
我一直在努力学习本教程: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()) 有关?感谢您的任何想法。
【问题讨论】:
-
删除创建的文件并在同一个会话中再次运行程序,让我知道会发生什么。
-
你是如何尝试加载
float.sty的?可以直接处理Rmd吗?请尽量减少您的示例,即删除产生错误所需的所有内容。 -
@Chabo 似乎没有帮助,不过是个好主意。 @RalfStubner 现在编辑,可以缩小到最后一个代码块。编辑 YAML 以加载浮动,并且文件绝对可以正常编织。收到错误
! Undefined control sequence. l.124 \multirow加载浮动包。 -
在进行了一些挖掘之后,加载包时似乎发生了很多错误。我不得不加载像 multirow 和 float 这样的乳胶包来压缩这些包,但我仍然收到
! Undefined control sequence错误。 kable 和 kable_extra 不加载这些吗?
标签: r shiny r-markdown