【发布时间】:2016-07-14 10:31:40
【问题描述】:
我正在使用 Shiny 和 knitr 的组合来创建 PDF 文档。
目前我想添加允许用户上传将放置在创建的文档中的图片的功能。但是,我真的卡住了,因为我无法获取输入图片的路径。有人可以帮我吗?
简单示例:
应用:
library(knitr)
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("picture", label = 'Picture'),
downloadButton('report', label = 'Download PDF')
),
mainPanel()
)
)
server <- function(input,output){
picture <- reactive({
input$picture[,4]
})
output$report = downloadHandler(
filename = "test.pdf",
content = function(file){
picture = picture()
out = knit2pdf(input = 'test.Rnw', compiler = 'xelatex', clean = TRUE)
file.rename(out, file)
},
contentType = 'application/pdf'
)
}
shinyApp(ui = ui, server = server)
还有.Rnw 文档:
\documentclass{article}
\begin{document}
Put picture here:
<<echo = FALSE , message = F, results='asis'>>=
cat(paste('\\includegraphics[height=3in]{', picture,'}'))
@
\end{document}
部分'\includegraphics[height=3in]{', picture,'}显然是造成问题的原因,因为我不知道图片路径只是临时的一个。
【问题讨论】:
-
上传文件路径为
input$picture$datapath -
是的,但它是临时目录的路径,我不知道如何使它工作。此外,$datapath 与我的代码中的 [,4] 相同。
-
尝试在你的
.Rnw文件中将paste替换为paste0,否则文件名后面有一个尾随空格,找不到图片。 -
我仍然收到错误:
running 'texi2dvi' on 'test.tex' failed LaTeX errors: C:/Users/meme/Desktop/test.tex:55: LaTeX Error: File C:/ea9c33bfd57973b840657c82/0' not found.