在有光泽的应用程序(传单地图)的行索引中不允许使用NA

时间:2018-08-05 06:01:43

标签: r shiny leaflet

人们,关于简单的Shiny应用程序,真的很简单。

我收到一个奇怪的错误行索引中不允许使用NAs

我的错误通常是在子集/反应/输出之间的某个地方

Sample data is here

Shapefile is here

# Load packages
library(shiny)
library(rgdal)
library(raster)
library(leaflet)
library(sp)
library(geojsonio)
library(RColorBrewer)
library(jsonlite)
library(shinythemes)

# Global code

# Read file on a local machine
data_pg <- read.csv("pg.csv", header = TRUE, stringsAsFactors = FALSE)

# Read a shapefile
countries <- readOGR(".","ne_50m_admin_0_countries")

# Merge data
data_pg_df <- merge(countries, data_pg, by.x = "NAME", by.y = "Country", duplicateGeoms = TRUE)


# Color palette
pal <- colorFactor(
palette = 'Dark2',
domain = data_pg_df$Region
)

# UI code

ui <- fluidPage(theme = shinytheme("united"),
            titlePanel("PG Map"),
            sidebarLayout(
              sidebarPanel(
                selectInput("regionInput", "Region",
                            choices = c("Choose region", 
                                        "Africa",
                                        "Asia",
                                        "Latin America",
                                        "North America",
                                        "Europe"),
                            selected = "Choose region"),
                selectInput("pgInput", "Select PG",
                            choices = c("Choose PG", 
                                        "PG 1",
                                        "PG 2",
                                        "PG 3"),
                            selected = "Choose PG")
              ),

              mainPanel(

                # Output
                tabsetPanel(type = "tabs",
                            tabPanel("Map", leafletOutput(outputId = 'map', height = 700)) #,
                            # tabPanel("Chart",  plotOutput("chart")),
                            # tabPanel("Table",  tableOutput("table"))
   )
  )
 )
)

#  Server
server <- function(input, output) {

selectedRegion <- reactive({
  data_pg_df[data_pg_df$Region == input$regionInput, ] 
})

output$map <- renderLeaflet({
  leaflet(data_pg_df) %>%
    addProviderTiles(providers$Stamen.Toner) %>%
    addPolygons(stroke = FALSE, 
                fillOpacity = 0.75, 
                color = pal(selectedRegion()), weight = 1) 

})



}

shinyApp(ui = ui, server = server)

因此,我要么以错误的方式子集。或搞乱反应性。否则我的输出是错误的。

我更新了代码,但错误仍然存​​在NA

# Global code

# Read file on a local machine
data_pg <- read.csv("pg.csv", header = TRUE, stringsAsFactors = FALSE)

# Read a shapefile
countries <- readOGR(".","ne_50m_admin_0_countries")

# Merge data
data_pg_df <- merge(countries, data_pg, by.x = "name", by.y = "Country", 
duplicateGeoms = TRUE)


# UI code

ui <- fluidPage(theme = shinytheme("united"),
            titlePanel("PG Map"),
            sidebarLayout(
              sidebarPanel(
                selectInput("regionInput", "Region",
                            choices = c("Choose region", 
                                        "Africa",
                                        "Asia",
                                        "Latin America",
                                        "North America",
                                        "Europe"),
                            selected = "Choose region"),
                selectInput("pgInput", "Select PG",
                            choices = c("Choose PG", 
                                        "PG 1",
                                        "PG 2",
                                        "PG 3"),
                            selected = "Choose PG")
              ),

              mainPanel(

                # Output
                tabsetPanel(type = "tabs",
                            tabPanel("Map", leafletOutput(outputId = 'map', height = 700)) #,
                            # tabPanel("Chart",  plotOutput("chart")),
                            # tabPanel("Table",  tableOutput("table"))
  )
  )
  )
  )

#  Server
server <- function(input, output) {

output$map <- renderLeaflet({
leaflet(data_pg_df) %>% 
  addProviderTiles(providers$Stamen.TonerLite) %>% 
  setView(11.0670977,0.912484, zoom = 4) 

})  

selectedRegion <- reactive({
data_pg_df[data_pg_df$Region == input$regionInput, ] 
})

observe({
state_popup <- paste0("<strong>Region: </strong>", 
                      selectedRegion()$Region, 
                      "<br><strong> Client name: </strong>", 
                      selectedRegion()$Client.name,
                      "<br><strong> Country: </strong>", 
                      selectedRegion()$Country,
                      "<br><strong> PG: </strong>", 
                      selectedRegion()$PG)


 leafletProxy("map", data = selectedRegion()) %>%
  clearShapes() %>%
  addPolygons(fillColor =  "blue",
              popup = state_popup,
              color = "#BDBDC3",
              fillOpacity = 0.5,
              weight = 1 
  )
 })

}

shinyApp(ui = ui, server = server)

Also updated shapefiles

2 个答案:

答案 0 :(得分:1)

使用E'\\x4AC3A1F218E1ED92AB0B3966C3E99CC5BD8419B4A91D504F85AE7621525F305A...' UPDATE table_name SET byte_info = E'\\x4AC3A1F218E1ED92AB0B3966C3E99C...' WHERE id = 1; E'J\\303\\241\\362\\030\\341\\355\\222\\253\\0139f\\303\\351\\234\\30...'::bytea UPDATE table_name SET byte_info = E'J\\303\\241\\362\\030\\341\\355\\...'::bytea WHERE id = 1; ,您会发现View(data_pg_df)是S4类,要成为S4的子集,您需要使用data_pg_df。另外,我认为您在合并方面遇到问题,如@所示。这将起作用

View(data_pg_df@data)

有警告:

  

pal(selectedRegion())中的警告:     一些值不在色标范围内,将被视为NA

答案 1 :(得分:0)

在R社区的帮助下完美解决。 enter image description here

治疗NA是关键:

“”如果您查看data_pg_df $ Region,您会看到它具有许多NA:

> data_pg_df$Region
[1] NA              NA              NA              NA              NA             
[6] NA              NA              NA              "Latin America" NA             
[11] NA              NA              NA              NA              NA             
[16] NA              NA              NA              NA              NA             
[21] NA              NA              NA              NA              NA             
[26] NA              NA              NA              NA              NA             
[31] NA              NA              NA              NA              NA             
[36] NA              NA              NA              "North America" NA             
[41] NA              "Asia"          NA              NA              NA             
[46] NA              NA              NA              NA              NA             
[51] NA              NA              NA              NA              NA             
[56] NA              NA              "Europe"        NA              NA             
[61] NA              NA              NA              NA              NA          
...   

使用data_pg_df $ Region == input $ regionInput,您正在将其与一个值进行比较,例如     “亚洲”。在R中,如果将值与NA进行比较,则得到NA。

> data_pg_df$Region == "Asia"
[1]    NA    NA    NA    NA    NA    NA    NA    NA FALSE    NA    NA    NA    NA    
NA    NA
[16]    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    
NA    NA
[31]    NA    NA    NA    NA    NA    NA    NA    NA FALSE    NA    NA  TRUE    NA    
NA    NA
[46]    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA FALSE    
NA    NA
[61]    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    
NA    NA
...

并且您不能使用NA索引到data_pg_df对象。

您需要以某种方式处理NA。我不确定是否适合     您的数据集,但看起来您可能只想保留==的行     解析为TRUE,并删除将导致NA和FALSE的行。去做这个,     您可以使用is.na()添加支票:

!is.na(data_pg_df$Region) & data_pg_df$Region == "Asia"
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE 
FALSE FALSE
[16] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE 
FALSE FALSE
[31] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE 
FALSE FALSE
[46] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE 
FALSE FALSE
[61] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE 
FALSE FALSE
...

那么您将拥有:

selectedRegion <- reactive({
data_pg_df[!is.na(data_pg_df$Region) & data_pg_df$Region == input$regionInput, ]
})