【发布时间】:2016-08-20 04:07:14
【问题描述】:
我创建了 R 代码,允许我转换和分析数据,然后将结果以表格的形式输出到 PDF 报告中。最近,我决定与不熟悉 R 的大学分享我的工作。因此,我想创建一个 Shiny 应用程序,让他们无需安装 R 就可以使用我的工作。 Shiny 应用程序将用作上传数据然后下载报告的平台。不幸的是,我无法将结果输出为 PDF。
我的 Shiny 代码如下所示:
library(knitr)
ui <- fluidPage(
fluidRow(
column(3,
wellPanel(
fileInput(inputId = "files",
label = "Choose csv files",
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv'),
multiple = TRUE),
textInput('filename', 'File name', value = ''),
downloadButton('report', label = 'Download PDF')
)
)
)
)
server <- function(input, output) {
data_set <- reactive({if(!is.null(input$files)) {
max_table = length(input$files[,1])
lst <- list()
for(i in 1:length(input$files[,1])){
lst[[i]] <- read.csv(input$files[[i, 'datapath']], sep = ",", header = TRUE, skip = 4, dec = ".")
}
lst <- lapply(lst, function(x) xtable(x))}})
output$report = downloadHandler(
filename = reactive({paste0(input$filename,'.pdf')}),
content = function(file) {
out = knit2pdf('pdf_shell.Rnw', clean = TRUE)
file.rename(out, file) # move pdf to file for downloading
},
contentType = 'application/pdf'
)
}
shinyApp(ui = ui, server = server)
我的.Rnw 文件如下所示:
\documentclass{article}
\usepackage{tabularx}
\begin{document}
\begin{table}[H]
\begin{tabularx}
\textbf{Test Name:}& \Sexpr{input$filename} \\
\textbf{Date:}& \\
\end{tabularx}
\end{table}
<<echo = FALSE , message = F, >>=
data_set
@
\end{document}
显然加载data_set 不起作用,我不知道如何继续。
现在我只想输出由我加载到 Shiny 中的数据创建的表。
非常感谢您的帮助。
旁注: csv 文件是外部简单的数据帧,由填充数据的标题和列组成。
当在 R 中 c 输出时,它们应该看起来像这样:
$data001
A B C D E
X 10 30 50 70
Y 20 40 60 80
【问题讨论】:
-
可能是因为在闪亮的 data_set -- reactive 中,所以尝试在 .Rnw 和
content = function(file) { data_set_1=data_set() out = knit2pdf('pdf_shell.Rnw', clean = TRUE) file.rename(out, file) # move pdf to file for downloading }中使用 data_set_1 -
@Batanichek 非常感谢您的回复,它有效,我能够将值输出到 PDF 中。我必须提高我对闪亮的理解,