R 闪亮的等值线地图

问题描述 投票:0回答:1

我正在创建闪亮的应用程序。 R 可以读取我的数据集。使用命令 myData <- read.csv("myData.csv"). however shinyServer file cannot read my data. and list no observation. Could you guys help me what is the problem? The Shinyapp provides interactive visulization for production of raw material in the world since 1900 to 2010 for every 10 years. Also I keep getting this error: "ERROR: 'breaks' are not unique"

代码在这里:

shinyUI(fluidPage(
 
  checkboxInput("type", "Please Select production type:",
                     c("Aluminium", "Gold", 
                       "Iron", "Silver", "Zinc")
  ),
  sliderInput("year","Choose a Year", 
              min = 1910,
              max = 2010,
              value= 2010),
  checkboxInput("Economy", "Please Select Economy Factor:", 
                     c("Income Inequallity", "labourers Real Wage", "GDP", "Inflation")),

  plotOutput("thisPlot"),
  leafletOutput("myMap")
)
)

闪亮服务器:

myData <- read.csv("myData.csv")
shinyServer<- function(input,output){
  
  output$myMap <- renderLeaflet({
    temp <- which(myData$type == input$type &
                    myData$year == input$year)
    myData <- myData[temp,]
        
    pal <- colorQuantile("YlGn", myData$production, n = 9)
    country_popup <- paste0("<strong>Estado: </strong>", myData$Country)
    
  leaflet(data = myData) %>% 
      setView(46.227638, 2.213749, zoom = 2) %>% 
      addTiles() %>% 
     addPolygons( lng = ~myData$Long, lat = ~myData$Lat, 
                 fillColor = ~pal(myData$production), 
                     fillOpacity = 0.8, 
                     color = "#BDBDC3", 
                     weight = 1, 
                     popup = country_popup)
   
   
  })
}

数据为:

Names = c("id", 
"Country", "type", "year", "production", "GDP", "Income", "Inflation", 
"Laborer", "Lat", "Long"), class = "data.frame", row.names = c(NA, 
-10670L))
head(myData)
  id Country type year production GDP   Income Inflation   Laborer      Lat  
Long

 1  1  Guyana Gold 1910   0.000000   0 42.43048         0 154.45527 4.860416 
  -58.9301

看起来确实读取了数据,但没有显示。我在创建分区统计图时遇到问题。现在它在我的闪亮中不起作用。

r shiny r-leaflet choropleth
1个回答
1
投票

是的,传单很挑剔。我不需要做太多改变,你几乎已经完成了。主要问题之一是您的过滤器通常会产生一个空数据框,这导致标记不显示(当然)。

这个空数据帧问题也是

"ERROR: 'breaks' are not unique"
消息的原因,因为
colorQuantile
为其
domain
参数获取空输入,这意味着它正在执行空
quantile
,并且所有中断都为零,因此“不独特”。高度倾斜的数据也可能发生这种情况。在这种情况下,您应该避免调用它 - 也许可以回退到
colorBin
,尽管检测到这一点可能有点复杂。

进行了以下更改。

  • 添加了一些虚假数据。
  • addPolygons
    更改为
    addCircleMarkers
    ,因为
    addPolygons
    用于添加您指定的任意形状。
  • 将您的
    checkBoxInput
    更改为
    checkBoxGroupInput
    ,因为您不需要复选框,您想要一组复选框。
  • 更改了过滤子句以使用
    myData$type %in% input$type
    而不是
    myData$type == input$type
    ,因为您可能想要成员资格。
  • 截断了
    input$year
    值,因为它可能不会返回整数,但您的
    year
    值绝对是整数。
  • 将边框颜色更改为
    "black"
    ,以便您可以在圆圈上看到它。
  • 请注意,悬停时不会出现
    popup
    ,您必须单击圆圈。
  • 删除了标记输入上的
    myData
    ,因为您在
    leaflet
    调用中指定了它。
  • 注释掉了
    plotOutput
    ,因为我不知道你想绘制什么。

这是代码 - 这应该可以帮助您开始:

library(shiny)
library(leaflet)

# fake-up some data
n <- 10000
countrylist <- c("Guyana","Venezuela","Columbia")
typelist <- c("Aluminium", "Gold","Iron", "Silver", "Zinc")
types <- sample(typelist,n,replace=T)
cntrs <- sample(countrylist,n,replace=T)
lat <- 2.2 + 50*runif(n)
long <- -46 + 50*runif(n)
year <- sample(1910:2010,n,replace=T)
prd <- 100*runif(n)


myData <- data.frame(Country=cntrs,type=types,year=year,production=prd,Long=long,Lat=lat)

u <- shinyUI(fluidPage(

  checkboxGroupInput("type", "Please Select production type:",
                c("Aluminium", "Gold","Iron", "Silver", "Zinc"),
                selected=c("Gold","Silver")
  ),
  sliderInput("year","Choose a Year", 
              min = 1910,
              max = 2010,
              value= 2010),
  checkboxGroupInput("Economy", "Please Select Economy Factor:", 
                c("Income Inequallity", "labourers Real Wage", "GDP", "Inflation")),
 # plotOutput("thisPlot"), 
  leafletOutput("myMap")
)
)

s <- function(input,output){

  output$myMap <- renderLeaflet({
    temp <- which(myData$type %in% input$type &
                    myData$year == trunc(input$year))
    print(nrow(myData))
    myData <- myData[temp,]
    print(nrow(myData))

    pal <- colorQuantile("YlGn", myData$production, n = 9)
    country_popup <- paste0("<strong>Estado: </strong>", myData$Country)

    leaflet(data = myData) %>% 
      setView(-46.227638, 2.213749, zoom = 2) %>% 
      addTiles() %>% 
      addCircleMarkers( lng = ~Long, lat = ~Lat, 
                   fillColor = ~pal(myData$production), 
                   radius = 6, # pixels 
                   fillOpacity = 0.8, 
                   color = "black", 
                   weight = 1, 
                   popup = country_popup)
  })
}
shinyApp(u,s)

这就是结果: enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.