值[[3L]](cond)出错:尝试获取插槽"数据"来自不是S4对象的对象(类" data.frame")

时间:2018-02-01 17:01:06

标签: r shiny leaflet

我正在尝试在线发布我的闪亮应用程序。但是,我收到以下错误:

trying URL 'https://data.overheid.nl/OpenDataSets/LBM2016/Gemeente2016.zip'
Content type 'application/zip' length 11401535 bytes (10.9 MB)
==================================================
downloaded 10.9 MB

trying URL 'https://data.overheid.nl/OpenDataSets/LBM2016/Gemeente.zip'
Content type 'application/zip' length 502231 bytes (490 KB)
==================================================
downloaded 490 KB

Error in value[[3L]](cond) : 
trying to get slot "data" from an object (class "data.frame") that is not an S4 object 
Calls: local ... tryCatch -> tryCatchList -> tryCatchOne -> <Anonymous>
Execution halted

我在本地运行应用程序时没有任何问题。 有人知道这里出了什么问题吗?如果我理解正确,它与一个导致错误的数据框的对象有关

这是我的代码:

# Set up working environment
source("R/setup_workenv.R")

# Download and extract source data
source("R/source_data.R")

# Preprocess data
source("R/preprocessing.R")

# Call required libraries and open function sources
library(shiny)
library(leaflet)
library(readxl)
library(raster)
library(rgdal)
library(sp)
library(sf)
library(DT)

# User interface for the shiny application
ui <- navbarPage("Navigation bar", id="nav",
             tabPanel("Interactive map",
                      fluidPage(
                        # Assign title for the sidebar
                        titlePanel("Quality-of-life-o-meter of The Netherlands"),

                        sidebarLayout(
                          sidebarPanel(
                            h3("Dimensions"),
                            h6("If preferred, assign a weight value per dimension and press the 'Search!' button below."),
                            # Assign titles for slider bars   
                            sliderInput("housingslider", 
                                        label = h4("Housing"), 
                                        min = 1, 
                                        max = 5, 
                                        value = 1),
                            sliderInput("populationslider", 
                                        label = h4("Population"), 
                                        min = 1, 
                                        max = 5, 
                                        value = 1),
                            sliderInput("provisionsslider", 
                                        label = h4("Provisions"), 
                                        min = 1, 
                                        max = 5, 
                                        value = 1),
                            sliderInput("safetyslider", 
                                        label = h4("Safety"), 
                                        min = 1, 
                                        max = 5, 
                                        value = 1),
                            sliderInput("physicalenvslider", 
                                        label = h4("Physical Environment"), 
                                        min = 1, 
                                        max = 5, 
                                        value = 1),
                            # Create action button, for reactive values
                            actionButton("action", label = "Search!")
                          ),
                          # The leaflet plot area
                          mainPanel(
                            leafletOutput("mymap", height = "800")

                          )
                        )
                      )
             ),
             # Create tabpanel with data explorer
             tabPanel("Data explorer",
                      hr(),
                      DT::dataTableOutput("mytable")
             )
                        )

# Establish processes to create output to the user interface
server <- function(input, output, session) {

  # Create color palette for the first map
  pal1 <- colorNumeric(
    palette = "Greens",
    domain = MunScores2016$Total_Score_2016,
    n = 6)

  # Initialise first displayed map of total score Municipalities 2016
  output$mymap <-renderLeaflet({
leaflet(data = MunScores2016) %>%
  addTiles() %>%
  addPolygons(
    fillColor = ~pal1(Total_Score_2016),
    fillOpacity = 1, 
    color = 'white', 
    weight = 1,
    popup = paste0("<strong>Municipality:</strong>", 
                   MunScores2016$Municipality_Name, 
                   "<br><strong>Quality-of-life-o-meter says: </strong>", 
                   MunScores2016$Total_Score_2016)) %>%
  addLegend(
    "bottomright",
    pal=pal1,
    values=~Total_Score_2016,
    opacity = 0.7,
    title="Total Quality-of-life-o-meter Score") 
  })

  # This code checks/observes if the find button is clicked, and if so, 
  # it executes formula to calculate new total score based on slide bars input
  observeEvent(input$action, {
Total_Score <- ((input$housingslider * MunScores2016$Housing_Score_2016 +
                   input$populationslider * MunScores2016$Population_Score_2016 +
                   input$provisionsslider * MunScores2016$Provisions_Score_2016 +
                   input$safetyslider * MunScores2016$Safety_Score_2016 +
                   input$physicalenvslider * MunScores2016$PhysicalEnvironment_Score_2016)/
                  (input$housingslider + input$populationslider + input$provisionsslider + input$safetyslider + input$physicalenvslider))

#Create reactive color palette for updated map
pal2 <- colorNumeric(
  palette = "Greens",
  domain = Total_Score,
  n = 6)

# Update the existing map
leafletProxy("mymap", data = MunScores2016) %>% 
  # Remove existing polygons and legend, before new ones are printed
  clearShapes() %>%
  clearControls() %>%
  addPolygons(
    fillColor = ~pal2(Total_Score), 
    fillOpacity = 1, 
    color = 'white', 
    weight = 1,
    popup = paste0("<strong>Municipality:</strong>", 
                   MunScores2016$Municipality_Name, 
                   "<br><strong>Quality-of-life-o-meter says: </strong>", 
                   Total_Score)) %>%
  addLegend("bottomright", # Legend position
            pal=pal2, # color palette
            values=~Total_Score, # legend values
            opacity = 0.7,
            title="Weighted Quality-of-life-o-meter Score")
  })

  # Create output table of MunScores data 2016
  output$mytable = DT::renderDataTable({
    DT::datatable(MunScores2016@data, 
              caption = 'Data explorer with all municipality scores of 2016', 
              colnames = c('Municipality Code', 'Municipality Name', 'Population Size', 
                           'Total Score', 'Housing Score', 'Population Score', 'Provisions Score', 
                           'Safety Score', 'Physical Environment Score'))
  })

}
# Call shinyApp function to execute the code
shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:0)

我有完全相同的错误,因为我将数据框传递到了传单中,而不是空间多边形数据框中。

我的问题是我有s4对象空间对象,并且正在与数据框合并,但是它将合并乘积转换为数据框,即使通过左连接也没有保留S4对象。我使用了sp :: merge,例如http://www.nickeubank.com/wp-content/uploads/2015/10/RGIS2_MergingSpatialData_part1_Joins.html

确保您传递的是空间数据框,而不是实际数据框。使用Class()确定什么是Munscores。