tmap不会呈现闪亮

时间:2019-06-07 12:57:13

标签: r shiny tmap

我有一些代码可以在tmap中显示shiny,但是它没有显示,也没有返回错误消息。 它仅在查看器中显示,但不发光。 我尽了一切所能,但仍然毫无头绪。

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)

# Define UI for application that draws a histogram
ui <- fluidPage(theme = shinytheme("darkly"),
   fluidRow(
   # Application title
   column(12, align = "center",tags$h2("Victoria Car Accidents"))
   ),
   fluidRow(
     column(9,
            leafletOutput("working_map",width = "100%", height = 
400),
     column(3)
   )))


# 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 <- function(input, output) {

  output$young_driver<-renderLeaflet({
    temp <- 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'
      group by LGA_NAME; "
    )
    sub_and_car <- left_join(LGA,temp,by = c("VIC_LGA__3" = 
"LGA_NAME"))

    tmap_mode("view")
    working_map<-tm_shape(sub_and_car)+tm_polygons(col="number of 
young driver", border.col="grey")
    tmap_leaflet(working_map)
  })
   }


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

1 个答案:

答案 0 :(得分:0)

您需要将服务器中的输出保存为与您在 ui 中使用的名称相同的名称。所以你有:

leafletOutput("working_map",width = "100%", height = 400)

output$young_driver<-renderLeaflet({...

leafletOutput 无法“看到”working_map。您应该用以下内容替换该行:

leafletOutput("young_driver",width = "100%", height = 400)

输出和渲染函数应该链接到同一个对象。

在这种情况下,我可以通过阅读您的代码看到此错误,但如果您提供一个可重现的示例通常会有所帮助,这样人们就可以复制和粘贴您的代码并直接运行它。

请看这里:https://stackoverflow.com/help/minimal-reproducible-example