【问题标题】:Passing R Shiny reactive SelectInput value to selectizeInput将 R Shiny 反应 SelectInput 值传递给 selectizeInput
【发布时间】:2015-05-12 06:47:14
【问题描述】:

我的 Shiny 应用使用来自鸟类地图集的开放数据,包括按物种划分的纬度/经度坐标。鸟类名称有不同的语言,加上首字母缩略词。

这个想法是用户首先选择语言(或首字母缩写词)。根据选择,Shiny 呈现一个 selectizeInput 列表,其中包含唯一鸟类物种名称。 Then, when one species is selected, a leaflet map is generated.

我已经完成了几个闪亮的应用程序,但这次我错过了一些明显的东西。当应用程序启动时,一切都很好。但是,选择新语言时不会重新呈现 selectizeInput 列表。

所有当前代码和一些示例数据都在这里作为 GitHub Gist https://gist.github.com/tts/924b764e7607db5d0a57

如果有人能指出我的问题,我将不胜感激。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    问题在于renderUIbirds 反应块都依赖于input$lan 输入。

    如果您在birds 块中添加print(input$birds),您将看到它在renderUI 有机会更新它们以适应新语言之前使用鸟的名称。然后你传递的data leaflet 情节是空的。

    尝试在鸟表达式中在input$lan 周围添加isolate,使其仅依赖于input$birds

    birds <- reactive({
        if( is.null(input$birds) )
          return()
        data[data[[isolate(input$lan)]] == input$birds, c("lon", "lat", "color")]
      })
    

    当您更改语言时,renderUI 将更改 selectize,这将触发 input$birds 并更新数据。

    除了使用renderUI,您还可以使用(替换uiOutput)在ui.R 中创建selectizeInput

    selectizeInput(
            inputId = "birds", 
            label = "Select species",
            multiple  = F,
            choices = unique(data[["englanti"]])
          )
    

    在您的 server.R 中,使用以下命令进行更新:

    observe({
        updateSelectizeInput(session, 'birds', choices = unique(data[[input$lan]]))
      })
    

    【讨论】:

    • 谢谢,正是我需要的。意识到我最好熟悉isolateobserve - 现在我有两个用例。很棒。
    猜你喜欢
    • 2014-12-03
    • 1970-01-01
    • 2014-02-23
    • 2018-03-22
    • 2017-01-17
    • 1970-01-01
    • 2021-01-08
    • 2016-04-11
    相关资源
    最近更新 更多