我对Shiny模块非常陌生。 我正在尝试从我的一个模块中调用一个函数(而不是一个模块)。 我想将当前反应性值的内容(在模块中)作为参数传递给函数。
该函数根据应该从'modemtab'中馈入的mrn编号,开始日期和结束日期发出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)
})
答案 0 :(得分: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)