我在R shine中使用conditionalPanel有问题。基本上,第一个条件(条件=" input.year == 2011")正在工作,这给了我正确的叶子图。但是,当我使用第二个条件(条件=" input.year == 2012")时,我得到一张空地图!关于如何摆脱这个问题的任何建议?
这是我的服务器和ui:
shinyUI(fluidPage(
# Title
titlePanel("Greater London Area, Satellite and UK AIR data"),
sidebarLayout(
sidebarPanel(
selectInput("year", "Choose a year", c("YEAR 2011" = "2011", "YEAR 2012" = "2012")),
conditionalPanel(
condition = "input.year == 2011",
selectInput("variable", "Choose a Layer", c("PM25_SAT" = "pm25_mean"))),
conditionalPanel(
condition = "input.year == 2012",
selectInput("variable", "Choose a Layer", c("URB_COVER" = "URB_Cover_nc")))
),
# Show leaflet map with a text div reporting the selected date and extents
mainPanel(
leafletOutput('myMap', height = 500, width = 800)
)
)))
####### Load data first
PM25_sat <- readOGR(dsn = "Copia.geojson",layer = "OGRGeoJSON")
popup_PM25_sat <- paste0("<strong>PM<sub>2.5</sub> (<font face=symbol>m</font>g/m<sup>3</sup>): </strong>",
PM25_sat$pm25_mean)
#### colors for the GeoJSON layer
qpal_SAT <- colorQuantile("Blues", PM25_sat$pm25_mean, n = 7)
#### colors for the GeoJSON layer legend
pal_SAT <- colorNumeric(
palette = "Blues",
domain = PM25_sat$pm25_mean)
###### Load geotiff file
URB_Cover_nc <- raster::raster("URB_cover.tif")
pal_URB <- colorNumeric(c("#FFFFCC", "#41B6C4","#0C2C84"), getValues(URB_Cover_nc),
na.color = "transparent")
#######################################################################
shinyServer(function(input, output) {
map = leaflet() %>% addTiles() %>% setView(-2, 52.5, 6)
finalMap <- reactive({
PM25_Layer <- input$variable
YEAR <- input$year
if (YEAR == "2011" & PM25_Layer == "pm25_mean")
return(map %>% addPolygons(data = PM25_sat,
stroke = FALSE, smoothFactor = 0.2,
fillOpacity = 0.5,
color = ~ qpal_SAT(pm25_mean),
popup = popup_PM25_sat) %>%
addLegend("bottomright", pal = pal_SAT, values = PM25_sat$pm25_mean,
title = "<br><strong>PM<sub>2.5</sub> (<font face=symbol>m</font>g/m<sup>3</sup>) Sat : </strong>",
labFormat = labelFormat(prefix = ""), opacity = 1))
if (YEAR == "2012" & PM25_Layer == "URB_Cover_nc")
return(map %>% addRasterImage(URB_Cover_nc,
colors = pal_URB,
opacity = 0.6) %>%
addLegend("bottomright",pal = pal_URB, values = getValues(URB_Cover_nc),
title = "<br><strong>PM<sub>2.5</sub> (<font face=symbol>m</font>g/m<sup>3</sup>) URB Cover: </strong>",
labFormat = labelFormat(prefix = ""),
opacity = 0.6))
else return (map)
})
output$myMap = renderLeaflet(finalMap())
})
答案 0 :(得分:0)
好的,我已经解决了这个问题。 我定义了两个变量:&#34; variable_1&#34;和&#34; variable_2&#34;分别为2011年和2012年。