【问题标题】:R Shiny updateSelectizeInput throwing error and not updatingR Shiny updateSelectizeInput 抛出错误并且不更新
【发布时间】:2020-04-07 14:20:35
【问题描述】:

我正在尝试创建两个闪亮的下拉菜单 - 一个是州列表,然后过滤第二个下拉列表,其中列出了所选州的所有县。以下代码有效,但是当您更改状态时县不会更新。

用户界面

pageWithSidebar(
 headerPanel("My chart"),
 sidebarPanel(
  uiOutput("sel_state"),
  uiOutput("sel_county")
 ),
 mainPanel(
  tableOutput("table")
 )
)

服务器

function(input, output, session) {

  output$sel_state <- renderUI({
   selectizeInput('state', 'Select a State', choices=c("Choose One" = "", state_list))
  })

  output$sel_county <- renderUI({
   county_list <- reactive({
    df %>%
     filter(state == input$state) %>%
     pull(county) %>%
     sort() %>%
     as.character()
   })
    selectizeInput('county', 'Select a County', choices=c("Choose One" = "", county_list()))
  })

  tab <- reactive({
   df %>%
    filter(state == input$state) %>%
    filter(county == input$county)
  })

  output$table <- renderTable({
   tab()
  })
 }

我尝试将县下拉列表中的“selectizeInput”替换为以下“updateSelectizeInput”,但不断收到以下错误

新服务器行

updateSelectizeInput(session, 'county', 'Select a County', choices=c("Choose One" = "", county_list()))

错误:无法将类型“环境”强制转换为“字符”类型的向量

创建数据框的代码 - 县列表还不是正确的列表

    x <- getURL("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-counties.csv")
    csv <- read.csv(text=x)
    df <- as.data.frame(csv)
    state_list <- c(levels(df$state))
    county_list <- subset(df, select = c(3,2))

输入输出

dput(head(cl))
structure(list(state = c("Washington", "Washington", "Washington", 
"Illinois", "Washington", "California"), county = c("Snohomish", 
"Snohomish", "Snohomish", "Cook", "Snohomish", "Orange")), row.names = 
c(NA, 6L), class = "data.frame")

【问题讨论】:

  • 不要将 selectizeInput 替换为 updateSelectizeInput。将其添加到服务器。 selectizeInput 进行 id =county 的输入,updateSelectizeInput 可以更新 id =county 的输入。
  • getURL 对我不起作用。当我运行 url.exists() 时,它返回“FALSE”。您可以尝试“dput(df)”并将输出复制/粘贴到问题中吗?如本答案所述:stackoverflow.com/a/49995752/11437205
  • 完成!我只是使用 head() 提取,因为它是一大组数据我还修复了 URL,它只是在问题中的格式奇怪,如果你愿意,你现在应该可以重用它。
  • 谢谢 - 更新的答案。

标签: r shiny


【解决方案1】:

我没有您的州/县信息(或其结构),但这个小示例完成了我认为您所描述的内容。注意事项:

您需要首先使用 selectizeInput 制作选择输入 UI 元素,然后您可以使用 updateSelectizeInput 更新其内容。所以你需要这两个函数,但是对于我创建的这个小代表,虽然不需要 updateSelectizeInput,所以我可能误解了你想要做的事情。

在此示例中,县 selectInput 接收一个反应性对象作为其选择输入,基于用户状态选择对县列表进行子集化,因此县选择输入将随着用户在状态之间切换而更新。

我还使用 conditionalPanel 包装了县输入 - 只有当用户使用 state selectInput 选择了一个州时,此输入才会显示。

UPDATE* 添加 'req(length(input$state) > 0)' 以便在选择状态之前反应不会在服务器中执行

library(shiny)
library(tidyverse)

ui <- fluidPage(


    pageWithSidebar(
        headerPanel("My chart"),
        sidebarPanel(
            uiOutput("sel_state"),
            conditionalPanel(condition = "input.state.length > 0",

            uiOutput("sel_county")
            )
        ),
        mainPanel(

        )
    )
)


server <- function(input, output) {

    #create example of data structure
   df <- structure(list(state = c("Washington", "Washington", "Washington", 
                         "Illinois", "Washington", "California"), county = c("Snohomish", 
                                                                             "Snohomish", "Snohomish", "Cook", "Snohomish", "Orange")), row.names = 
              c(NA, 6L), class = "data.frame")
   #pull out unique state names
    state_list <- as.character(unique(df$state))

    #filter for counties only in desired state and pull column to vector 
    selected_state_counties <- reactive({
    req(length(input$state) > 0)
        df %>% 
            filter(state == input$state) %>% 
            pull(county) %>% 
            as.character()

    })

output$sel_state <- renderUI({
        selectizeInput('state', 'Select a State', choices=c("Choose One" = "", state_list))
    })

output$sel_county <- renderUI({
        selectizeInput('county', 'Select a County', choices=c("Choose One" = "", selected_state_counties()))
    })


}

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

【讨论】:

  • 这太棒了!我唯一遇到的是数据结构 - 正如你所说。我将州和县放在一个数据框中,因此制作 state_list 很容易,但我正试图将我的两列数据框变成一个county_list,就像你拥有的那样。我的数据框目前是一列州,然后是一列县(因此有多次列出的州)
  • 如果您能够添加数据结构的小示例,我将尝试重新设计解决方案。如果这样做需要参考:stackoverflow.com/help/minimal-reproducible-example
  • 我添加了代码以将数据提取到原始问题中,因为我很笨,无法弄清楚如何正确格式化评论:)
  • 刚刚根据您在此处的第一条评论更新了答案
  • 另外,将 'req(length(input$state) > 0)' 添加到 selected_state_counties 反应式中。我注意到 R 控制台中出现了一个小警告。我再次更新了答案。
猜你喜欢
  • 2013-07-16
  • 2016-03-29
  • 1970-01-01
  • 2021-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多