簡體   English   中英

在 R shiny 模塊中捕獲選擇輸入值

[英]Capture selectize Input value in R shiny module

我正在構建一個帶有選擇輸入的 shiny 應用程序。
輸入中的選擇取決於基礎數據中的 id。
在我的真實應用程序中,數據通過調用 API 進行更新。
當我點擊“更新數據”按鈕時,我希望選擇輸入中的選定 id 選項保持不變。

在使用 shiny 模塊之前,我能夠做到這一點。 但是,當我嘗試將代碼轉換為使用 shiny 模塊時,它無法保存選定的 id 值,並且每次更新基礎數據時都會重置選擇輸入。

以下示例在沒有模塊的情況下很有幫助,但是當我使用該模塊時,它似乎不起作用...... 鏈接在這里

下面是一個代表。 謝謝你的幫助。

library(shiny)
library(tidyverse)


# module UI

mymod_ui <- function(id){
      ns <- NS(id)
      tagList(
                
                uiOutput(ns("ids_lookup")),
                              
      )        
      
}


# module server

mymod_server <- function(input, output, session, data, actionb){
      ns <-session$ns
      
      ids <- reactive(
                
                data() %>%
                          filter(!is.na(first_name) & !is.na(last_name) & !is.na(ages)) %>%
                          mutate(ids = paste(first_name, last_name, sep = " ")) %>%
                          select(ids)
      )
      
      
      output$ids_lookup <- renderUI({
                
                selectizeInput(ns("lookup"), 
                               label = "Enter id:", 
                               choices = c("Type here ...", ids()), multiple = FALSE)
                
      })
      
      # here is where I would like to hold on to the selected ids when updating the table
      # when I click the "reload_data" button I don't want the name to change
      # I pass the button from the main server section into the module
      
      current_id_selection <- reactiveVal("NULL")
      
      observeEvent(actionb(), {
                
                current_id_selection(ns(input$ids_lookup))
                
                updateSelectizeInput(session, 
                                     inputId = ns("lookup"), 
                                     choices = ids(), 
                                     selected = current_id_selection())
      })
     
}


ui <- fluidPage(
      titlePanel("Test module app"),
      br(),
      
      # this button reloads the data
      actionButton(
                
                inputId = "reload_data", 
                label = "Reload data"
      ),
      br(),
      br(),
      
      # have a look at the data
      h4("Raw data"),
      tableOutput("mytable"),
      
      br(),
      
      # now select a single id for further analysis in a much larger app
      mymod_ui("mymod"), 
      
      
)


server <- function(input, output, session) {
      
     
      
      df <- eventReactive(input$reload_data, {
                
                # in reality, df is a dataframe which is updated from an API call everytime you press the action button
                
                df <- tibble(
                          first_name = c("john", "james", "jenny", "steph"),
                          last_name = c("x", "y", "z", NA),
                          ages = runif(4, 30, 60)
                )
      
                
                return(df)
                
      }
      )
      
      
      output$mytable <- renderTable({
                
                df()
                
      })
      
      
      # make the reload data button a reactive val that can be passed to the module for the selectize Input
      
      mybutton <- reactive(input$reload_data)
      
      callModule(mymod_server, "mymod", data = df, actionb = mybutton)
      

      
      
}


shinyApp(ui, server)

只需在updateSelectizeInput()中使用inputId = "lookup"而不是inputId = ns("lookup") 另外,您那里還有另一個錯字。 嘗試這個

library(shiny)
library(tidyverse)


# module UI

mymod_ui <- function(id){
  ns <- NS(id)
  tagList(
    uiOutput(ns("ids_lookup")),
    verbatimTextOutput(ns("t1"))
  )        
  
}


# module server

mymod_server <- function(input, output, session, data, actionb){
  ns <-session$ns
  
  ids <- reactive(
    
    data() %>%
      filter(!is.na(first_name) & !is.na(last_name) & !is.na(ages)) %>%
      mutate(ids = paste(first_name, last_name, sep = " ")) %>%
      select(ids)
  )
  
  
  output$ids_lookup <- renderUI({
    
    selectizeInput(ns("lookup"), 
                   label = "Enter id:", 
                   choices = c("Type here ...", ids()), multiple = FALSE)
    
  })
  
  # here is where I would like to hold on to the selected ids when updating the table
  # when I click the "reload_data" button I don't want the name to change
  # I pass the button from the main server section into the module
  
  current_id_selection <- reactiveValues(v=NULL)
  
  observeEvent(actionb(), {
    req(input$lookup)
    current_id_selection$v <- input$lookup
    
    output$t1 <- renderPrint(paste0("Current select is ",current_id_selection$v))
    
    updateSelectizeInput(session, 
                         inputId = "lookup", 
                         choices =  ids(), 
                         selected = current_id_selection$v )
  })
  
}


ui <- fluidPage(
  titlePanel("Test module app"),
  br(),
  
  # this button reloads the data
  actionButton(inputId = "reload_data", label = "Reload data"
  ),
  br(),
  br(),
  
  # have a look at the data
  h4("Raw data"),
  tableOutput("mytable"),
  
  br(),
  
  # now select a single id for further analysis in a much larger app
  mymod_ui("mymod")
  
)


server <- function(input, output, session) {
  
  df <- eventReactive(input$reload_data, {
    
    # in reality, df is a dataframe which is updated from an API call everytime you press the action button
    
    df <- tibble(
      first_name = c("john", "james", "jenny", "steph"),
      last_name = c("x", "y", "z", NA),
      ages = runif(4, 30, 60)
    )
    
    return(df)
    
  })
  
  
  output$mytable <- renderTable({
    
    df()
    
  })
  
  
  # make the reload data button a reactive val that can be passed to the module for the selectize Input
  
  mybutton <- reactive(input$reload_data)
  
  callModule(mymod_server, "mymod", data = df, actionb = mybutton)
  
}

shinyApp(ui, server)

輸出

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM