【问题标题】:Capture selectize Input value in R shiny module在 R 闪亮模块中捕获选择输入值
【发布时间】:2021-05-27 00:19:00
【问题描述】:

我正在使用选择输入构建一个闪亮的应用程序。
输入中的选择取决于基础数据中的 ID。
在我的真实应用中,数据通过调用 API 进行更新。
当我点击“更新数据”按钮时,我希望选择输入中选择的 id 选项保持不变。

在使用闪亮的模块之前,我能够做到这一点。但是,当我尝试将代码转换为使用闪亮模块时,它无法保存选定的 id 值,并且每次更新基础数据时都会重置 selectize 输入。

下面的例子在没有模块的情况下很有帮助,但是当我使用模块时它似乎不起作用...link here

下面是一个代表。感谢您的帮助。

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)

【问题讨论】:

    标签: r shiny module shinymodules


    【解决方案1】:

    只需在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)
    

    【讨论】:

    • 巨大的帮助非常感谢它
    猜你喜欢
    • 1970-01-01
    • 2021-07-16
    • 2016-03-16
    • 2017-03-02
    • 2020-07-04
    • 2016-11-21
    • 2020-08-01
    相关资源
    最近更新 更多