【问题标题】:How to get choices values of SelectInput in Shiny?如何在 Shiny 中获取 SelectInput 的选择值?
【发布时间】:2021-03-24 03:04:05
【问题描述】:

如何获取 SelectInpute 中的选项列表?

ui.R

selectInput(inputId = "select_gender", 
    label = "Gender",
    choices = c("Male","Female"),
    width = 150
)

服务器.R

# Something like...

genders <- input$select_gender["choices"]

# So that the gender would be:

> genders

[1] Male Female

【问题讨论】:

  • input$select_gender 返回用户的选择。要获取所有选项,我建议使用全局变量来存储选项,然后您可以在 ui 和服务器中使用它。

标签: shiny


【解决方案1】:

来自scoping rules of Shiny

global.R 中定义的对象与服务器函数定义之外的app.R 中定义的对象类似,但有一个重要区别:它们对 ui 对象中的代码也是可见的。这是因为它们被加载到 R 会话的全局环境中; Shiny 应用程序中的所有 R 代码都在全局环境或其子环境中运行。

但是,这并不意味着app.R 中定义的对象不能同时用于 UI 和服务器端,它们只是属于不同的环境。

例如:

library("shiny")
library("pryr")

# or in global.R
genders <- c("Male", "Female")
gen_env <- where("genders")
par_env <- parent.env(gen_env)

ui <- fluidPage(
  selectInput("shiny_gender", "Select Gender", choices = genders),
  verbatimTextOutput("selected_gender_index"),
  p("The `genders` object belongs to the environment:"),
  verbatimTextOutput("gen_env_print"),
  p("Which is the child of the environment:"),
  verbatimTextOutput("par_env_print")
)

server <- function(input, output) {
   output$selected_gender_index <- renderPrint({
     # use the 'genders' vector on the server side as well
     which(genders %in% input$shiny_gender)
   })

   output$gen_env_print <- renderPrint(gen_env)
   output$par_env_print <- renderPrint(par_env)
}

shinyApp(ui = ui, server = server)

【讨论】:

    【解决方案2】:

    我一直在寻找selectinput 的选择,但没有重新计算选择。例如,如果数据来自数据库、文件或其他来源。

    我没有得到答案。 (我得到了这个问题,但不是我的解决方案)。

    这是一个解决方案,它也可以从服务器设置selectinput

    • 在反应函数中设置选择列表
    • 在服务器端构建selectinput(带有选择列表反应函数)
    • 在服务器端设置并获取selectinput

    这里是代码

    options(encoding = "UTF-8") 
    
    library("shiny")
    library("pryr")
    ui <- fluidPage(
      uiOutput("shiny_gender.UI"),
      verbatimTextOutput("selected_gender_index"),
      p("The `genders` object belongs to the environment:"),
      verbatimTextOutput("gen_env_print"),
      p("Which is the child of the environment:"),
      verbatimTextOutput("par_env_print"),
      p(""),
      textInput("set_input_txt","Set the car in letter (for example `Datsun 710`)",
                #" Set the Select Input Male / Female ",
                ""),
      actionButton("submit","submit")
    )
    
    
    server <- function(input, output, session) {
      observeEvent(
        c(input$submit),
        {
    
          if (input$submit>0) {
            updateSelectInput(session, "shiny_gender",
                              #   server = TRUE,  if updateSelectizeInput
                              choices =shiny_gender.list(),
                              selected = input$set_input_txt
            )
          }  
        }
      )
    
      shiny_gender.list <- reactive  ({
        #c("Male", "Female")
        rownames(mtcars)
      })  
    
    
      output$shiny_gender.UI <- renderUI({
        selectInput( "shiny_gender",
                     label="Select car",#"Select Gender",
                     choices =shiny_gender.list()
        )
      })  
    
    
      output$selected_gender_index <- renderPrint({
    
        which(shiny_gender.list() %in% input$shiny_gender)
      })
    
    
      output$gen_env_print <- renderPrint(where("shiny_gender.list"))
      output$par_env_print <- renderPrint(parent.env( where("shiny_gender.list")))
    
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-21
      • 2019-01-19
      • 2017-10-19
      • 2017-04-22
      • 2018-06-14
      • 2019-09-25
      • 1970-01-01
      相关资源
      最近更新 更多