如何根据从下拉列表中选择的选项更新闪亮应用程序中的数值
How to update a numeric value in shiny app based on selected choice from dropdown
我有一个闪亮的应用程序,其中 UI 中的第一个输入是从下拉列表中选择一个国家/地区。然后我希望其他数字输入(即人口规模)根据所选国家/地区进行更新。数据存储为数据框,第一列列出国家,第二列列出每个国家的人口规模。输入应从数据框中检索此数据。我曾尝试使用 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
})
}
observeEvent(input$country,{
updateNumericInput(
session, "pop_size", value = dataframe$population[dataframe$Country == input$country]
)
})
我有一个闪亮的应用程序,其中 UI 中的第一个输入是从下拉列表中选择一个国家/地区。然后我希望其他数字输入(即人口规模)根据所选国家/地区进行更新。数据存储为数据框,第一列列出国家,第二列列出每个国家的人口规模。输入应从数据框中检索此数据。我曾尝试使用 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
})
}
observeEvent(input$country,{
updateNumericInput(
session, "pop_size", value = dataframe$population[dataframe$Country == input$country]
)
})