R闪亮:sqldf不能与observe一起使用,显示:result_create中的错误:在“&”附近:语法错误

时间:2019-06-08 06:43:46

标签: r shiny sqldf

当用户使用滑块更改输入时,我正在尝试让波形图作出反应。我正在使用sqldf库查询结果并将其绘制在地图中。但是,当前,我得到的结果是“ result_create中的错误:靠近“&”:语法错误”。不知道如何解决它,或者从一开始我就错了。

library(magrittr)
library(shiny)
library(shinythemes)
library(dplyr)
library(readr)
library(ggplot2)
library(leaflet)
library(evaluate)
library(ggmap)
library(rgdal)
library(tmap)
library(tmaptools)
library(sf)
library(geojsonio)
library(sqldf)
library(DBI)
library(gsubfn)
library(RH2)
library(RSQLite)
library(rJava)
library(shinydashboard)


# Define UI for application that draws a histogram
ui <- dashboardPage(
   # Application title
   dashboardHeader(title="Vic Car Accidents"),
   dashboardSidebar(
     sliderInput("range", "Year:",
                 min = 2013, max = 2018,
                 value = c(2013,2018))),

   dashboardBody(
            box(title = "Choropleth Map",width = 12, status = "primary",leafletOutput("young_driver",width = "100%", height = 400)
                ),
            box(tableOutput("values"))
   ))


# Define server logic required to draw a histogram
options(scipen = 999)
#read LGA geojson file from local file
LGA<-st_read("Data/LGA.geojson",stringsAsFactors = FALSE)
#read car crashes data from local file
carCrashes<- read_csv("Data/Car.csv")

server <- shinyServer(function(input, output, session) {
  sliderValues <- reactive({
    data.frame(name = "range", value= (paste(input$range[1],input$range[2])))})
  output$values <- renderTable({
    sliderValues()
  })

  temp <- observe({
    year_1 <- input$range[1]
    year_2 <- input$range[2]
    #data.frame(name = "range", value= (paste(input$range[1],input$range[2])))})
    read.csv.sql(
    "Data/Car.csv",
    sql = ("select distinct LGA_NAME, count(LGA_NAME) as 'number of young driver'
    from file
    where driver_type = 'young driver' and
year_period >= year_1 && year_period <= year_2
    group by LGA_NAME; ")

  )
    sub_and_car <- left_join(LGA,temp,by = c("VIC_LGA__3" = "LGA_NAME"))
    output$young_driver<-renderLeaflet({

      tm<-tm_shape(sub_and_car)+tm_polygons(col="number of young driver", border.col="grey")

      tmap_leaflet(tm)
    })


 # output$young_driver<-renderLeaflet({



  # tm<-tm_shape(sub_and_car)+tm_polygons(col="number of young driver", border.col="grey")

  #tmap_leaflet(tm)
  })
   })


# Run the application 
shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:0)

好吧,我明白了。。。您在代码中犯了多个错误:

observeEvent仅应用于检查副作用,而不要创建data.frame。始终使用eventReactive来做到这一点。我在您的sqldf代码中找不到很多错误。无论如何,我还是建议学习dplyr,这是其他程序员也使用的标准。

通过渲染窗口小部件分别创建data.frames。由于地图的绘制需要很长时间,因此我使用shinycssloaders向用户显示了一些反馈。否则,他或她可能只是关闭应用程序。

这里是代码,只需更改路径即可:

library(magrittr)
library(shiny)
library(shinythemes)
library(dplyr)
library(readr)
library(ggplot2)
library(leaflet)
library(evaluate)
library(ggmap)
library(rgdal)
library(tmap)
library(tmaptools)
library(sf)
library(geojsonio)
library(sqldf)
library(DBI)
library(gsubfn)
library(RH2)
library(RSQLite)
library(rJava)
library(shinydashboard)
library(shinycssloaders)


# Define UI for application that draws a histogram
ui <- dashboardPage(
   # Application title
   dashboardHeader(title="Vic Car Accidents"),
   dashboardSidebar(
     sliderInput("range", "Year:",
                 min = 2013, max = 2018,
                 value = c(2013,2018)),
   actionButton("change", "Change")),

   dashboardBody(
            box(title = "Choropleth Map",width = 12, status = "primary",withSpinner(leafletOutput("young_driver",width = "100%", height = 400))
                ),
            box(tableOutput("values"))
   ))


# Define server logic required to draw a histogram
options(scipen = 999)
#read LGA geojson file from local file
LGA<-st_read("C:/Users/User/Downloads/R-project-master/R-project-master/LGA.geojson",stringsAsFactors = FALSE)
#read car crashes data from local file
carCrashes<- read_csv("C:/Users/User/Downloads/R-project-master/R-project-master/Car.csv")

server <- shinyServer(function(input, output, session) {
  sliderValues <- reactive({
    data.frame(name = "range", value= (paste(input$range[1],input$range[2])))})
  output$values <- renderTable({
    sliderValues()
  })

  cmap <- eventReactive(input$change,{
    range_1 <- as.numeric(input$range[1])
    range_2 <- as.numeric(input$range[2])

    temp <- carCrashes %>%
      filter(driver_type == 'young driver' & year_period >= range_1 & year_period <= range_2) %>%
      group_by(LGA_NAME) %>%
      mutate(`Number of young drivers` = n())


    # temp<- sqldf(
    # "C:/Users/User/Downloads/R-project-master/R-project-master/Car.csv",
    # sql = paste("select distinct LGA_NAME, count(LGA_NAME) as 'number of young driver' 
    # from file
    # where driver_type = 'young driver' and
    # year_period between range_1 and range_2
    # group by LGA_NAME; ")

    sub_and_car <- left_join(LGA,temp,by = c("VIC_LGA__3" = "LGA_NAME"))

    })

  output$young_driver <- renderLeaflet({
    tm <- tm_shape(cmap())+tm_polygons(col="Number of young drivers", border.col="grey")
    tmap_leaflet(tm)
  })

   })


# Run the application 
shinyApp(ui = ui, server = server)