在过去的几个小时里,我一直在使用 if 语句、observeEvent 和 leafletProxy 的形式进行编码,但我似乎无法让它工作。寿命表是预期寿命数据。它是三列,COUNTY、男性、女性。我似乎无法让单选按钮工作,它总是停留在男性位置。
基本上,我希望我的传单地图根据单选按钮的输入给出不同的布局。要么获取男性和女性的预期寿命,要么获取弹出窗口。
library(shiny)
library(leaflet)
library(tigris)
library(dplyr)
lnd <- counties(state = 'IN', cb = TRUE, resolution = '20m')
#Read life table
life <- read.table("life.txt",sep="\t", header=TRUE)
lnd@data <- left_join(lnd@data, life, by = c('NAME' = 'county'))
popup <- paste0("County name: ", lnd$NAME, "<br>", "Life Expectancy: ", round(lnd$male,2))
popup2 <- paste0("County name: ", lnd$NAME, "<br>", "Life Expectancy: ", round(lnd$female,2))
ui <- shinyUI(fluidPage(#theme = "bootstrap.css",
titlePanel("Life Exptectancy in Indiana Counties: "),
h3("A Spatial Analysis Project"),
br(),
sidebarLayout(position = "right",
sidebarPanel(
tags$b("Life Expectancy in Indiana, U.S.A."),
hr(),
radioButtons("gender", "Gender:",c("Male"="Male","Female"="Female")),
hr()
),
mainPanel(
leafletOutput("map")
)
)
))
server <- function(input, output) {
output$map <- renderLeaflet({
leaflet(lnd) %>%
addPolygons() %>%
addProviderTiles(providers$CartoDB.Positron)
})
observeEvent (input$gender == "Male", {
leafletProxy("map", data=lnd) %>%
clearShapes() %>%
addPolygons(stroke = FALSE, smoothFactor = 0.2, fillOpacity = 1, popup = popup)
})
observeEvent (input$gender == "Female", {
leafletProxy("map", data=lnd) %>%
clearShapes() %>%
addPolygons(stroke = FALSE, smoothFactor = 0.2, fillOpacity = 1, popup = popup2)
})
}
shinyApp(ui, server)
这是我的应用程序的照片应用程序保持蓝色,无论我点击女性还是男性,都会保留相同的男性数据。我知道这与 leafletProxy 有关,但无法弄清楚
observeEvent
的两个input$gender
会产生问题,因为只有第一个observeEvent
会被执行。如果您仅使用一个 observeEvent
更改服务器并将 if 和 else 条件放入其中,它应该可以工作。由于我没有您的数据life.txt
,我用一些随机值生成了预期寿命。
library(shiny)
library(leaflet)
library(tigris)
library(dplyr)
dat <- data.frame(long = rnorm(40) * 2 + 13, lat = rnorm(40) + 48)
#Read life table
life <- read.table("life.txt",sep="\t", header=TRUE)
#lnd@data <- left_join(lnd@data, life, by = c('NAME' = 'county'))
lnd@data <- cbind(lnd@data, male = rnorm(nrow(lnd@data), 50, 10), female = rnorm(nrow(lnd@data), 40, 5))
popup <- paste0("County name: ", lnd$NAME, "<br>", "Life Expectancy: ", round(lnd$male,2))
popup2 <- paste0("County name: ", lnd$NAME, "<br>", "Life Expectancy: ", round(lnd$female,2))
ui <- shinyUI(fluidPage(#theme = "bootstrap.css",
titlePanel("Life Exptectancy in Indiana Counties: "),
h3("A Spatial Analysis Project"),
br(),
sidebarLayout(position = "right",
sidebarPanel(
tags$b("Life Expectancy in Indiana, U.S.A."),
hr(),
radioButtons("gender", "Gender:",c("Male"="Male","Female"="Female")),
hr()
),
mainPanel(
leafletOutput("map")
)
)
))
server <- function(input, output) {
output$map <- renderLeaflet({
leaflet(lnd) %>%
addPolygons() %>%
addProviderTiles(providers$CartoDB.Positron)
})
observeEvent (input$gender , {
if(input$gender == "Male"){
leafletProxy("map", data=lnd) %>%
clearShapes() %>%
addPolygons(stroke = FALSE, smoothFactor = 0.2, fillOpacity = 1, popup = popup)
}else
{
leafletProxy("map", data=lnd) %>%
clearShapes() %>%
addPolygons(stroke = FALSE, smoothFactor = 0.2, fillOpacity = 1, popup = popup2)
}
})
}
shinyApp(ui, server)
希望有帮助!
您需要为用户输入创建一个反应数据集。您在代码中使用相同的数据集
lnd
。
我不知道你的数据是如何构造的,因为我没有你的数据,我希望在这个例子中能够理解。
# Data filtered male
lndMale <- reactive({
lnd[lnd$gender == input$gender, ]
})
# Data filtered female
lndFemale <- reactive({
lnd[lnd$gender == input$gender, ]
})
然后您可以在需要的地方使用反应式数据集。
server <- function(input, output) {
output$map <- renderLeaflet({
leaflet(lnd) %>%
addPolygons() %>%
addProviderTiles(providers$CartoDB.Positron)
})
observeEvent (input$gender == "Male", {
leafletProxy("map", data=lndMale()) %>%
clearShapes() %>%
addPolygons(stroke = FALSE, smoothFactor = 0.2, fillOpacity = 1, popup = popup)
})
observeEvent (input$gender == "Female", {
leafletProxy("map", data=lndFemale()) %>%
clearShapes() %>%
addPolygons(stroke = FALSE, smoothFactor = 0.2, fillOpacity = 1, popup = popup2)
})
}
也许你必须更改部分代码,并在某处使用
isolate
,我没有数据集。