ObserveEvent issues in shiny modules

Deadly 提交于 2020-06-01 07:39:06

问题


Hoping the everyone is doing good in these tough times.

In my demo app the observeEvent when run from the module server side updates the numeric value instantly even when I am using ignoreInit = TRUE . This argument is working fine in Shiny apps without the shiny modules. I ran into a this trivial problem while using observeEvent from module server which I am not understanding why? Can anyone explain the reason for such differential behavior in Shiny apps?

Below is my Demo code

       library(shiny)
       library(shinyjs)
      # Module UI

          ui_module <- function(id){
               ns <- NS(id)
                     fluidRow(
                           selectInput(ns("Year"),"Year",c(2020:2050)),
                           uiOutput(ns("Input_ui")),
             # Default value define in ui
                 tags$div(id = ns("Def_id") ,numericInput(ns("default"),"Deafault",NA,value = 
                             2423,step = 1))
                   )
           }

      # Module Server

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

          # Default value while rendering ui.

            output$Input_ui <- renderUI({
                        ns <- session$ns
                       numericInput(ns("Input"), "Number", NA, value = 537153, step = 1)
            })

        # Values update anyhow at the start even if I am using ignoreInit = TRUE
        # The argument works in Apps without the shiny modules but not here. WHY???

               observeEvent(input$Year,{
                            updateNumericInput(session,
                                         "Input", "Number",value = 4, step = 1)

                            updateNumericInput(session,
                                          "default", "Deafault",value = 60, step = 1)},ignoreInit = TRUE)
                         }

         # App UI

                ui <- fluidPage(

                       inlineCSS(".row {margin:60px 0;}
                       div[id *= 'input_div']{position:absolute;left:26em;width:200px;height:30px;}
                       div[id *= 'input2_div']{position:absolute;left:20em;width:200px;height:30px;
                       div[id *= 'Def_id']{position:absolute;left:30em;width:200px;height:30px;"),

            fluidRow(id = "Row",
                      uiOutput("ui"),
                      actionButton("add","ADD")
                   )
              )

       # APP Server

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

                   #Counter

                   n <- 0

         # One by one adding the modules

              observeEvent(input$add,{
                          n <<- n + 1
                          panels <- paste0("panels_new",n)
                           insertUI("#Row",
                           "beforeEnd",
                           ui_module(panels))

                 callModule(server_module,panels)
           })

              # Generating a no of shiny modules based on the some table rows
              # n will be nrow in my main app.

             output$ui <- renderUI({
                         n <- 2
                         list <- as.list(1:n)
                         lapply(list, function(i){
                         panels <- paste0("panels",i)
                                  fluidRow(
                                        ui_module(panels)
                                        )
                               }) 
                 })

             observe({
                      n <- 2
                     list <- as.list(1:n)
                     lapply(list, function(i){
                     panels <- paste0("panels",i)
                     callModule(server_module,panels)
                 })
            })
       }

    shinyApp(ui,server)

Thanks

Jitu

来源:https://stackoverflow.com/questions/62024757/observeevent-issues-in-shiny-modules

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!