我有一个闪亮的应用程序,用户可以选择传递给参数化R标记报告的选项。然后,Rmd获取一系列R脚本来提取和总结数据,创建报告的情节等。
我正在寻找的提取数据的脚本包括一个参数化的SQL查询,它继承了R标记params的值(而后者又是从闪亮的input继承的)。但是,整个过程在此时停止,我得到一个错误,说明params不存在。
我相当肯定,将输入从闪亮的值传递到R标记参数是很好的--所以问题似乎是将它们传递给源脚本(注意:它只是一个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标记的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标记参数继承的名称:
# 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标记块中找到另一个脚本来建立。
这是我得到的错误:
Quitting from lines 84-88 (Pathogen_Report.Rmd)
Warning: Error in gsub: object 'params' not found
[No stack trace available]发布于 2020-01-11 05:30:35
看看local in source (?source)中的参数。
localTRUE、FALSE或环境,以确定解析表达式的计算位置。FALSE (默认值)对应于用户的工作区(全局环境),并对应于调用源的环境。
当直接呈现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'}####################################################################
查询数据库和提取数据
源(Extract_data.R,local=FALSE) #与源(Extract_data.R)相同
However, when running the `Rmd` through a `Shiny App` you want to work in the environment that `Shiny` works in, and you want to source the external script as if it was pasted in line (see [https://shiny.rstudio.com/articles/scoping.html](https://shiny.rstudio.com/articles/scoping.html)). The following should work:
```javascriptparams:
病原体:
label: "Enter pathogen of interest:" value: Shigellainput: selectchoices: [Campylobacter, Escherichia, Salmonella, Shigella]普金斯托:
value: no标题:“病原体报告”
日期:"r format(Sys.time(), '%d %B %Y')“
输出: html_document
####################################################################
# QUERY DATABASE AND EXTRACT DATA
source(Extract_data.R, local=TRUE) https://stackoverflow.com/questions/59681544
复制相似问题