【问题标题】:Access reactive created in observe访问在观察中创建的反应式
【发布时间】:2021-03-04 08:44:00
【问题描述】:

我正在尝试访问在 observe( ) 中创建的反应式并希望作为模块返回的对象。在下面的示例中,我正在从模块中查找 mydf()input$account_page。我知道我可以使用observeEvent(input$go_to, { }) 显示模态,但我有其他用例可以这样做,我很难为此创建可重现的示例。因此,我为指定的问题创建了以下内容。

library(shiny)
library(shinyWidgets)

choiceVec <- c("Beef/Pork" = "beefpork","Sugar sweeteened bev." = "ssb",
               "Total fruit" = "total_fruit")

address_ui <- function(id) { }

address_server <- function(id, signin =  reactive(0)) {
  moduleServer(id, function(input, output, session) {
    
    observe({
      
      shiny::showModal(
        
        shiny::modalDialog(
          
          fluidRow(
            
            column(
              width = 6,
              
              shiny::textInput(
                "state",
                "State",
                width = "100%", 
                placeholder = "California"
              )),
            
            column(
              width = 6,
              
              shinyWidgets::pickerInput(
                "country",
                "Country",
                choices = choiceVec, 
                width = "100%", 
                # selected = "us",
                options = list(
                  title = "Select Country",
                  `live-search` = TRUE)
              )
            )),
          
          title = "Address",
          footer = tags$span(
            shiny::actionButton(
              'account_page',
              'Next'
            )
          ),
          size = 'm'
        )
      )
      
      
      mydf <- reactive({
        
        list(
             state = input$state,
             country = input$country
        )
        
      })
      
      
    })
    
    return( list(mydf(), reactive({ input$account_page }) ))
    
  })
  
  }
    
      

ui <- fluidPage(
  actionLink(
    "go_to",
    "Click Me",
    icon = icon("credit-card")
  ),
  address_ui("user_info")
)

server <- function(input, output, session) {
  
  user_details_return <- address_server(
    id = "user_info",
    signin = 1
  )
  
  
}

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    反应性对象mydf() 仅在观察者内部可用。试试这个

    library(shiny)
    library(shinyWidgets)
    
    choiceVec <- c("Beef/Pork" = "beefpork","Sugar sweeteened bev." = "ssb",
                   "Total fruit" = "total_fruit")
    
    address_ui <- function(id) {ns <- NS(id) 
                                verbatimTextOutput(ns("t1")) }
    
    address_server <- function(id, signin =  reactive(0)) {
      moduleServer(id, function(input, output, session) {
        ns <- session$ns
        observe({
          
          shiny::showModal(
            
            shiny::modalDialog(
              
              fluidRow(
                
                column(
                  width = 6,
                  
                  shiny::textInput(
                    ns("state"),
                    "State",
                    width = "100%", 
                    placeholder = "California"
                  )),
                
                column(
                  width = 6,
                  
                  shinyWidgets::pickerInput(
                    ns("country"),
                    "Country",
                    choices = choiceVec, 
                    width = "100%", 
                    # selected = "us",
                    options = list(
                      title = "Select Country",
                      `live-search` = TRUE)
                  )
                )),
              
              title = "Address",
              footer = tags$span(
                shiny::actionButton(
                  ns('account_page'),
                  'Next'
                )
              ),
              size = 'm'
            )
          )
          
          
          mydf <- reactive({
            
            list(
              state = input$state,
              country = input$country
            )
            
          })
          
          observeEvent(input$account_page, {
            removeModal()
            return( list(mydf(), reactive({ input$account_page }) ))
          })
            
          output$t1 <- renderPrint({
            mydf()
          })
        })
        
      })
      
    }
    
    
    
    ui <- fluidPage(
      actionLink(
        "go_to",
        "Click Me",
        icon = icon("credit-card")
      ),
      address_ui("user_info") 
    )
    
    server <- function(input, output, session) {
      
      user_details_return <- address_server(
        id = "user_info",
        signin = 1
      )
      
    }
    
    shinyApp(ui, server)
    

    请注意,showModal 不需要位于 observer 中。你可以这样做

          observe({return( list(mydf(), reactive({ input$account_page }) )) })
    
          observeEvent(input$account_page, {
            removeModal()
          })
    

    【讨论】:

      猜你喜欢
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 2019-03-31
      • 2018-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多