在我的 Shiny 应用程序中,用户可以构建可自定义的报告 - 好吧,这就是要实现的目的......
现在用户必须选择所有元素(摘要、直方图、附加注释)以保持简单。然后,输入小部件中的所有内容都会转移到报告创建中(请参阅 app.R 中的
downloadhandler()
)。
报告可以是 html 或 pdf。两种变体都在不同阶段失败。
但是现在让我们看一下代码并进行澄清:您可以复制代码并运行它。唯一需要更改的是两个文件中的 html 或 pdf(app.R 和 report.Rmd,请参阅注释)。
app.R
# the packages I'm using (hopefully a complete list)
#library(pandoc)
#library(tinytex)
#library(bookdown)
#library(markdown)
#library(rmarkdown)
#library(knitr)
#pandoc_activate()
ui <- fluidPage(
fluidRow(
# Inputarea
# Choose which elements should be added to the Report
column(5,
br(),
checkboxGroupInput(
inputId = "show_elements",
label = "Choose elements to be in report",
choices = c("Abstract","Histogram","Additional Comments"),
selected = c("Abstract","Histogram","Additional Comments")
#inline = FALSE,
#width = NULL,
#choiceNames = NULL,
#choiceValues = NULL
),
br(),
br(),
hr(),
br(),
# if Abstract is checked, then it is shown in the UI
conditionalPanel(
condition = "input.show_elements.includes('Abstract')",
textAreaInput(inputId="caption", label=NULL, value ="Abstract data", width = "1000px", height="100px")
),
br(),
# if Histogram is checked, then it is shown in the UI
conditionalPanel(
condition = "input.show_elements.includes('Histogram')",
sliderInput(inputId = "bins",
label = "Number of bins:",
min = 1,
max = 50,
value = 30)
),
# if Additional Comments is checked, then it is shown in the UI
conditionalPanel(
condition = "input.show_elements.includes('Additional Comments')",
textAreaInput(inputId="caption_1", label=NULL, value ="further notes for the report", width = "1000px", height="100px")
)
),
# Outputarea
column(7,
br(),
# if Abstract is checked, then it is shown in the UI
conditionalPanel(
condition = "input.show_elements.includes('Abstract')",
verbatimTextOutput("abstract")
),
br(),
# if Histogram is checked, then it is shown in the UI
conditionalPanel(
condition = "input.show_elements.includes('Histogram')",
plotOutput(outputId = "distPlot")
),
br(),
# if Additional Comments is checked, then it is shown in the UI
conditionalPanel(
condition = "input.show_elements.includes('Additional Comments')",
verbatimTextOutput("add_com")
),
br(),
# The download button
downloadButton("reportGeneration", "Download Report")
)
)
)
server <- function(input, output) {
# Output corresponding to "Abstract"
output$abstract <- renderText({ input$caption })
# Output corresponding to "Histogram"
output$distPlot <- renderPlot({
x <- faithful$waiting
bins <- seq(min(x), max(x), length.out = input$bins + 1)
hist(x, breaks = bins, col = "#007bc2", border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times")
})
# Output corresponding to "Additional Comments"
output$add_com <- renderText({ input$caption_1 })
# Download the document (html or pdf)
output$reportGeneration <- downloadHandler(
filename = "report.html", # if necessary change to report.pdf (app.R and report.Rmd must have either html or pdf)
content = function(file) {
tempReport<-file.path("C:/Users/tueftla/R","report.Rmd")
file.copy('report.Rmd', tempReport, overwrite = TRUE)
params = list(text1=input$caption, value1=input$bins, text2=input$caption_1)
rmarkdown::render(
input =tempReport,
output_file = file,
params = params,
envir = new.env(parent = globalenv())
)
}
)
}
shinyApp(ui, server)
报告。Rmd
<!-- YAML header with the three parameters -->
---
title: "My R Markdown Document"
author: "tueftla"
date: "`r format(Sys.time(), '%d.%m.%Y')`"
output: html_document <!-- Depending on the app.R it could be also pdf_document -->
params:
text1: NA
text2: NA
value1: NA
---
```{r include=FALSE}
library(knitr)
```
## Abstract <!-- Here the first parameter text1 should be used -->
```{r, results='asis',echo=FALSE,warning=FALSE}
print(params[["text1"]])
```
## Histogram <!-- Here the third parameter value1 should be used -->
```{r results='asis',echo=FALSE,warning=FALSE}
x <- faithful$waiting
bins <- seq(min(x), max(x), length.out = params[["value1"]] + 1)
hist(x, breaks = bins, col = "#007bc2", border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times")
```
## Additional Comments <!-- Here the second parameter text2 should be used -->
```{r, results='asis',echo=FALSE,warning=FALSE}
print(params[["text2"]])
```
当我使用
runApp()
和 html 输出(在两个文件中)启动 Shiny 应用程序时,控制台输出以下行:
Listening on http://127.0.0.1:3347
processing file: report.Rmd
output file: report.knit.md
"C:/Users/tueftla/AppData/Local/r-pandoc/r-pandoc/3.1.12.2/pandoc" +RTS -K512m -RTS report.knit.md --to html4 --from markdown+autolink_bare_uris+tex_math_single_backslash --output pandoc50a034b324e1.html --lua-filter "C:\Users\tueftla\AppData\Local\R\win-library\4.3\rmarkdown\rmarkdown\lua\pagebreak.lua" --lua-filter "C:\Users\tueftla\AppData\Local\R\win-library\4.3\rmarkdown\rmarkdown\lua\latex-div.lua" --embed-resources --standalone --variable bs3=TRUE --section-divs --template "C:\Users\tueftla\AppData\Local\R\win-library\4.3\rmarkdown\rmd\h\default.html" --no-highlight --variable highlightjs=1 --variable theme=bootstrap --mathjax --variable "mathjax-url=https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" --include-in-header "C:\Users\tueftla\AppData\Local\Temp\Rtmp04VMVZ\rmarkdown-str50a01a17b14.html"
Output created: C:\Users\tueftla\AppData\Local\Temp\Rtmp04VMVZ\file50a062984a73.html
它似乎以某种方式工作,因为它说它已创建... 但是 html 文件不存在。文件夹Rtmp04VMVZ存在并且有一些文件...
问题1:我根据控制台创建的html文件在哪里?!是否可以直接下载它,或者甚至更好地指定保存 html 的文件夹?
现在让我们使用
pdf 输出(在两个文件中)执行
runApp()
并查看控制台输出:
Listening on http://127.0.0.1:3347
processing file: report.Rmd
output file: report.knit.md
"C:/Users/tueftla/AppData/Local/r-pandoc/r-pandoc/3.1.12.2/pandoc" +RTS -K512m -RTS report.knit.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output pandoc50a0326f48e.tex --lua-filter "C:\Users\tueftla\AppData\Local\R\win-library\4.3\rmarkdown\rmarkdown\lua\pagebreak.lua" --lua-filter "C:\Users\tueftla\AppData\Local\R\win-library\4.3\rmarkdown\rmarkdown\lua\latex-div.lua" --embed-resources --standalone --highlight-style tango --pdf-engine pdflatex --variable graphics --variable "geometry:margin=1in"
Warning in system2(..., stdout = if (use_file_stdout()) f1 else FALSE, stderr = f2)
'"pdflatex"' not found
Warning: Error in : LaTeX failed to compile C:\Users\tueftla\AppData\Local\Temp\Rtmp04VMVZ\file50a026d52af1.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips.
1: runApp
对我来说,它似乎因为缺少 pdflatex 而失败?!调试提示没有改变任何东西(从我的角度来看)...
问题2:这里有什么问题以及如何解决?就像我在第一个问题中提到的那样,是否可以只下载 pdf,或者甚至更好地指定应保存 pdf 的文件夹?
非常感谢您的帮助!
EDIT_1:我稍微更改了
report.Rmd
文件。在代码块中,我仅使用 params
... 的值
<!-- YAML header with the three parameters -->
---
title: "My R Markdown Document"
author: "tueftla"
date: "`r format(Sys.time(), '%d.%m.%Y')`"
output: html_document <!-- Depending on the app.R it could be also pdf_document -->
params:
text1: NA
text2: NA
value1: NA
---
```{r include=FALSE}
library(knitr)
```
## Abstract <!-- Here the first parameter text1 should be used -->
```{r, results='asis',echo=FALSE,warning=FALSE}
print("Abstract") #print(params[["text1"]])
```
## Histogram <!-- Here the third parameter value1 should be used -->
```{r results='asis',echo=FALSE,warning=FALSE}
x <- faithful$waiting
bins <- seq(min(x), max(x), length.out = 5 + 1) #bins <- seq(min(x), max(x), length.out = params[["value1"]] + 1)
hist(x, breaks = bins, col = "#007bc2", border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times")
```
## Additional Comments <!-- Here the second parameter text2 should be used -->
```{r, results='asis',echo=FALSE,warning=FALSE}
print("Addidtional comments"]) #print(params[["text2"]])
```
...运行时
runApp()
渲染也失败,我收到这些错误消息
Listening on http://127.0.0.1:4084
processing file: report.Rmd
output file: report.knit.md
"C:/Users/tueftla/AppData/Local/r-pandoc/r-pandoc/3.1.12.2/pandoc" +RTS -K512m -RTS report.knit.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output pandoc7fcc19ef2230.tex --lua-filter "C:\Users\tueftla\AppData\Local\R\win-library\4.3\rmarkdown\rmarkdown\lua\pagebreak.lua" --lua-filter "C:\Users\tueftla\AppData\Local\R\win-library\4.3\rmarkdown\rmarkdown\lua\latex-div.lua" --embed-resources --standalone --highlight-style tango --pdf-engine pdflatex --variable graphics --variable "geometry:margin=1in"
! Sorry, but C:\Users\tueftla\AppData\Local\Programs\MiKTeX\miktex\bin\x64\pdflatex.exe failed.
! The log file contains hopefully all information, to make MiKTeX run again
! C:\Users\tueftla\AppData\Local\MiKTeX\miktex\log\pdflatex.log
Warning: Error in : LaTeX failed to compile C:\Users\tueftla\AppData\Local\Temp\Rtmp2rsGxa\file7fcc51044777.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See file7fcc51044777.log for more info.
1: runApp
日志文件(
C:\Users\tueftla\AppData\Local\MiKTeX\miktex\log\pdflatex.log
)
2024-03-11 16:51:35,134+0100 INFO pdflatex - this process (24692) started by Rgui in directory C:\Users\tueftla\R with command line: C:\Users\tueftla\AppData\Local\Programs\MiKTeX\miktex\bin\x64\pdflatex.exe -halt-on-error -interaction=batchmode C:\Users\tueftla\AppData\Local\Temp\Rtmp2rsGxa\file7fcc51044777.tex
2024-03-11 16:51:35,134+0100 INFO pdflatex - running on Windows 10.0.19045
2024-03-11 16:51:35,139+0100 INFO pdflatex - this is MiKTeX-PDFTEX 4.19.0 (1.40.26) (MiKTeX 24.3)
2024-03-11 16:51:35,170+0100 INFO pdflatex - allowing known shell commands
2024-03-11 16:51:35,229+0100 INFO pdflatex - going to create file: pdflatex.fmt
2024-03-11 16:51:35,229+0100 INFO pdflatex.core - start process: miktex formats build pdflatex --engine pdftex
2024-03-11 16:51:51,269+0100 FATAL pdflatex.core - GUI framework cannot be initialized.
2024-03-11 16:51:51,269+0100 FATAL pdflatex.core - Data:
2024-03-11 16:51:51,269+0100 FATAL pdflatex.core - Source: Libraries\MiKTeX\UI\Qt\mikuiqt.cpp:126
2024-03-11 16:51:51,273+0100 FATAL pdflatex - GUI framework cannot be initialized.
2024-03-11 16:51:51,273+0100 FATAL pdflatex - Info:
2024-03-11 16:51:51,273+0100 FATAL pdflatex - Source: Libraries\MiKTeX\UI\Qt\mikuiqt.cpp
2024-03-11 16:51:51,273+0100 FATAL pdflatex - Line: 126
2024-03-11 16:51:51,276+0100 INFO pdflatex - this process (24692) finishes with exit code 1
2024-03-11 16:51:51,283+0100 WARN pdflatex.core - still open: C:/Users/tueftla/AppData/Local/Temp/Rtmp2rsGxa/file7fcc51044777.tex
2024-03-11 16:51:51,283+0100 WARN pdflatex.core - still open: file7fcc51044777.log
使用
<!-- ... -->
作为注释仅适用于一个地方:R Markdown 文档的 content 中。在您的 R markdown 文档中,您在 YAML 标头之前、YAML 标头内部以及 R 代码块内部都有类似的注释,但这些注释都无法正确解析或运行。
使用上述更改来调整您的代码。 (
app.R
不变。)
---
title: "My R Markdown Document"
author: "tueftla"
date: "`r format(Sys.time(), '%d.%m.%Y')`"
output: html_document
params:
text1: NA
text2: NA
value1: NA
---
```{r include=FALSE}
library(knitr)
```
## Abstract <!-- Here the first parameter text1 should be used -->
```{r, results='asis',echo=FALSE,warning=FALSE}
print(params[["text1"]])
```
## Histogram <!-- Here the third parameter value1 should be used -->
```{r results='asis',echo=FALSE,warning=FALSE}
x <- faithful$waiting
bins <- seq(min(x), max(x), length.out = params[["value1"]] + 1)
hist(x, breaks = bins, col = "#007bc2", border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times")
```
## Additional Comments <!-- Here the second parameter text2 should be used -->
```{r, results='asis',echo=FALSE,warning=FALSE}
print(params[["text2"]])
```
这样,我可以渲染 HTML,并在更改
output_format=
和文件扩展名后渲染 PDF,两者都没有错误。