The code below is a full .Rmd file that successfully generates a .pdf file with a hovmoller plot of surface temperature. (I apologize for pasting the whole file, but it is short and I'm not sure what might be causing the problem - though I believe it is in the last section of the code.) The data file is here: https://crudata.uea.ac.uk/cru/data/temperature/HadCRUT.4.6.0.0.median.nc
When I run the .Rmd file, it seems to work flawlessly. It generates a .pdf file that is 230kb that contains a (rather informative) hovmoller plot. If, however, I put the code into a .R file (or run it line by line from the .Rmd file), it still generates a .pdf file, but it is empty and generates an error message if I try to open it.
Can anybody tell me why this happens?
---
title: "Hovmoller plot - HadCRUT4 through November 2018"
author: "jbtg"
date: "January 1, 2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r warning=FALSE,message=FALSE}
library(maptools)
library(ncdf4)
library(raster)
library(rasterVis)
library(RColorBrewer)
library(zoo)
library(sf)
library(rgdal)
```
```{r}
#HADCRUT4 combined air temp/SST anomaly as of November 1, 2018
#read a 3D netCDF dataset
hadcrut4_path <- "C:/Users/jtene/Dropbox/!!!netcdf/Rmarkdown/"
hadcrut4_name <- "HadCRUT.4.6.0.0.median.nov18.nc"
hadcrut4_file <- paste0(hadcrut4_path,hadcrut4_name)
hadcrut4 <- brick(hadcrut4_file) #opens anomaly netCDF file (calls nc_open)
hadcrut4 #gives a summary of the file contents
print(c(filename(hadcrut4), hasValues(hadcrut4), inMemory(hadcrut4)))
```
Set up and produce the Hovmöller plot:
```{r}
#setup
idx <- seq(as.Date('1850-01-01'), as.Date('2018-11-01'), 'month')
idx <- as.yearmon(idx)
tmpplt <- setZ(hadcrut4, idx)
names(tmpplt) <- as.character(idx)
#plot
trellis.device('pdf', file = 'hov_hadcrut4.pdf')
hovmoller(tmpplt, dirXY=y,
at = do.breaks(c(-3.5,3.5),14),
contour = F, interpolate = F,
par.settings=RdBuTheme(region=rev(brewer.pal(9,'RdBu'))),
main="HadCRUT4 Anomalies 1850-2018 (1961-1990 base period)")
dev.off
```