【问题标题】:How to update a numeric value in shiny app based on selected choice from dropdown如何根据下拉列表中的选择更新闪亮应用程序中的数值
【发布时间】:2022-02-25 13:48:31
【问题描述】:

我有一个闪亮的应用程序,用户界面中的第一个输入是从下拉列表中选择一个国家/地区。然后,我希望根据选择的国家/地区来更新其他数字输入(即人口规模)。数据存储为数据框,第一列列出国家,第二列列出每个国家的人口规模。输入应从数据框中检索此数据。我尝试使用 updateNumericInput,但是我在应用程序中的人口规模输入字段显示为空白,并且无法填充正确的值。我该如何处理?

这是我的代码的较短示例:

ui <- dashboardPage(
dashboardHeader(title = "Shiny app"),
dashboardSidebar(
selectInput("country", "Country", choices = dataframe$country_name),
numericInput("pop_size", "Population size", value = 0))

server <- function(input, output, session) {
observeEvent(input$country,{
updateNumericInput(session, "pop_size", value = dataframe$population)
})
}

我也为服务器尝试过这个,但仍然没有运气:

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

reactive({
#filter dataframe to selected country
data <- dataframe
data <- data[data$country_name == input$country,]

updateNumericInput(session, "pop_size", value = data$population)

data
})
}

【问题讨论】:

  • 如果您包含一个简单的reproducible example,其中包含可用于测试和验证可能解决方案的示例输入和所需输出,则更容易为您提供帮助。你的updateNumericInput 代码到底是什么样的?
  • 抱歉,我刚刚编辑了我的问题以包含我的示例代码。希望这能澄清我的问题。

标签: r shiny


【解决方案1】:
observeEvent(input$country,{
  updateNumericInput(
    session, "pop_size", value = dataframe$population[dataframe$Country == input$country]
  )
})

【讨论】:

    猜你喜欢
    • 2015-08-03
    • 2020-05-11
    • 2021-04-18
    • 2019-12-23
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    • 2021-08-31
    • 2018-10-17
    相关资源
    最近更新 更多