R Markdown stops rendering when encountering a chunk with base R graphics - r

sessionInfo()
R version 4.2.1 (2022-06-23)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Monterey 12.3
Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_4.2.1 fastmap_1.1.0 cli_3.3.0 htmltools_0.5.3 tools_4.2.1 yaml_2.3.5
[7] rmarkdown_2.14 gdata_2.18.0.1 knitr_1.39 xfun_0.31 digest_0.6.29 rlang_1.0.2
[13] gtools_3.9.3 evaluate_0.15
I have an issue where RMarkdowns stop knitting (without error) when encountering chunks with graphics/plots.
I experience this issue even with the default RMarkdown with summary(cars) and plot(pressure). However, when I comment out the plot(pressure) chunk, the .html file is rendered successfully and I get an output instantaneously.
I was getting the following error as well:
Error in .rs.parsePackageDependencies(contents, extension) :
object 'partition_yaml_front_matter' not found
and I tried remotes::install_version("rlang", "1.0.2") as suggested here:
https://github.com/rstudio/rstudio/issues/11552
But that hasn't solved this issue.
Any help or tips appreciated...

Related

Use older version of ggplot in R script file

I have ggplot2 3.4.0 installed on Ubuntu 22.04, but for some reasons I would like to use older version ggplot2 3.3.6.
library(ggplot2, lib.loc="~/R/ggplot336/")
I worked fine when I started with a clean script file. However, when I use an existing script file like:
library(ggplot2, lib.loc="~/R/ggplot336/")
ggimage::geom_image()
I got the following error massage:
Error in value[3L] :
Package ‘ggplot2’ version 3.4.0 cannot be unloaded:
Error in unloadNamespace(package) : namespace ‘ggplot2’ is imported by ‘ggfun’, ‘ggplotify’, ‘ggimage’ so cannot be unloaded
Edit
Fresh start Rstudio
Open my R script file with following 3 lines:
sessionInfo()
library(ggplot2, lib.loc="~/R/ggplot336/")
ggimage::geom_image()
When I run the first line, sessionInfo() before doing anything else. We can see ggplot2_3.4.0 there. Could this be a Rstudio feature or an issue?
R version 4.2.2 Patched (2022-11-10 r83330)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.1 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.10.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
locale:
[1] LC_CTYPE=en_AU.UTF-8 LC_NUMERIC=C LC_TIME=en_AU.UTF-8
[4] LC_COLLATE=en_AU.UTF-8 LC_MONETARY=en_AU.UTF-8 LC_MESSAGES=en_AU.UTF-8
[7] LC_PAPER=en_AU.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=en_AU.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] Rcpp_1.0.9 highr_0.9 pillar_1.8.1 compiler_4.2.2
[5] R.utils_2.12.2 R.methodsS3_1.8.2 yulab.utils_0.0.5 tools_4.2.2
[9] digest_0.6.30 evaluate_0.18 jsonlite_1.8.3 lifecycle_1.0.3
[13] tibble_3.1.8 gtable_0.3.1 ggimage_0.3.1 R.cache_0.16.0
[17] pkgconfig_2.0.3 rlang_1.0.6 reprex_2.0.2 DBI_1.1.3
[21] cli_3.4.1 ggplotify_0.1.0 rstudioapi_0.14 magick_2.7.3
[25] yaml_2.3.6 xfun_0.35 fastmap_1.1.0 knitr_1.41
[29] withr_2.5.0 dplyr_1.0.10 styler_1.8.1 generics_0.1.3
[33] vctrs_0.5.1 fs_1.5.2 gridGraphics_0.5-1 grid_4.2.2
[37] tidyselect_1.2.0 glue_1.6.2 R6_2.5.1 processx_3.8.0
[41] fansi_1.0.3 rmarkdown_2.18 clipr_0.8.0 callr_3.7.3
[45] ggplot2_3.4.0 purrr_0.3.5 magrittr_2.0.3 ps_1.7.2
[49] htmltools_0.5.3 scales_1.2.1 assertthat_0.2.1 colorspace_2.0-3
[53] utf8_1.2.2 munsell_0.5.0 ggfun_0.0.9 R.oo_1.25.0
It looks like R studio is loading in all of the previous libraries from a previous session. Maybe something leftover in .RData or .Rhistory. A fresh start of R should have far few loaded libraries.
Here is my fresh start:
sessionInfo()
R version 4.2.2 (2022-10-31)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Monterey 12.6.1
Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_4.2.2 tools_4.2.2
In Rstudio's preference you should see this screen:
I would try unchecking all of the boxes and then quit and restart Rstudio to see if that clears up the problem.
After some experiments, I kind of worked out the problem. Since this has consumed so much time of my time, I thought this could be helpful to others.
The problem is :: (double colon) with packages using ggplot2, such as ggimage in my example. It appears that RStudio automatically load some dependent packages when a script file is open. One way to fix this problem for my work now is to remove ::, but use library() then function (geom_image) instead.
This fixed my problem but I still don't understand why RStudio behaves this way.
If :: (double colon) is necessary, comment lines containing ::, then after loading older version of ggplot2, uncomment those lines. A little awkward, but it works for me.

Different sessionInfo() output on Mac cli vs. RStudio

When running sessionInfo() in Rstudio I get
R version 4.2.1 (2022-06-23)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Monterey 12.5.1
Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib
locale:
[1] de_DE.UTF-8/de_DE.UTF-8/de_DE.UTF-8/C/de_DE.UTF-8/de_DE.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] lubridate_1.8.0 fansi_1.0.3 assertthat_0.2.1 utf8_1.2.2 dplyr_1.0.10 R6_2.5.1 DBI_1.1.3 lifecycle_1.0.2 magrittr_2.0.3
[10] pillar_1.8.1 stringi_1.7.8 rlang_1.0.5 cli_3.4.0 rstudioapi_0.14 snakecase_0.11.0 vctrs_0.4.1 generics_0.1.3 tools_4.2.1
[19] stringr_1.4.1 glue_1.6.2 purrr_0.3.4 janitor_2.1.0 compiler_4.2.1 pkgconfig_2.0.3 tidyselect_1.1.2 tibble_3.1.8
When running the same on an R-session started in the terminal via R I get
R version 4.2.1 (2022-06-23)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur ... 10.16
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib
locale:
[1] de_DE.UTF-8/de_DE.UTF-8/de_DE.UTF-8/C/de_DE.UTF-8/de_DE.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_4.2.1
As you can see the outputs differ:
RStudio says it's Monterey (which is correct), cli says it's Big Sur.
BLAS is missing
different packages are loaded via namespace
Both use the same .libPaths().
There's only one R in the path at /usr/local/bin/R.
This behaviour occurs on two different Macs, both with Monterey and R 4.2.1.
So why are the sessions different?
The running component is a copy of the utils::osVersion variable value. As explained in ?sessionInfo, that is set to the value used when R was compiled, which is typically 10.16 on Intel Macs.
Presumably RStudio and R.app replace that value with a correct one when run, but command line R doesn't.
If you build R yourself under Monterey, you'll see the correct value.
Regarding the packages loaded: RStudio loads a bunch of packages to support its API.
Regarding BLAS: I don't know, but it could be similar to the running issue, i.e. the front-end changed it.

R markdown knitting error and R starting error after installing the packages extrafont, sysfonts and showtext / re-installation of R

In desperation I try to re-establish the ability for knitting markdown skripts, as this is no more possible after installing 3 packages concerning other fonts in graphs. This is what happened:
I worked with R 4.1.0 (2021-05-18) -- "Camp Pontanezen" (and Rstudio) up to yesterday, wrote thousands of rows in R and R markdown (which worked perfectly for several month). In order to change font types in plots I installed the packages extrafont, sysfonts and showtext, without any efforts regarding the ability to change the font in graphs. Without changing ANYTHING in the .Rmd files, these weren't able to be knitted anymore after these installations (there was an error, I can't reproduce at the moment and nothing was knitted).
I thought, uninstalling all packages and/or re-installing R and Rstudio and the packages needed for the skripts would help in this situation, but this was not the case.
case1: (latest version of R, R 4.2.0)
neither I'm able to knit markdown files, nor the start of R works (see error-messages below)
case2: (version of R, that worked before, R 4.1.0)
no error while starting R, but knitting is still unpossible (see error above)
there might be a utf-8 problem, as my username contains a german umlaut (ü), but I had no problems before the installation of these 3 packages! default encoding is UTF-8 in the global options. Maybe the updated versions of other packages not mentioned are also a problem. But it seems, that there's profound disbalance in the whole R system now.
here's my sessionInfo:
R version 4.1.0 (2021-05-18)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)
Matrix products: default
locale:
[1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252
[3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C
[5] LC_TIME=German_Germany.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] tidyr_1.2.0 fansi_1.0.3 assertthat_0.2.1 utf8_1.2.2 crayon_1.5.1
[6] dplyr_1.0.9 R6_2.5.1 backports_1.4.1 DBI_1.1.2 lifecycle_1.0.1
[11] magrittr_2.0.3 pillar_1.7.0 rlang_1.0.2 cli_3.3.0 vctrs_0.4.1
[16] generics_0.1.2 ellipsis_0.3.2 tools_4.1.0 glue_1.6.2 purrr_0.3.4
[21] broom_0.8.0 compiler_4.1.0 pkgconfig_2.0.3 tidyselect_1.1.2 tibble_3.1.7
I don't know what to do next.

R Blogdown serve site: Error in !server$daemon : invalid argument type

My website used to work fine in my previous laptop, which used R 3.6.3. In my new laptop when I run blogdown::serve_site() I get the error Error in !server$daemon : invalid argument type. I tried doing what was suggested here but didn't help.
Here is the session info:
R version 4.1.0 (2021-05-18)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 11.2.3
Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] Rcpp_1.0.6 bookdown_0.22 digest_0.6.27 later_1.2.0 R6_2.5.0
[6] jsonlite_1.7.2 magrittr_2.0.1 evaluate_0.14 blogdown_1.3.2 rlang_0.4.11
[11] rstudioapi_0.13 promises_1.2.0.1 rmarkdown_2.9 tools_4.1.0 servr_0.22
[16] httpuv_1.6.1 xfun_0.24 yaml_2.2.1 compiler_4.1.0 htmltools_0.5.1.1
[21] knitr_1.33
This is the website link and you can find the Github repo here.
You should remove the quotes on interactive(). It should be
servr.daemon = interactive()
instead of
servr.daemon = "interactive()"
Actually servr.daemon = interactive() is the default, so you don't really need to set it explicitly (i.e., you can remove this setting).

Error creating notebook: no lines available in input

When trying to Preview a simple notebook in RStudio, I am getting the following error in the top of the source pane and no nb.html file is created:
"Error creating notebook: no lines available in input"
Screenshot:
> sessionInfo(package = NULL)
R version 3.3.3 (2017-03-06)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=German_Austria.1252 LC_CTYPE=German_Austria.1252 LC_MONETARY=German_Austria.1252 LC_NUMERIC=C LC_TIME=German_Austria.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] backports_1.0.5 magrittr_1.5 rprojroot_1.2 htmltools_0.3.6 tools_3.3.3 base64enc_0.1-3 yaml_2.1.14 Rcpp_0.12.12 stringi_1.1.3 rmarkdown_1.8 knitr_1.17
[12] stringr_1.2.0 digest_0.6.12 evaluate_0.10
> RStudio.Version()
$mode
[1] "desktop"
$version
[1] ‘1.0.153’
I wasn't able to replicate the exact error but you could try writing the output html notebook in same line.
---
title: "R Notebook"
output: html_notebook: default
html_document: default
---
I fixed this by opening a new Notebook, copying the contents of my original, error-ing Notebook there, and then overwriting the original Notebook. The error presented, for me, after I moved a folder containing all the files associated with this project to a new location.

Resources