0

我对闪亮的模块很陌生。我正在尝试从我的一个模块中调用一个函数(不是模块)。我想将我当前的反应值(在我的模块中)的内容作为参数传递给函数。

该函数根据应该从“modemtab”输入函数的mrn 编号、startdate 和enddate 生成一个sql 查询命令。

这是我得到的错误: .getReactiveEnvironment()$currentContext() 中的错误:没有活动的反应上下文不允许操作。(你试图做一些只能从反应式表达式或观察者内部完成的事情。)

我知道这是因为我将反应值而不是它们的内容传递给函数。我的问题是如何传递这些反应值的内容。

我在这里包含了我的一段代码。

谢谢。

app_server <- function(input, output,session) {
.
.
# getting csvupload_values from the first module and feeding it into the next.

csvupload_values <- callModule(csvupload, 'csv-upload')
callModule(modemtab,'mrntab', csvupload_values)


modemtab <- function(input, output, session, csvupload_values){

# the ouput$query is made in the UI part, but it's not the cause of issue.

output$query <- renderText({
    if(!is.null(csvupload_values$file_uploaded())){
      make_query(mrns = csvupload_values$file_uploaded()$mrns, 
                     startDate = csvupload_values$dates()[1], 
                     endDate = csvupload_values$dates()[2])


}




#This is the function called from within the second module (modemtab)
#this function is saved as a separate file in R folder 

    make_query <- function(...){
      glue_sql("
               select *
               FROM table
               WHERE
                 rgn_cd = {`rgn_cd`}
                 AND prdct_lne_cd = {`lob`}
                 AND ENCTR_STRT_TS >= {`startDate`}
                  AND ENCTR_END_TS <= {`endDate`}
                 "
               ,...
               ,.con = DBI::ANSI())
    }


csvuploadUI <- function(id){
  ns <- NS(id)
  tagList(
    fileInput(ns('file'), "Choose CSV File",
              accept = c("text/csv",
                         "text/comma-separated-values,text/plain",
                         ".csv")),


    dateRangeInput(
      ns('mrn_date_range'), label = 'Select the range of date:', 
      start = NULL, end = NULL, min = NULL,
      max = NULL, format = "mm/dd/yyyy", 
      startview = "month", weekstart = 0,
      language = "en", separator = " to ", width = NULL),


    # Input: Checkbox if file has header ----
    checkboxInput(ns('header'), "Header", TRUE)

  )

}

# Module Server

csvupload <- function(input, output, session){

  userFile <- reactive({
    # If no file is selected, don't do anything
    validate(need(input$file, message = FALSE))
    input$file
  })

  dataframe <- reactive({
    read.csv(userFile()$datapath,
             header = input$header)
  })


4

1 回答 1

0

这里:

csvupload_values <- callModule(csvupload, 'csv-upload')
callModule(modemtab,'mrntab', csvupload_values)

csvupload_values是一个反应导体,所以你不能callModule(modemtab,'mrntab', csvupload_values)在反应上下文之外做。你可以做:

server <- function(input, output,session) {

  csvupload_values <- callModule(csvupload, 'csv-upload')

  observeEvent(csvupload_values(),{
    if(!is.null(csvupload_values())){
      callModule(modemtab, 'mrntab', csvupload_values)    
    }
  })

}

现在,csvupload_values()一旦你上传了文件,它就是一个数据框,所以我不明白你为什么这样做csvupload_values$file_uploaded()。这是一个完整的例子:

modemtabUI <- function(id){
  ns <- NS(id)
  textOutput(ns("query"))
}

modemtab <- function(input, output, session, csvupload_values){
  output$query <- renderText({
    colnames(csvupload_values())
  })
}

csvuploadUI <- function(id){
  ns <- NS(id)
  tagList(
    fileInput(ns('file'), "Choose CSV File",
              accept = c("text/csv",
                         "text/comma-separated-values,text/plain",
                         ".csv")),

    checkboxInput(ns('header'), "Header", TRUE),

    dateRangeInput(
      ns('mrn_date_range'), label = 'Select the range of date:', 
      start = NULL, end = NULL, min = NULL, max = NULL, format = "mm/dd/yyyy", 
      startview = "month", weekstart = 0,
      language = "en", separator = " to ", width = NULL)
  )
}

csvupload <- function(input, output, session){

  userFile <- reactive({
    # If no file is selected, don't do anything
    validate(need(input$file, message = FALSE))
    input$file
  })

  reactive({
    read.csv(userFile()$datapath, header = input$header)
  })

}

ui <- fluidPage(
  csvuploadUI("csv-upload")
  modemtabUI("mrntab")
)

server <- function(input, output,session) {

  csvupload_values <- callModule(csvupload, 'csv-upload')

  observeEvent(csvupload_values(),{
    if(!is.null(csvupload_values())){
      callModule(modemtab, 'mrntab', csvupload_values)    
    }
  })

}

shinyApp(ui, server)
于 2019-06-08T09:25:12.450 回答