rtmap

Problem saving a tmap file: Receive error message - Unknown format. tm should be either a tmap output, or a list of tmap outputs


In RStudio, I cannot get the map created by the following tmap script to save to my working directory. When running the script prior to the tmap_save line, I receive no errors. However, when I run tmap_save, I receive the following error:

Error in tmap_save(st_map, filename = mapname) : Unknown format. tm should be either a tmap output, or a list of tmap outputs

Here is my script with sample data:

# Load Packages
library(tidyverse)
library(readr)
library(dplyr)
library(lubridate)
library(forcats)
library(stringr)
library(ggplot2)
library(ggmap)
library(maps)
library(mapdata)
library(sf)
library(tmap)
library(spData)
library(ggspatial)
library(readxl)
library(haven)
library(RColorBrewer)
library(ggsci)
library(ggthemes)
library(vroom)
library(scales)

# Map Title
map_title <- 'Place Schedule Adherence Map'

# Initialize Map
tmap_mode("view")
st_map <- NULL

# Create timepd List
tp_list <- sa_sf %>%
  select(timepd) %>%
  arrange(timepd)

tp_list <- unique(tp_list$timepd) %>% sort()

# Loop Through Each Time Period
for (tp in tp_list) {

st_map <- st_map %>%

  tm_shape(sa_sf %>% filter(timepd == tp)) +
    tm_symbols(col='pgr_delay',
               size=.01,
               scale=10,
               palette = 'YlOrRd',
               group = paste(tp, 'Place Schedule Adherence'),
               interactive=TRUE,
               popup.vars=c('schedule_type',
                            'timepd',
                            'place',
                            'stopid',
                            'stopname',
                            'route_list',
                            'pc_late'))  
 } 

# Add Map Title and Scale Bar
st_map <- st_map %>%
  tm_layout(title = map_title,
            title.size = 10,
            title.fontface = 'bold') 

# Create Map html File
mapname = 'Schedule Adherence Map.html'

# Create Map html File
tmap_save(st_map, filename = mapname)

mtcars <- structure(list(schedule_type = c("WKD", "WKD", "WKD", "WKD", "WKD", "WKD", "WKD", "WKD", "WKD", "WKD", "WKD", "WKD"), timepd = c("03:00-07:00", "03:00-07:00", "07:00-09:00", "07:00-09:00", "09:00-14:30", "09:00-14:30", "14:30-18:00", "14:30-18:00", "18:00-22:00", "18:00-22:00", "22:00-30:00", "22:00-30:00"), place = c("AEEN", "AEEN", "AEEN", "AEEN", "AEEN", "AEEN", "AEEN", "AEEN", "AEEN", "AEEN", "AEEN", "AEEN"), stopid = c(2108, 4138, 2108, 4138, 2108, 4138, 2108, 4138, 2108, 4138, 2108, 4138 ), stopname = c("Elgin / Avenue", "Elgin / Avenue", "Elgin / Avenue", "Elgin / Avenue", "Elgin / Avenue", "Elgin / Avenue", "Elgin / Avenue", "Elgin / Avenue", "Elgin / Avenue", "Elgin / Avenue", "Elgin / Avenue", "Elgin / Avenue"), route_list = c("58", "58", "58", "58", "58", "58", "58", "58", "58", "58", "58", "58"), pc_late = c(11, 1, 19, 5, 12, 15, 21, 39, 11, 14, 6, 3), geometry = structure(list( structure(c(-80.306392, 43.378458), class = c("XY", "POINT", "sfg")), structure(c(-80.306576, 43.379437), class = c("XY", "POINT", "sfg")), structure(c(-80.306392, 43.378458), class = c("XY", "POINT", "sfg")), structure(c(-80.306576, 43.379437), class = c("XY", "POINT", "sfg")), structure(c(-80.306392, 43.378458), class = c("XY", "POINT", "sfg")), structure(c(-80.306576, 43.379437), class = c("XY", "POINT", "sfg")), structure(c(-80.306392, 43.378458), class = c("XY", "POINT", "sfg")), structure(c(-80.306576, 43.379437), class = c("XY", "POINT", "sfg")), structure(c(-80.306392, 43.378458), class = c("XY", "POINT", "sfg")), structure(c(-80.306576, 43.379437), class = c("XY", "POINT", "sfg")), structure(c(-80.306392, 43.378458), class = c("XY", "POINT", "sfg")), structure(c(-80.306576, 43.379437), class = c("XY", "POINT", "sfg"))), class = c("sfc_POINT", "sfc"), precision = 0, bbox = structure(c(xmin = -80.306576, ymin = 43.378458, xmax = -80.306392, ymax = 43.379437), class = "bbox"), crs = structure(list( input = "EPSG:4326", wkt = "GEOGCRS["WGS 84",\n ENSEMBLE["World Geodetic System 1984 ensemble",\n MEMBER["World Geodetic System 1984 (Transit)"],\n MEMBER["World Geodetic System 1984 (G730)"],\n MEMBER["World Geodetic System 1984 (G873)"],\n MEMBER["World Geodetic System 1984 (G1150)"],\n MEMBER["World Geodetic System 1984 (G1674)"],\n MEMBER["World Geodetic System 1984 (G1762)"],\n MEMBER["World Geodetic System 1984 (G2139)"],\n ELLIPSOID["WGS 84",6378137,298.257223563,\n LENGTHUNIT["metre",1]],\n ENSEMBLEACCURACY[2.0]],\n PRIMEM["Greenwich",0,\n ANGLEUNIT["degree",0.0174532925199433]],\n CS[ellipsoidal,2],\n AXIS["geodetic latitude (Lat)",north,\n ORDER[1],\n ANGLEUNIT["degree",0.0174532925199433]],\n AXIS["geodetic longitude (Lon)",east,\n ORDER[2],\n ANGLEUNIT["degree",0.0174532925199433]],\n USAGE[\n SCOPE["Horizontal component of 3D system."],\n AREA["World."],\n BBOX[-90,-180,90,180]],\n ID["EPSG",4326]]"), class = "crs"), n_empty = 0L)), row.names = c(NA, -12L), sf_column = "geometry", agr = structure(c(schedule_type = NA_integer_, timepd = NA_integer_, place = NA_integer_, stopid = NA_integer_, stopname = NA_integer_, route_list = NA_integer_, pc_late = NA_integer_ ), levels = c("constant", "aggregate", "identity"), class = "factor"), class = c("sf", "tbl_df", "tbl", "data.frame"))


Solution

  • I don't think it's just saving. If this is the actual code, you shouldn't be able to see any rendered tmap ouput as there are 2 occasions where pipe (%>%) is used where + is expected.

    # in loop
    st_map <- st_map %>%
      tm_shape(...) 
    
    # /../
    
    # post-loop
    st_map <- st_map %>%
      tm_layout(...) 
    

    Try changing those to st_map + tm_... and you should end up with a valid tmap object