闪亮报告:试图将输入值作为参数传递给编织.RMD文档,但失败
这个简单的闪亮应用程序具有各种可选选项(selectInput)的下拉列表,SelectInput中的每个选项都有一个.rmd文件匹配其名称。基于通过下拉列表选择的选项,我试图调用其各自的.RMD文件,在下面完成代码。
EventReactive()方法似乎获得了选定的值,并将其作为输入传递,以调用相应的.RMD文件,但是代码执行但没有生成报告。
另外,请评论EventReactive()并将所需的.RMD文件作为变量(report_name)生成带有所需内容的可下载报告。我该如何解决此问题以获得所需的功能?
app.r
library(shiny)
ui <- fluidPage(
selectInput("select_id",
"Select an option",
list(
" "=" ",
"apples"='apples',
"oranges"='oranges')
),
radioButtons('format', 'Document format', c('HTML', 'Word'),
inline = TRUE),
downloadButton('downloadReport')
)
server <- function(input, output, session) {
# download of report selectable format based on button press
output$downloadReport <- downloadHandler(
filename = function() {
paste('my-report', sep = '.', switch(
input$format, PDF = 'pdf', HTML = 'html'
))
},
# get selected option from dropdown and use the input value as the name to call the Rmd file
report_name <- eventReactive(input$select_id, {
paste(input$select_id,'Rmd', sep =".")
}),
# replacing report_name() with report_name [below line] generates report, but is not the desired outcome
# report_name <- "apples.Rmd",
content = function(file) {
cat('The selected fruit report is - - - :',report_name())
src <- normalizePath(report_name())
# temporarily switch to the temp dir, in case you do not have write
# permission to the current working directory
owd <- setwd(tempdir())
on.exit(setwd(owd))
file.copy(src, report_name(), overwrite = TRUE)
out <- rmarkdown::render(report_name(),
switch(input$format,
PDF = pdf_document(),
HTML = html_document()
))
file.rename(out, file)
}
)
}
shinyApp(ui, server)
apples.rmd
---
title: "This is a report for Apples"
output: html_document
params:
tbl: NA
include: NA
---
oranges.rmd
---
title: "This is a report for Oranges"
output: html_document
params:
tbl: NA
include: NA
---
This simple Shiny app has a dropdown of various selectable options (selectInput), each of the options in selectInput have a .Rmd file matching its name. Based on the option selected through the dropdown I'm trying to call its respective .Rmd file, complete code below.
The eventReactive() method seems to get the selected value and is passed as an input to call the respective .Rmd file, however the code executes but does not generate the report.
Alternatively, commenting out eventReactive() and calling out the required .Rmd file as a variable (report_name) generates a downloadable report with the required content. How do I go about fixing this to get the desired functionality?
app.R
library(shiny)
ui <- fluidPage(
selectInput("select_id",
"Select an option",
list(
" "=" ",
"apples"='apples',
"oranges"='oranges')
),
radioButtons('format', 'Document format', c('HTML', 'Word'),
inline = TRUE),
downloadButton('downloadReport')
)
server <- function(input, output, session) {
# download of report selectable format based on button press
output$downloadReport <- downloadHandler(
filename = function() {
paste('my-report', sep = '.', switch(
input$format, PDF = 'pdf', HTML = 'html'
))
},
# get selected option from dropdown and use the input value as the name to call the Rmd file
report_name <- eventReactive(input$select_id, {
paste(input$select_id,'Rmd', sep =".")
}),
# replacing report_name() with report_name [below line] generates report, but is not the desired outcome
# report_name <- "apples.Rmd",
content = function(file) {
cat('The selected fruit report is - - - :',report_name())
src <- normalizePath(report_name())
# temporarily switch to the temp dir, in case you do not have write
# permission to the current working directory
owd <- setwd(tempdir())
on.exit(setwd(owd))
file.copy(src, report_name(), overwrite = TRUE)
out <- rmarkdown::render(report_name(),
switch(input$format,
PDF = pdf_document(),
HTML = html_document()
))
file.rename(out, file)
}
)
}
shinyApp(ui, server)
apples.Rmd
---
title: "This is a report for Apples"
output: html_document
params:
tbl: NA
include: NA
---
oranges.Rmd
---
title: "This is a report for Oranges"
output: html_document
params:
tbl: NA
include: NA
---
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要纠正的几件事:
report_name&lt; - ...
应在下载handler
之外。输入$ select_id
是“” 选择,则可能应该不是 允许下载。为此,我们
req
uire是其他的。pdf_document
等。(
::
-Reference在尝试渲染报告名称之前的存在,以抢占与路径相关的问题,如果用户可能看不到相关的错误消息。 (是否不是,取决于您正确处理路径的自信心。)
您的模板包括
参数:
,但您没有在Render(。)。我假设此处省略了它,但请确保您将
params = ...
添加到渲染调用中。您的
输入$ select_id
优惠<代码>“ html” 和“ word”
,但是您的switch> switch
语句正在寻找PDF
和html
。当选择HTML时,上述有效,但是您需要更新selectInput
和switch> switch
语句以确保正确引用所有候选人。A few things to correct:
report_name <- ...
should be outside of thedownloadHandler
.input$select_id
is the" "
choice. For this, wereq
uire it to be something else.::
-referencerender
but notpdf_document
, etc.Follow-on notes:
You might want to test for presence of the report name before trying to render it, to preempt path-related problems where the user may not see the relevant error message. (Or not, depending on how confident you are that paths are being handled correctly.)
Your templates include
params:
but you are not passing anything inrender(.)
. I'm assuming it was omitted here for brevity, but make sure you addparams=...
into the render call.Your
input$select_id
offers"HTML"
and"Word"
, yet yourswitch
statements look forPDF
andHTML
. The above works when html is selected, but you need to update yourselectInput
andswitch
statements to ensure all candidates are referenced correctly.