gpt4 book ai didi

r - Shiny 中的 conditionalPanel 导致 selectInput 失败

转载 作者:行者123 更新时间:2023-12-04 10:43:47 26 4
gpt4 key购买 nike

我对为什么我的 selectInput 不起作用感到困惑。

包含 conditionalPanel 后,突然在 selectInput 中将名字从 John Doe 切换为 Joe Blow 不再有效——日期不更新, map 继续灰色。

我不明白为什么会这样。它起作用的唯一方法是如果我单击 no_date 然后返回并更改名称。然后事情就火了。但我无法弄清楚为什么/这是如何被打破的。

最初,一切都很好:

enter image description here

但是当我选择 Joe Blow 时:

enter image description here

注意日期没有改变。

conditionalPanel 发生了一些奇怪的事情,但对于我来说,我被困住了。感谢您阅读本文。

UI.R

library(shiny)
library(ggplot2)
library(dplyr)
library(leaflet)


DF <- data.frame(lon=c(-120.6596156, -87.27751, -119.7725868, -124.2026, -117.1858759),
lat=c(35.2827524, 33.83122, 36.7468422, 41.75575, 34.5008311),
date=c('2014-03-14', '2014-01-11', '2013-11-22', '2012-08-23', '2013-08-23'),
city=c('San Luis Obispo', 'Jasper', 'Fresno', 'Crescent City', 'Apple Valley'),
P1_name=c('John Doe', 'John Doe', 'John Doe', 'John Doe', 'Joe Blow'))

DF[, c('date', 'city', 'P1_name')] <- sapply(DF[, c('date', 'city', 'P1_name')], as.character)


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

output$fdate<-renderUI({
selectInput('dates', 'Select date', choices=DF[which(DF$P1_name == input$person), ]$date, selectize = FALSE)
})

output$map<-renderLeaflet({
DF <- filter(DF, P1_name==input$person, date==input$dates)
output$city <-renderText({c("Location:", DF$city)})


m <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
setView(lng=DF$lon, lat=DF$lat, zoom=5) %>%
addMarkers(lng=DF$lon, lat=DF$lat, popup=DF$city)

})

}

ui <- fluidPage(
titlePanel("Testing Shiny"),
sidebarLayout (
sidebarPanel(

selectInput('person', 'Select person', choices=unique(DF$P1_name), selectize = FALSE),

radioButtons('radio', 'Select method', choices=c('show_date', 'no_date'), selected = NULL, inline = TRUE),
conditionalPanel(
condition = "input.radio == 'show_date'",
uiOutput('fdate')

),

textOutput("city")

),

mainPanel(
leafletOutput('map')
)
))

shinyApp(ui = ui, server = server)

最佳答案

本质上output$fdate应该和'dates'同名

library(shiny)
library(ggplot2)
library(dplyr)
library(leaflet)


DF <- data.frame(lon=c(-120.6596156, -87.27751, -119.7725868, -124.2026, -117.1858759),
lat=c(35.2827524, 33.83122, 36.7468422, 41.75575, 34.5008311),
date=c('2014-03-14', '2014-01-11', '2013-11-22', '2012-08-23', '2013-08-23'),
city=c('San Luis Obispo', 'Jasper', 'Fresno', 'Crescent City', 'Apple Valley'),
P1_name=c('John Doe', 'John Doe', 'John Doe', 'John Doe', 'Joe Blow'))

DF[, c('date', 'city', 'P1_name')] <- sapply(DF[, c('date', 'city', 'P1_name')], as.character)


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

output$dates<-renderUI({
selectInput('dates', 'Select date', choices=DF[which(DF$P1_name == input$person), ]$date, selectize = FALSE)
})

output$map<-renderLeaflet({
validate(
need(!is.null(input$dates),""),
need(!is.null(input$person),"")
)
DF <- filter(DF, P1_name==input$person, date==input$dates)
output$city <-renderText({c("Location:", DF$city)})


m <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
setView(lng=DF$lon, lat=DF$lat, zoom=5) %>%
addMarkers(lng=DF$lon, lat=DF$lat, popup=DF$city)

})

}

ui <- fluidPage(
titlePanel("Testing Shiny"),
sidebarLayout (
sidebarPanel(

selectInput('person', 'Select person', choices=unique(DF$P1_name), selectize = FALSE),

radioButtons('radio', 'Select method', choices=c('show_date', 'no_date'), selected = NULL, inline = TRUE),
conditionalPanel(
condition = "input.radio == 'show_date'",
uiOutput('dates')

),

textOutput("city")

),

mainPanel(
leafletOutput('map')
)
))

shinyApp(ui = ui, server = server)

关于r - Shiny 中的 conditionalPanel 导致 selectInput 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31842437/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com