Suppress download message from ceramic package in pagedown Rmarkdown

39 Views Asked by At

I have a loop that clips to different features and the ceramic package downloads a basemap for each feature and then the map is printed using the tmap package. In an html or pdf output in Rmarkdown, I can use {r map, results = 'hide', fig.keep='all'} to hide ceramic's "Preparing to download..." messages. The maps print, but the messages don't (exactly what I want). However, using pagedreport, that method doesn't work. No maps print.

I tried wrapping the ceramic part suppressWarnings(suppressMessages(suppressPackageStartupMessages({basemap})))

And setting options(warn=-1) at the beginning of the chunk

Neither work. Any other suggestions?

You'd need your own mapbox API key to run this. The output is ugly, but gets the point across.

Reproducible loop:

---
title: "nc"
date: "`r Sys.Date()`"
output: 
  pagedreport::paged_windmill:
    
knit: pagedown::chrome_print

---

```
{r libs, include = F}  

library(raster)
library(sp)
library(tidyverse)
library(ceramic)
library(tmap)
library(maps)
```


 ```
{r individual-maps, echo=FALSE, error=TRUE, message=FALSE, warning=FALSE, results='asis', fig.keep='all'}

#get practice shapefile
nc <- sf::read_sf(system.file("shape/nc.shp", package="sf"))


#get list of unique trail numbers
#unique_county_names <- unique(nc$NAME)
unique_county_names <- c("Ashe", "Alleghany", "Surry") # created a short list for simplicity

#set Mapbox API key for the session
  Sys.setenv(MAPBOX_API_KEY = "need your own")

for (i in 1:length(unique_county_names)) {
  county <-
    filter(nc, NAME == unique_county_names[i])

  
  # set basemap
basemap = ceramic::cc_location(loc = 
    extent(sf::st_transform(
   county, 4326
  )),
  base_url = "https://api.mapbox.com/styles/v1/mapbox/outdoors-v12/tiles/{zoom}/{x}/{y}")

  
 county_map <- tm_shape(basemap) +
    tm_rgb() +
    tm_shape(county) +
   tm_polygons()
    
  
  print(county_map)

}
```
1

There are 1 best solutions below

0
mil On

I figured out it was a cat() output which then led me to this post How to hide or disable in-function printed message

and the answer by Ben, credited to Hadley Wickham:

quiet <- function(x) { 
  sink(tempfile()) 
  on.exit(sink()) 
  invisible(force(x)) 
} 

so then I wrapped it around the basemap:

basemap = quiet(ceramic::cc_location(loc = 
    extent(sf::st_transform(
   county, 4326
  )),
  base_url = "https://api.mapbox.com/styles/v1/mapbox/outdoors-v12/tiles/{zoom}/{x}/{y}"))

No more messages!