Shiny 应用程序中的行索引不允许 NA(传单地图)

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

我收到一个奇怪的错误NAs not allowed in row index

我的错误传统上是介于子集化/反应性/输出之间

样本数据在这里

形状文件在这里

# 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)

所以,要么我以错误的方式进行子集化。或者搞乱反应性。或者我的输出是错误的。

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

# 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)

还更新了形状文件

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

使用

View(data_pg_df)
,你会发现
data_pg_df
是一个 S4 类,要子集 S4,你需要使用
@
。另外,我认为您在合并方面遇到了问题,正如您在
View(data_pg_df@data)
中看到的那样。这会起作用

data_pg_df@data[data_pg_df@data$Region == input$regionInput, ]

警告:

pal(selectedRegion()) 中的警告: 有些值超出色标范围,将被视为 NA


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, ]
})
© www.soinside.com 2019 - 2024. All rights reserved.