我有一个Shiny应用程序,用户可以在其中选择传递到参数化R降价报告的选项。然后,Rmd会提供一系列R脚本,以提取和汇总数据,为报告创建图等。
我提供的提取数据的脚本包括一个参数化的SQL查询,该查询从R markdown params
继承值(该值又从Shiny input
继承)。但是,整个过程到此为止,我收到一条错误消息,指出params
不存在。
我可以肯定地说,将输入从Shiny传递到R markdown参数是正常的-因此看来问题是将它们传递给源脚本(注意:这只是R脚本,而不是函数)。我猜想这与脚本源时正在访问的环境有关(尽管它使用的是在R标记中的上一个块中生成的数据库连接,而没有任何问题)-但除此之外,在如何纠正这一点。任何想法将不胜感激。
这里是闪亮的应用程序:
##########################################
# SHINY APP - USER INTERFACE:
ui = fluidPage (
selectInput("pathogen", "Enter pathogen of interest:", c("Campylobacter" = "Campylobacter", "Escherichia" = "Escherichia",
"Salmonella" = "Salmonella", "Shigella" = "Shigella"), selected = "Salmonella" ),
radioButtons("pkginstall", "Install required packages?", c("Yes" = "yes", "No" = "no"),selected = "yes"),
downloadButton("report", "Generate report")
)
##########################################
# SHINY APP - SERVER LOGIC:
#fileInput("download_location","Select File Location"),
server = function(input, output) {
# Create the output:
output$report = downloadHandler(
filename = paste0("Pathogen Report ", input$pathogen, "_", format(Sys.time(),"%d-%b-%Y %H.%M"), ".html"),
content = function(file) {
# Copy the .Rmd to a temporary directory:
tempReport <- file.path(tempdir(), "Pathogen_Report.Rmd")
file.copy("Pathogen_Report.Rmd", tempReport, overwrite = TRUE)
# Set up parameters to pass to Rmd document:
params <- list(pathogen = input$pathogen, pkginstall = input$pkginstall)
# Define name of report:
outname <- paste0("Pathogen Report ", input$pathogen, "_", format(Sys.time(),"%d-%b-%Y %H.%M"), ".html")
# Knit the document:
created_filename <- rmarkdown::render(input = tempReport,
output_file = outname,
params = params,
envir = new.env(parent = globalenv())
)
file.rename(created_filename, file)
}
)
}
##########################################
# SHINY APP - RUN:
# Run app:
shinyApp(ui =ui, server=server)
##################################################################
这是R markdown YAML标头:
---
params:
pathogen:
label: "Enter pathogen of interest:"
value: Shigella
input: select
choices: [Campylobacter, Escherichia, Salmonella, Shigella]
title: "Pathogen Report"
date: "`r format(Sys.time(), '%d %B %Y')`"
output:
phecharts::html_phe:
includes:
in_header: phe_logo.html
---
以及有关R脚本的相关代码块:
{r, GDW Query, echo=FALSE, cache=FALSE, message=FALSE, warning=FALSE, results='hide'}
####################################################################
# QUERY DATABASE AND EXTRACT DATA
source("Extract_data.R")
[Extract_data.R
包含一个SQL查询,其中病原体名称应替换为从R markdown参数继承的病原体名称:
# Example SQL to PostgreSQL database:
query <- "SELECT * FROM table1 WHERE table1.organism ~ '^@pathogen'"
# Substituting pathogen for pathogen name from R markdown parameters:
query <- gsub("@pathogen", params$pathogen, query)
# Executing the query:
mydata <- data.table(RPostgres::dbGetQuery(conn = dbcon, statement = query))
注意,通过在此之前的R markdown块中获取另一个脚本,已经成功建立了数据库连接。
这是我得到的错误:
Quitting from lines 84-88 (Pathogen_Report.Rmd)
Warning: Error in gsub: object 'params' not found
[No stack trace available]
[查看local
(source
)中的参数?source
。
local
TRUE,FALSE或环境,确定在何处评估已解析的表达式。 FALSE(默认)对应于用户的工作空间(全局环境),TRUE对应于调用源的环境。
直接渲染Rmd
时,params
是默认设置的值,并且您处于全局环境中。这样就可以了:
---
params:
pathogen:
label: "Enter pathogen of interest:"
value: Shigella
input: select
choices: [Campylobacter, Escherichia, Salmonella, Shigella]
pkginstall:
value: no
title: "Pathogen Report"
date: "`r format(Sys.time(), '%d %B %Y')`"
output: html_document
---
```{r GDW Query, echo=TRUE, cache=FALSE, message=FALSE, warning=FALSE, results='hide'}
####################################################################
# QUERY DATABASE AND EXTRACT DATA
source(Extract_data.R, local=FALSE) # same as source(Extract_data.R)
但是,当通过Rmd
运行Shiny App
时,您想要在Shiny
可以工作的环境中工作,并且想要像粘贴在行中一样来获取外部脚本(请参阅https://shiny.rstudio.com/articles/scoping.html) 。以下应该工作:
---
params:
pathogen:
label: "Enter pathogen of interest:"
value: Shigella
input: select
choices: [Campylobacter, Escherichia, Salmonella, Shigella]
pkginstall:
value: no
title: "Pathogen Report"
date: "`r format(Sys.time(), '%d %B %Y')`"
output: html_document
---
```{r GDW Query, echo=TRUE, cache=FALSE, message=FALSE, warning=FALSE, results='hide'}
####################################################################
# QUERY DATABASE AND EXTRACT DATA
source(Extract_data.R, local=TRUE)