首页 > 解决方案 > tmap 不会以闪亮的方式呈现

问题描述

我有一些呈现tmapin 的代码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)

标签: rshinytmap

解决方案


您需要将服务器中的输出保存为您在 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


推荐阅读