0

I am writing an R-package and its documentation using roxygen2 to document my functions and pkgdown to create a github page.

The package is about rasters, so I have included some raster plot in the examples of my function documentation.

When the examples run in the console everything looks fine. When they run via the pkgdown package (using build_reference() or build_site()) the plot display on the github changes.

This is the correct plot

library(raster)

r <- raster(matrix(1:49, nrow = 7, byrow = TRUE))
plot(r)
text(r)

correct plot

This is how the plot is rendered on the github page in the reference section.

wrong plot

Note that if the same plots are included in a vignette and not in the documentation they are rendered just fine.

I am a bit at a lost here. Do you have any suggestion on why this is happening and how can I correct this behavior?

Gerald T
  • 704
  • 3
  • 18

2 Answers2

0

I believe that this behavior is owed to the fact the raster plot fix the aspect ration to 1 while the text function plots on the entire plotting area.

I still do not understand why things are fine in the vignettes and not in the documentation but a workaround is to set the argument asp = NA.

library(raster)

r <- raster(matrix(1:49, nrow = 7, byrow = TRUE))
plot(r, asp=NA)
text(r)

enter image description here

With this setting the plot is showed fine everywhere on the site.

Gerald T
  • 704
  • 3
  • 18
0

It looks like pkgdown resets the pars after each plot. I think this may not be an issue if you use terra instead of raster --- and that would be a much better choice anyway if you are developing a new package.

Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
  • Thank you Robert for your answer. I'll try to switch to `terra` as you suggested. However the package has been in development for quite some time and I am afraid it'll take quite some time to change raster for terra. – Gerald T Jan 28 '22 at 11:25
  • ~80% of the interface is the same. See the bottom of `?terra` for differences with `raster` – Robert Hijmans Jan 28 '22 at 17:54