首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Shiny中两个相关selectizeInput小部件的反应性更新

Shiny中两个相关selectizeInput小部件的反应性更新
EN

Stack Overflow用户
提问于 2018-03-19 22:38:14
回答 1查看 745关注 0票数 0

我有一个数据库,将科学物种名称(即栎属)和普通名称(即北红橡树)联系起来。在我试图创建的闪亮应用程序中,我希望有两个selectizeInput小部件,用户可以选择从我的数据库中填充的任意数量的科学名称或公共名称。

我希望这两个输入小部件是相互响应的,因此如果用户从科学列表中选择多个物种,那么该物种的公共名称将填充公共名称输入字段,反之亦然。我在这方面做了两次尝试,但在这两种情况下都没有完全正确地获得功能,所以我希望得到建议。

尝试1:

代码语言:javascript
复制
comm <- c("northern red oak", "white pine", "balsam fir", "box elder")
sci <- c("Quercus rubra", "Pinus strobus", "Abies balsamea", "Acer negundo")
db <- as.data.frame(cbind(comm, sci))
colnames(db) <- c("common", "scientific_name")

ui <- fluidPage(

# Application title
titlePanel("Mapping Tree Distributions"),


sidebarLayout(
  sidebarPanel(
    uiOutput("scientific"),

    uiOutput("common")
  ),

  mainPanel()
 )
)


server <- function(input, output, session) {

output$scientific <- renderUI({
     common.value <- input$common.name
     default.scientific <- if (is.null(common.value)) {
       "Quercus rubra"
     } else {
       as.character(db$scientific_name[db$common == common.value])
     }
     selectInput("scientific.name",
                 "Scientific Name of Organism",
                 choices = db$scientific_name,
                 multiple = TRUE,
                 selectize = TRUE,
                 selected = default.scientific)
 })

output$common <- renderUI({
       scientific.value <- input$scientific.name
       default.common <- if (is.null(scientific.value)) {
            "northern red oak"
            } else {
             as.character(db$common[db$scientific_name == scientific.value])
              }
      selectInput("common.name",
                 "Common Name of Organism",
                 choices = db$common,
                 multiple = TRUE,
                 selectize = TRUE,
                 selected = default.common)

    })
    }

# Run the application
shinyApp(ui = ui, server = server)

这主要是可行的,但一旦我在两个列表中选择第二个项目,它要么立即删除我之前选择的内容,要么恢复到默认选项(红色橡木/栎)。

这是我的第二次尝试:

代码语言:javascript
复制
comm <- c("northern red oak", "white pine", "balsam fir", "box elder")
sci <- c("Quercus rubra", "Pinus strobus", "Abies balsamea", "Acer negundo")
db <- as.data.frame(cbind(comm, sci))
colnames(db) <- c("common", "scientific_name")

ui <- fluidPage(

  # Application title
  titlePanel("Mapping Tree Distributions"),

  sidebarLayout(
    sidebarPanel(
      selectizeInput("scientific.name",
                  "Scientific Name of Organism",
                  choices = db$scientific_name,
                  multiple = TRUE,
                  selected = NULL),


      selectizeInput("common.name",
                  "Common Name of Organism",
                  choices = db$common,
                  multiple = TRUE,
                  selected = NULL)
    ),

    mainPanel()


  )
)


server <- function(input, output, session) {

  observeEvent(input$scientific.name, {
    updateSelectizeInput(session,
                         "common.name",
                         selected = db$common[db$scientific_name == 
                                    input$scientific.name])
  })

  observeEvent(input$common.name, {
    updateSelectizeInput(session,
                         "scientific.name",
                         selected = db$scientific_name[db$common == 
                                     input$common.name])
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

第二次尝试允许我一次选择多个名称,但一旦选择了2或3个名称,就会删除以前的选择。此外,如果我从科学列表中选择,它并不总是更新公共名称列表,反之亦然。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-19 23:30:52

在观察者中将==更改为%in%可以解决以下问题:

代码语言:javascript
复制
  observeEvent(input$scientific.name, {
    updateSelectizeInput(session,
                             "common.name",
                             selected = db$common[db$scientific_name %in% 


input$scientific.name])
  })

  observeEvent(input$common.name, {
    updateSelectizeInput(session,
                         "scientific.name",
                         selected = db$scientific_name[db$common %in% 
                                                         input$common.name])
  })
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49373540

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档