来自selectInput的具有多个条件的闪亮R watchEvent

然后

我正在开发一个闪亮的应用程序,并且observeEvent()在创建全部源自的多个输入的复杂表达式时遇到功能上的困难selectInput()

我的问题是一些内的表达式的observeEvent()功能在启动时触发,导致事件过早执行(即我actionButton()在启动时禁用,因为它应该是,但是当选择的至少一个输入变为启用时,理想我想希望仅在选择所有输入后才启用它)。如下所示:

  observeEvent({
    #input$cohort_file
    input$cohort_IDvar
    input$cohort_index_date
    input$cohort_EOF_date
    input$cohort_EOF_type
    input$cohort_Y_name
    input$cohort_L0
  }, {
    enable("set_cohort_button")
  })

作为参考,我使用shinyjsgithub上@daattali提供包来启用/禁用actionButton()

除最后一个输入(即input$cohort_L0)外的所有内容似乎都在启动进行了初始化,因此仅在选中observeEvent()启用如果您运行我的应用程序并从上到下按顺序选择输入,那么它似乎可以正常工作。当我决定随机选择输入时,我才发现它并没有达到预期的工作,并且发现选择是我需要选择启用的唯一输入actionButtoninput$cohort_L0observeEvent()input$cohort_L0actionButton()

代码的UI部分如下所示:

# Variable selection
                          selectInput('cohort_IDvar', 'ID', choices = ''),
                          selectInput('cohort_index_date', 'Index date', choices = ''),
                          selectInput('cohort_EOF_date', 'End of follow-up date', choices = ''),
                          selectInput('cohort_EOF_type', 'End of follow-up reason', choices = ''),
                          selectInput('cohort_Y_name', 'Outcome', choices = ''),
                          selectInput('cohort_L0', 'Baseline covariate measurements', choices = '', multiple=TRUE, selectize=TRUE),

我正在observe()收集一个上传数据集的列名,将其定向selectInput()为如下所示:

  ### Collecting column names of dataset and making them selectable input
  observe({
    value <- c("",names(cohort_data()))
    updateSelectInput(session,"cohort_IDvar",choices = value)
    updateSelectInput(session,"cohort_index_date",choices = value)
    updateSelectInput(session,"cohort_EOF_date",choices = value)
    updateSelectInput(session,"cohort_EOF_type",choices = value)
    updateSelectInput(session,"cohort_L0",choices = value)
  })

我已经研究过使用参数,ignoreInit = TRUE但对于在其中包含多个表达式的情况,它没有任何作用observeEvent()我也考虑过强制不进行默认选择,selectInput()但是没有运气。

因此,我的两部分问题是observEvent()当仅选择所有输入时如何执行/如何阻止启动时初始化输入?

我的整个代码:

library(shiny)
library(shinyjs)

ui <- fluidPage(

  useShinyjs(),
  navbarPage("Test",
             tabPanel("Cohort",
                      sidebarLayout(
                        sidebarPanel(
                          fileInput("cohort_file", "Choose CSV File",
                                    multiple = FALSE,
                                    accept = c("text/csv",
                                               "text/comma-separated-values,text/plain",
                                               ".csv")),
                          # Horizontal line ----
                          tags$hr(),
                          # Variable selection
                          selectInput('cohort_IDvar', 'ID', choices = ''),
                          selectInput('cohort_index_date', 'Index date', choices = ''),
                          selectInput('cohort_EOF_date', 'End of follow-up date', choices = ''),
                          selectInput('cohort_EOF_type', 'End of follow-up reason', choices = ''),
                          selectInput('cohort_Y_name', 'Outcome', choices = ''),
                          selectInput('cohort_L0', 'Baseline covariate measurements', choices = '', multiple=TRUE, selectize=TRUE),
                          # Horizontal line ----
                          tags$hr(),
                          disabled(
                            actionButton("set_cohort_button","Set cohort")
                          )
                          #actionButton("refresh_cohort_button","Refresh")
                        ),
                        mainPanel(
                          DT::dataTableOutput("cohort_table"),
                          tags$div(id = 'cohort_r_template')
                        )
                      )
             )
  )
)

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

  ################################################
  ################# Cohort code
  ################################################

  cohort_data <- reactive({
    inFile_cohort <- input$cohort_file
    if (is.null(inFile_cohort))
      return(NULL)
    df <- read.csv(inFile_cohort$datapath, 
                   sep = ',')
    return(df)
  })

  rv <- reactiveValues(cohort.data = NULL)
  rv <- reactiveValues(cohort.id = NULL)
  rv <- reactiveValues(cohort.index.date = NULL)
  rv <- reactiveValues(cohort.eof.date = NULL)
  rv <- reactiveValues(cohort.eof.type = NULL)

  ### Creating a reactiveValue of the loaded dataset
  observeEvent(input$cohort_file, rv$cohort.data <- cohort_data())

  ### Displaying loaded dataset in UI
  output$cohort_table <- DT::renderDataTable({
    df <- cohort_data()
    DT::datatable(df,options=list(scrollX=TRUE, scrollCollapse=TRUE))
  })

  ### Collecting column names of dataset and making them selectable input
  observe({
    value <- c("",names(cohort_data()))
    updateSelectInput(session,"cohort_IDvar",choices = value)
    updateSelectInput(session,"cohort_index_date",choices = value)
    updateSelectInput(session,"cohort_EOF_date",choices = value)
    updateSelectInput(session,"cohort_EOF_type",choices = value)
    updateSelectInput(session,"cohort_L0",choices = value)
  })

  ### Creating selectable input for Outcome based on End of Follow-Up unique values
  observeEvent(input$cohort_EOF_type,{
    updateSelectInput(session,"cohort_Y_name",choices = unique(cohort_data()[,input$cohort_EOF_type]))
  })

  ### Series of observeEvents for creating vector reactiveValues of selected column
  observeEvent(input$cohort_IDvar, {
    rv$cohort.id <- cohort_data()[,input$cohort_IDvar]
  })
  observeEvent(input$cohort_index_date, {
    rv$cohort.index.date <- cohort_data()[,input$cohort_index_date]
  })
  observeEvent(input$cohort_EOF_date, {
    rv$cohort.eof.date <- cohort_data()[,input$cohort_EOF_date]
  })
  observeEvent(input$cohort_EOF_type, {
    rv$cohort.eof.type <- cohort_data()[,input$cohort_EOF_type]
  })

  ### ATTENTION: Following eventReactive not needed for example so commenting out
  ### Setting id and eof.type as characters and index.date and eof.date as Dates
  #cohort_data_final <- eventReactive(input$set_cohort_button,{
  #  rv$cohort.data[,input$cohort_IDvar] <- as.character(rv$cohort.id)
  #  rv$cohort.data[,input$cohort_index_date] <- as.Date(rv$cohort.index.date)
  #  rv$cohort.data[,input$cohort_EOF_date] <- as.Date(rv$cohort.eof.date)
  #  rv$cohort.data[,input$cohort_EOF_type] <- as.character(rv$cohort.eof.type)
  #  return(rv$cohort.data)
  #})

  ### Applying desired R function
  #set_cohort <- eventReactive(input$set_cohort_button,{
    #function::setCohort(data.table::as.data.table(cohort_data_final()), input$cohort_IDvar, input$cohort_index_date, input$cohort_EOF_date, input$cohort_EOF_type, input$cohort_Y_name, input$cohort_L0)
  #})

  ### R code template of function
  cohort_code <- eventReactive(input$set_cohort_button,{
    paste0("cohort <- setCohort(data = as.data.table(",input$cohort_file$name,"), IDvar = ",input$cohort_IDvar,", index_date = ",input$cohort_index_date,", EOF_date = ",input$cohort_EOF_date,", EOF_type = ",input$cohort_EOF_type,", Y_name = ",input$cohort_Y_name,", L0 = c(",paste0(input$cohort_L0,collapse=","),"))")
  })

  ### R code template output fo UI
  output$cohort_code <- renderText({
    paste0("cohort <- setCohort(data = as.data.table(",input$cohort_file$name,"), IDvar = ",input$cohort_IDvar,", index_date = ",input$cohort_index_date,", EOF_date = ",input$cohort_EOF_date,", EOF_type = ",input$cohort_EOF_type,", Y_name = ",input$cohort_Y_name,", L0 = c(",paste0(input$cohort_L0,collapse=","),"))")
  })

  ### Disables cohort button when "Set cohort" button is clicked
  observeEvent(input$set_cohort_button, {
    disable("set_cohort_button")
  })

  ### Disables cohort button if different dataset is loaded
  observeEvent(input$cohort_file, {
    disable("set_cohort_button")
  })

  ### This is where I run into trouble
  observeEvent({
    #input$cohort_file
    input$cohort_IDvar
    input$cohort_index_date
    input$cohort_EOF_date
    input$cohort_EOF_type
    input$cohort_Y_name
    input$cohort_L0
  }, {
    enable("set_cohort_button")
  })

  ### Inserts heading and R template code in UI when "Set cohort" button is clicked
  observeEvent(input$set_cohort_button, {
    insertUI(
      selector = '#cohort_r_template',
      ui = tags$div(id = "cohort_insertUI", 
                    h3("R Template Code"),
                    verbatimTextOutput("cohort_code"))
    )
  })

  ### Removes heading and R template code in UI when new file is uploaded or when input is changed
  observeEvent({
    input$cohort_file
    input$cohort_IDvar
    input$cohort_index_date
    input$cohort_EOF_date
    input$cohort_EOF_type
    input$cohort_Y_name
    input$cohort_L0
  }, {
    removeUI(
      selector = '#cohort_insertUI'
    )
  })

}

# Run the application 
shinyApp(ui = ui, server = server)
迪安·阿塔利

您作为触发事件传递给observeEvent的代码块是

{
  input$cohort_IDvar
  input$cohort_index_date
  input$cohort_EOF_date
  input$cohort_EOF_type
  input$cohort_Y_name
  input$cohort_L0
}

这意味着,就像任何其他反应式代码块一样,当这些值中的任何一个发生更改时,该反应式块都被视为无效,因此观察者将触发。因此,您所看到的行为是有道理的。

听起来您想要的是仅在设置所有值时才执行。听起来好像很好地使用了该req()功能!尝试这样的事情:

observe({
  req(input$cohort_IDvar, input$cohort_index_date, input$cohort_EOF_date, ...)
  enable("set_cohort_button")
})

请注意,shinyjs::enable()具体而言,您可以改用shinyjs::toggleState()函数。我认为在这种情况下,该req()功能是更好的选择。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

java.nio.file.WatchEvent仅提供相对路径。如何获取修改后的文件的绝对路径?

Java-使用WatchEvent抑制未经检查的强制转换警告是否安全?

与唯一的watchEvent关联的actionButton的动态数量

R strsplit()具有多个条件

R有光泽; 如何使用来自selectInput的多个输入传递到dplyr中的“ select”选项?

R带有updateCheckboxGroupInput()和selectinput()的闪亮缓存值

在模块中使用的watchEvent Shiny函数不起作用

如何使react()内部的值更改,具体取决于该react()内部的watchEvent()

闪亮的watchEvent复制输出

让downloadButton与watchEvent一起使用

条件面板R闪亮的多个条件

具有多个文件输入的R闪亮条件面板

来自selectInput的闪亮更新数据

索引匹配具有来自数据透视表的多个条件

Google表格中的COUNTIF具有多个“非”或“其他”多个条件,而不是来自Google表单的回复条件

通过使用watchEvent发生内存泄漏

使用一个if()而不是多个watchEvent()在闪亮的应用程序中显示消息

来自具有多个条件的数据框的条件子集

R闪亮:eventReactive / ObserveEvent,eventExpr具有AND

闪亮模块中的带有updateMaterialSwitch的watchEvent不会更新输入

根据先前的输入更新带有watchEvent的闪亮模块

使用Shiny R中的watchEvent不会更新数据框

R存储来自lapply的具有多个功能的输出

具有多个条件的R子集

观察者()中嵌套的watchEvent()经常执行

R中具有多个条件的Countif

Mysql,联接表并具有来自不同行的多个条件

闪亮的仪表板。每个井面板具有多个 selectInput 的动态 UI

具有多个条件和多个结果的 R if 语句