Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将R标记参数传递给源R脚本_R_Shiny_Parameter Passing_R Markdown_Environment - Fatal编程技术网

将R标记参数传递给源R脚本

将R标记参数传递给源R脚本,r,shiny,parameter-passing,r-markdown,environment,R,Shiny,Parameter Passing,R Markdown,Environment,我有一个闪亮的应用程序,用户可以在其中选择传递给参数化R降价报告的选项。然后,Rmd生成一系列R脚本,以提取和总结数据,为报告创建绘图等 我正在寻找的用于提取数据的脚本包括一个参数化SQL查询,它从R标记params继承值(这反过来又从输入继承)。但是,整个过程在这一点停止,我得到一个错误,指出params不存在 我可以合理地确定,将输入从Shiny传递到R markdown参数工作正常-因此,问题似乎是将它们传递到源代码脚本(注意:它只是一个R脚本,而不是一个函数)。我猜这与脚本在获取源代码时

我有一个闪亮的应用程序,用户可以在其中选择传递给参数化R降价报告的选项。然后,Rmd生成一系列R脚本,以提取和总结数据,为报告创建绘图等

我正在寻找的用于提取数据的脚本包括一个参数化SQL查询,它从R标记
params
继承值(这反过来又从输入继承)。但是,整个过程在这一点停止,我得到一个错误,指出
params
不存在

我可以合理地确定,将输入从Shiny传递到R markdown参数工作正常-因此,问题似乎是将它们传递到源代码脚本(注意:它只是一个R脚本,而不是一个函数)。我猜这与脚本在获取源代码时访问的环境有关(尽管它使用在R markdown中的前一个块中生成的数据库连接没有问题),但除此之外,对于如何纠正这一点,我有点不知所措。任何想法都将不胜感激

以下是闪亮的应用程序:

##########################################
# 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脚本的相关块:

{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))


查看
source
?source
)中的参数
local

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)
但是,当通过
shinny应用程序运行
Rmd
时,您希望在
shinny
工作的环境中工作,并且希望将外部脚本源代码化,就像它粘贴到行中一样(请参阅)。以下方面应起作用:

---
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) 

查看
source
?source
)中的参数
local

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)
但是,当通过
shinny应用程序运行
Rmd
时,您希望在
shinny
工作的环境中工作,并且希望将外部脚本源代码化,就像它粘贴到行中一样(请参阅)。以下方面应起作用:

---
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)