Реактивный доступ, созданный в observe

#r #shiny

#r #блестящий

Вопрос:

Я пытаюсь получить доступ к реактивному, созданному в 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)
 

Ответ №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 находиться внутри observe буквы r. Вы можете просто сделать

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

      observeEvent(input$account_page, {
        removeModal()
      })