R 使用用户输入文件生成PDF报告-闪亮
我已经创建了R代码,它允许我转换和分析数据,然后将结果以表格的形式输出到PDF报告中。最近,我决定与不熟悉R的大学分享我的工作。因此,我想创建一个闪亮的应用程序,允许他们在不安装R的情况下使用我的工作。闪亮的应用程序将作为上传数据然后下载报告的平台。不幸的是,我无法将结果输出到PDF 我的闪亮代码如下所示:R 使用用户输入文件生成PDF报告-闪亮,r,pdf,latex,shiny,knitr,R,Pdf,Latex,Shiny,Knitr,我已经创建了R代码,它允许我转换和分析数据,然后将结果以表格的形式输出到PDF报告中。最近,我决定与不熟悉R的大学分享我的工作。因此,我想创建一个闪亮的应用程序,允许他们在不安装R的情况下使用我的工作。闪亮的应用程序将作为上传数据然后下载报告的平台。不幸的是,我无法将结果输出到PDF 我的闪亮代码如下所示: library(knitr) ui <- fluidPage( fluidRow( column(3, wellPanel(
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)
显然,加载数据集
不起作用,我不知道如何前进。
现在,我只想输出从加载到数据库中的数据创建的表
我非常感谢你的帮助
旁注:csv文件是外部简单的数据框架,由标题和填充数据的列组成
在R中计算时,它们应该是这样的:
$data001
A B C D E
X 10 30 50 70
Y 20 40 60 80
问题,因为在
中有光泽
数据集
是被动的
,因此您不能像这样使用它
<<echo = FALSE , message = F, >>=
data_set
@
处理者
或者可能是简单的
data\u set()
in.Rnw
可能是因为在闪亮的data\u set-反应,所以尝试使用data\u set\u 1 in.Rnw和content=function(file){data\u set\u 1=data\u set()out=knit2pdf('pdf\u shell.Rnw',clean=TRUE)文件。重命名(out,file)#将pdf移到文件下载}
@Batanichek非常感谢您的回复,它很有效,我可以将值输出到PDF中。我必须提高我对shiny的理解,这两个方面都是处理程序中的data\u set\u 1=data\u set()
,以及.Rnw中的data\u set()
工作。我真的很感谢你的帮助。
<<echo = FALSE , message = F, >>=
data_set
@
\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_1
@
\end{document}
downloadHandler(
filename = reactive({paste0(input$filename,'.pdf')}),
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
},
contentType = 'application/pdf'
)