2

I have some code that renders a tmap in shiny, however, it does not show and no error messages are returned. It shows in the viewer alone, but not in shiny. I tried almost everything I could, but still no clue.

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)
NelsonGon
  • 13,015
  • 7
  • 27
  • 57
Bolun Cui
  • 31
  • 1

1 Answers1

0

You need to save the output in the server to the same name you are using in the ui. So you have:

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

output$young_driver<-renderLeaflet({...

leafletOutput cannot "see" working_map. You should replace that line with the following:

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

The output and render functions should be linked with the same object.

In this case I could see this error from reading your code, but it is often helpful if you provide a reproducible example so people can copy and paste your code and run it directly.

See here: https://stackoverflow.com/help/minimal-reproducible-example

nd37255
  • 248
  • 1
  • 9