【问题标题】:R Shiny - What is the problem with my observe function ({UpdateSelectInput})?R Shiny - 我的观察函数({UpdateSelectInput})有什么问题?
【发布时间】:2020-09-29 05:55:32
【问题描述】:

我查看过类似的帖子来解决我的问题,但没有任何帮助,而且我已经没有耐心了。

我有一个非常简单的应用程序,我试图在其中查找选定的美国州内的所有县。我在 UI 中使用 SelectInput 让用户选择他们想要的州,并在服务器中使用 UpdateSelectInput 以便用户可以从他们的州选择中选择他们想要的县。

这是我的简化数据的样子:

**STATE_NAME      NAMELSAD**
Alabama            Clay County  
Alabama            Marengo County
Arkansas           Scott County

我的代码如下所示:

global.r

library(shiny)
library(leaflet)
library(sp)
library(rgdal)
library(htmltools)
library(DT)
library(ggplot2)
library(shinythemes)

path <- "C:/Users/user/Desktop/Countyapp/Countyapp/test_covid/"
setwd(path)

counties <- read.csv("us_counties1.csv")

UI.r

ui <- fluidPage(
        selectInput(inputId = "selectstate", label = "Select State", choices = (counties$STATE_NAME)),
        selectInput(inputId = "selectcounty", label =  "Select County", choices = NULL)
)

最后,server.R

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

    observe({
        updateSelectInput(session, "selectcounty", "Select County", 
                          choices = counties$NAMELSAD[counties$STATE_NAME==input$STATE_NAME])
    })

}

shinyApp(ui = ui, server = [enter image description here][1]server) 

基本上,我的第一个 SelectInput 工作,你可以选择任何你想要的状态。但是第二个选择器是空白的!为什么。 我的观察功能有问题,但我一直坐在这里,没有可能的解决方案。 如果可以的话请帮忙!谢谢!

【问题讨论】:

  • 您的selectInput for state 有一个inputId of selectstate --- 所以要引用这个输入,您需要input$selectstate...尝试在您的服务器中将其更改为:@987654330 @ 看看是否可行...

标签: r shiny selectinput


【解决方案1】:

observe() 函数的目的是观察反应表达式的变化。但是,updateSelectInput() 不是您编写的 reactive 表达式,因为其中的任何表达式都不涉及任何反应值或表达式。

相反,您应该 observe() 更改输入值,例如,如下所示:

observe({
 if(input$selectstate != ""){
 updateSelectInput(session, "selectcounty", "Select County", 
                          choices = counties$NAMELSAD[counties$STATE_NAME==input$selectstate,])
 }
else {
 updateSelectInput(session, "selectcounty", "Select County", 
                          choices = "Select State")
 }
})

observe 中的表达式现在是响应式的,因为 input 是响应式的。

请参阅this answer 进行精彩讨论。

【讨论】:

    【解决方案2】:

    请尝试使用observeEvent,而不是observe

    还有一个错字,input$STATE_NAME应该是input$selectstate

    下面是一个最小的工作示例:

    library(shiny)
    
    #Create your dataframe (runs once when app starts)
    counties <- data.frame(
        STATE_NAME = c('Alabama', 'Alabama', 'Arkansas'),
        NAMELSAD   = c('Clay County', 'Marengo County', 'Scott County'),
        stringsAsFactors = FALSE
    )
    
    #UI
    ui <- fluidPage(
    
        selectInput(inputId = "selectstate",  label = "Select State",  choices = (counties$STATE_NAME)),
        selectInput(inputId = "selectcounty", label = "Select County", choices = NULL)
    
    )
    
    #Server
    server <- function(input, output, session) {
    
        #Runs when input$selectstate changes
        observeEvent(input$selectstate, {
    
            updateSelectInput(session, inputId = "selectcounty", label = "Select County", choices = counties[counties$STATE_NAME == input$selectstate,]$NAMELSAD)
    
        })
    
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 非常感谢。发布后我才意识到我的错误!就这么一个小错误我花了 5 个小时!
    【解决方案3】:

    天哪,我很愚蠢。

    这是因为我在观察事件中将输入命名错误。

    让它工作。答:

      observe({
            updateSelectInput(session, "selectcounty", "Select County", 
                              choices = counties$NAMELSAD[counties$STATE_NAME==input$selectstate])
        })
    

    【讨论】:

      猜你喜欢
      • 2017-04-05
      • 2019-12-25
      • 2017-09-16
      • 2021-12-03
      • 2016-07-03
      • 2016-07-25
      • 2018-11-28
      • 2019-03-05
      • 2020-12-20
      相关资源
      最近更新 更多