我在读取数据时遇到麻烦

时间:2020-05-21 03:49:30

标签: r shiny subset reactive

我对新手有点陌生,我一直在尝试制作一个交互式地图,显示一年内(2019年5月开始,2020年5月结束)为一个项目收集的注册表数量。

当我运行代码时,弹出窗口将打开并按我的意愿显示地图和滑块输入框,但是地图未显示数据,当我单击每种状态时,它将显示NA。

运行代码时,出现以下错误消息:

“ ==”不兼容的方法(“ Ops.factor”,“ Ops.yearmon”),“ ==“不兼容的方法(“ Ops.factor”,“ ==。Date”) “ ==“不兼容的方法(” Ops.factor“,” ==。Date“)的” ==“不兼容的方法(” Ops.factor“,” = = .Date“)用于==”不兼容的方法(“ Ops.factor”,“ ==。Date”)对于“ ==”不兼容方法(“ Ops.factor”,“ ==。Date”) =“

这是我的代码:

ui <- bootstrapPage(

    # Application title
    titlePanel("Numero de registros"),

    tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
  leafletOutput("map", width = "100%", height = "100%"),
  absolutePanel(top = 10, right = 10,
                sliderInput("range", "Fecha", 
                            min = as.Date(as.yearmon("2019-05-23")), 
                            max = as.Date(as.yearmon("2020-05-07")), # most recent date on the CSV file. UPDATE with each CSV
                            value = as.Date(as.yearmon("2019-05-23")),
                            timeFormat = "%b %Y",
                )
  )
)

# Shiny app - server
server <- function(input, output, session) {

  # Reactive expression for the data subsetted to what the user selected
  filteredData <- reactive({
    states <- left_join(states, subset(reg, reg_date == input$range[1]), 
                             by = "id")
    states

  })

  # This reactive expression represents the palette function, which changes as the user makes selections in UI.
  colorpal <- reactive({
    colorFactor(palette = c("#D8F0C6", "#AED68F", "#8ABA64", "#66993E", "#477622", "#274C0A"), reg$Freq)
  })

  output$map <- renderLeaflet({
    # Use leaflet() here, and only include aspects of the map that won't need to change dynamically 
    states <- left_join(states, subset(reg, reg_date ==  as.yearmon("2019-05-23")), by = "id")
    pal <- colorpal()
    leaflet(states) %>% addTiles() %>%
      setView(-102, 23.8, 5)%>% 
      addLegend(position = "bottomright",
                pal = pal, values = ~reg$Freq,
                title = "Registros") %>% 
      addTiles()
  })

  # Incremental changes to the map (in this case, replacing the circles when a new color is chosen) should be performed in   an observer. Each independent set of things that can change should be managed in its own observer.
  observe({
    pal <- colorpal()
    leafletProxy("map", data = filteredData()) %>%
      clearShapes() %>%
      addPolygons(stroke = TRUE, weight = 1, color = "#000000",
                  fillOpacity = 0.8, smoothFactor = 0.5,
                  fillColor = ~pal(Freq), 
                  popup = ~ sprintf("Estado: %s<br/>Registros: %s",
                                    stri_trans_totitle(state_name), 
                                    round(Freq, 1)))
  })


}
shinyApp(ui, server)

我真的很感激有人可以帮助我修复代码。

0 个答案:

没有答案