.Rmd和.R之间的差异生成图像

时间:2019-01-01 19:45:39

标签: r r-markdown rastervis trellis

下面的代码是完整的.Rmd文件,该文件成功生成带有表面温度的霍夫莫勒图的.pdf文件。 (对于粘贴整个文件,我深表歉意,但这很简短,我不确定是什么引起了问题-尽管我相信它在代码的最后一部分。)数据文件在此处:{{3} }

当我运行.Rmd文件时,它似乎可以正常工作。它会生成一个230kb的.pdf文件,其中包含一个(相当有用的)hovmoller图。但是,如果我将代码放入.R文件中(或从.Rmd文件逐行运行),它仍然会生成.pdf文件,但是如果我尝试将其打开,它将为空并生成错误消息

有人可以告诉我为什么会这样吗?

---
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
```

0 个答案:

没有答案