带有ggplot贴图的闪亮应用程序 - 多边形颜色与用户输入不匹配

时间:2015-01-01 10:51:03

标签: r ggplot2 shiny

我正在尝试使用Shiny和ggplot2构建一个简单的地图应用程序。它的工作原理如下:

  • 用户选择国家/地区
  • 该应用加载形状文件并为adm1国家/地区区域提供输入字段列表
  • 用户输入每个区域的数值(字段最初用随机值填充)
  • 来自输入字段的所有值都在向量中收集,合并到地图数据并作为fill函数的ggplot()参数给出

问题是ggplot似乎没有正确解释每个区域的输入值。此外,通过UI修改输入值时,地图上的颜色不会更改。我相信indicator参数的fill向量没有被正确解释/传递。

感谢您的建议。

注意:在下面的代码中,shapefile源自UCDavis网站以获得再现性。我通常将它们存储在本地。

ui.R

shinyUI(fluidPage(
sidebarLayout(

 sidebarPanel(      
  selectInput("ctry", "Country:",
              list("Burkina Faso"="BFA","Ethiopia"="ETH","Ghana"="GHA",
                   "Kenya"="KEN","Malawi"="MWI","Mali"="MLI"), selected="ETH"), 
  uiOutput("sliders")

 ),   
 mainPanel(
   plotOutput('map', width = "100%")
 )
)
)
)

server.R

x<-c("ggplot2","sp","maptools","rgdal","rgeos","scales","grid","gridExtra","plyr") 
lapply(x, require, character.only=T)
rm(x)

shinyServer(function(input, output, session) { 

 gadm<-reactive ({
 load(paste0("http://biogeo.ucdavis.edu/data/gadm2/R/",input$ctry,"_adm1.RData")) #load country shapefile
 gadm@data$id <- rownames(gadm@data)
 gadm.df      <- fortify(gadm)
 gadm.df      <- join(gadm.df,gadm@data, by="id")
 return(gadm.df)
 })

 output$sliders <- renderUI({
   regions    <- unique(gadm()$NAME_1)  #get list of region names
   numRegions <- length(regions)        #get number of regions
   lapply(1:numRegions, function(i) {   #generate 1 input field per region
   numericInput(paste0("reg",i),        #with random values
               label = regions[i], value = round(runif(1, 1.0, 7.5),2),
               step=0.1) })
 })

 mapdata<- reactive({
   regions    <- unique(gadm()$NAME_1)  #get list of region names
   numRegions <- length(regions)        #get number of regions
   indicator  <- input$reg1             #initate vector with first value of user inputs
   for (i in 2:numRegions)(
   indicator<-c(indicator,eval(paste0("input$reg",i)))  #collect all user inputs values in a vector
   )
   indicator <- as.data.frame(t(rbind(indicator,regions)))#attribute region name to user input values
   colnames(indicator)<-c("indicator","NAME_1")
   merge(gadm(), indicator, by="NAME_1")                  #merge it with map data
   })

   themap <- function() {    
     ggplot() + geom_polygon(data=mapdata(), 
                        aes(x=long, y=lat, group=group, fill=as.numeric(indicator) )) + 
     scale_fill_gradient("test",low="#99d8c9", high="#00441b") +
     geom_path(data=mapdata(), 
            aes(x=long, y=lat, group=group), color='grey', size=0.15, alpha=0.6) + 
     coord_map()
   }

   output$map<-renderPlot({  themap()  }, height = 700 )

})

1 个答案:

答案 0 :(得分:2)

问题是表达式eval(paste0("input$reg",i))不是返回输入$ regN的内容,而是字符串&#34;输入$ regN&#34;。您可以使用双括号获取所需的输入元素:

x<-c("ggplot2","sp","maptools","rgdal","rgeos","scales","grid","gridExtra","plyr") 
lapply(x, require, character.only=T)
rm(x)

shinyServer(function(input, output, session) { 

 gadm<-reactive ({
 load(paste0(input$ctry,"_adm1.RData")) #load country shapefile
 gadm@data$id <- rownames(gadm@data)
 gadm.df      <- fortify(gadm)
 gadm.df      <- join(gadm.df,gadm@data, by="id")
 return(gadm.df)
 })

 output$sliders <- renderUI({
   regions    <- unique(gadm()$NAME_1)  #get list of region names
   numRegions <- length(regions)        #get number of regions
   lapply(1:numRegions, function(i) {   #generate 1 input field per region
   numericInput(paste0("reg",i),        #with random values
               label = regions[i], value = round(runif(1, 1.0, 7.5),2),
               step=0.1) })
 })

 mapdata<- reactive({
   regions    <- unique(gadm()$NAME_1)  #get list of region names
   numRegions <- length(regions)        #get number of regions

   indicator  <- sapply(seq_along(regions),function(i) input[[paste0('reg',i)]])
   if (any(is.null(unlist(indicator)))) return()
   indicator <- as.data.frame(cbind(indicator,regions))#attribute region name to user input values

   colnames(indicator)<-c("indicator","NAME_1")
   merge(gadm(), indicator, by="NAME_1")                  #merge it with map data
   })

   themap <- function() {    
     d <- mapdata()
     if (is.null(d)) return()
     ggplot() + geom_polygon(data=d, 
                        aes(x=long, y=lat, group=group, fill=as.numeric(indicator) )) + 
     scale_fill_gradient("test",low="#99d8c9", high="#00441b") +
     geom_path(data=d, 
            aes(x=long, y=lat, group=group), color='grey', size=0.15, alpha=0.6) + 
     coord_map()
   }

   output$map<-renderPlot({  themap()  }, height = 700 )

})

请注意,mapdata()可能会在输出$ sliders之前调用,因此在mapdata()评估时可能不存在输入$ regN。为了避免相关问题,我在上面的代码中插入了几个检查。