【发布时间】:2020-06-15 09:34:38
【问题描述】:
这是我希望最终以 PDF 格式存储的代码示例。
library(shiny)
library(arsenal)
library(knitr)
library(survival)
# 创建带有列和标识符的随机数据集 样品
random <- data.frame(replicate(10,sample(5:100, 10)))
samples <- data.frame(replicate(10,sample(0:1, 1)))
randomdf <- merge(samples,random, by= "row.names")
rm(random, samples)
我删除了带有行名的列以获得更好的表格示例
randomdf$Row.names <- NULL
用 Shiny 创建表
shinyApp(
ui = fluidPage(tableOutput("table")), # creating a page with fluidpage
server = function(input, output) {
output$table <- renderTable({
as.data.frame(summary(tableby(randomdf$replicate.10..sample.0.1..1..~.,
data = randomdf,
numeric.stats=c("medianq1q3"),
numeric.test="kwt"),
text = "html",
digits = 2))
},striped=TRUE, sanitize.text.function = function(x) x)
}
)
我找到了下面应该生成 PDF 的代码,但我不确定如何使用之前的代码来实现它。
shinyApp(
ui <- fluidPage(tableOutput("table"),
downloadButton("report","Generate report")),
server <-function(input, output) {
rendertable to indicate that
inputs change
output$table <- renderTable({
as.data.frame(summary(tableby(randomdf$replicate.10..sample.0.1..1..~.,
data = randomdf,
numeric.stats=c("medianq1q3"),
numeric.test="kwt"),
text = "html",
digits = 2))
},striped=TRUE, sanitize.text.function = function(x) x)
# 这里是我现在添加新代码的地方
output$report <- downloadHandler(
filename = "report.pdf",
content = function(file) {
tempReport <- file.path(tempdir(), "report.Rmd")
file.copy("report.Rmd", tempReport, overwrite = TRUE)
params = list(n= input$table)
rmarkdown::render(tempReport, output_file = file,
params = params,
envir = new.env(parent = globalenv()))
}
)
}
)
现在,我的 html 中确实有“生成报告”按钮,但由于以下错误,我无法生成报告:
normalizePath 中的警告(路径,winslash = winslash,mustWork = 必须工作):
路径[1]="/var/folders/4g/ld58jxf94s74vrcf_1gd07bh0000gn/T//RtmphCZeLD/report.Rmd": 没有这样的文件或目录警告:abs_path 中的错误:文件 '/var/folders/4g/ld58jxf94s74vrcf_1gd07bh0000gn/T//RtmphCZeLD/report.Rmd' 不存在。 [没有可用的堆栈跟踪]
【问题讨论】:
-
是的,但是我很难在那里实现我的代码。如果你能指出我正确的方向,我可以再试一次
标签: r shiny pdf-generation