Difference generating image between .Rmd and .R
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
```
r r-markdown rastervis trellis
add a comment |
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
```
r r-markdown rastervis trellis
3
Don't you need to close thedev.off
with parentheses to finish the write?
– Chris Hartgerink
Jan 1 at 20:26
Chris, I think you're right, but it doesn't seem to change the result. I still get the same output file if I run the .Rmd, but I still get an empty file if I run the code line by line or in a .R file - even if I add the parentheses.
– jbtg
Jan 3 at 2:12
Okay. Hm. I usually use thepdf()
to start the plot and that works. Maybe you could try that?
– Chris Hartgerink
Jan 3 at 18:18
add a comment |
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
```
r r-markdown rastervis trellis
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
```
r r-markdown rastervis trellis
r r-markdown rastervis trellis
edited Jan 1 at 19:50
Julius Vainora
38.1k76585
38.1k76585
asked Jan 1 at 19:45
jbtgjbtg
112
112
3
Don't you need to close thedev.off
with parentheses to finish the write?
– Chris Hartgerink
Jan 1 at 20:26
Chris, I think you're right, but it doesn't seem to change the result. I still get the same output file if I run the .Rmd, but I still get an empty file if I run the code line by line or in a .R file - even if I add the parentheses.
– jbtg
Jan 3 at 2:12
Okay. Hm. I usually use thepdf()
to start the plot and that works. Maybe you could try that?
– Chris Hartgerink
Jan 3 at 18:18
add a comment |
3
Don't you need to close thedev.off
with parentheses to finish the write?
– Chris Hartgerink
Jan 1 at 20:26
Chris, I think you're right, but it doesn't seem to change the result. I still get the same output file if I run the .Rmd, but I still get an empty file if I run the code line by line or in a .R file - even if I add the parentheses.
– jbtg
Jan 3 at 2:12
Okay. Hm. I usually use thepdf()
to start the plot and that works. Maybe you could try that?
– Chris Hartgerink
Jan 3 at 18:18
3
3
Don't you need to close the
dev.off
with parentheses to finish the write?– Chris Hartgerink
Jan 1 at 20:26
Don't you need to close the
dev.off
with parentheses to finish the write?– Chris Hartgerink
Jan 1 at 20:26
Chris, I think you're right, but it doesn't seem to change the result. I still get the same output file if I run the .Rmd, but I still get an empty file if I run the code line by line or in a .R file - even if I add the parentheses.
– jbtg
Jan 3 at 2:12
Chris, I think you're right, but it doesn't seem to change the result. I still get the same output file if I run the .Rmd, but I still get an empty file if I run the code line by line or in a .R file - even if I add the parentheses.
– jbtg
Jan 3 at 2:12
Okay. Hm. I usually use the
pdf()
to start the plot and that works. Maybe you could try that?– Chris Hartgerink
Jan 3 at 18:18
Okay. Hm. I usually use the
pdf()
to start the plot and that works. Maybe you could try that?– Chris Hartgerink
Jan 3 at 18:18
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998437%2fdifference-generating-image-between-rmd-and-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998437%2fdifference-generating-image-between-rmd-and-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Don't you need to close the
dev.off
with parentheses to finish the write?– Chris Hartgerink
Jan 1 at 20:26
Chris, I think you're right, but it doesn't seem to change the result. I still get the same output file if I run the .Rmd, but I still get an empty file if I run the code line by line or in a .R file - even if I add the parentheses.
– jbtg
Jan 3 at 2:12
Okay. Hm. I usually use the
pdf()
to start the plot and that works. Maybe you could try that?– Chris Hartgerink
Jan 3 at 18:18