我使用Shiny和knitr的组合来创建PDF文档。通过Shiny将图片上传到knitr文档
目前我想添加功能,允许用户上传将被放置在创建的文件中的图片。然而,我真的被卡住了,因为我无法获得输入图片的路径。任何人都可以帮助我吗?
简单的例子:
应用:
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'文件中用'paste0'替换'paste',否则会在文件名后找不到尾部空格并且找不到图片。 – NicE