rmarkdown::render behaves differently in terminal and rstudio

Hello,

I am trying to render a HTML file using rmarkdown::render for an interactive plot generated from this package GitHub - oganm/brainspriteR: Shiny widget for brainsprite. It works very well when I simply run the following in studio

rmarkdown::render('./test.R', output_format = "html_document", output_dir = './', output_file = 'testbrainsprite.html')

In test.R I have the following short code

#' author: "a"
#' date: "`r format(Sys.time(), '%B, %Y')`"
#' linkcolor: blue
#' output:
#'   html_document:
#'     fig_caption: yes
#'     theme: spacelab #sandstone #spacelab #flatly
#'     highlight: default
#'     toc: FALSE
#'     toc_depth: 3
#'     number_sections: FALSE
#'     toc_float:
#'      smooth_scroll: FALSE
#' ---

#' ```{css, echo=FALSE}
#' pre {
#'   max-height: 300px;
#'   overflow-y: auto;
#' }
#' 
#' pre[class] {
#'   max-height: 600px;
#' }
#' ```

#+ include = FALSE
# use ragg for better figure quality
knitr::opts_chunk$set(dev = "ragg_png", out.width = "100%", fig.align = "center") 

#' ### Load library and utility functions
# ---------------------------------------------------------------------------------------------
#+ warning = FALSE, message = FALSE
library(tidyverse)
library(data.table)
library(ragg)
library(brainspriteR)

#' ### plot
#+ echo = FALSE, warning = FALSE, message = FALSE, fig.height = 10, out.width = '100%', dpi = 100
brainsprite(sprites = './sprite.jpg', # from https://github.com/oganm/brainspriteR/tree/master/inst
            flagCoordinates = TRUE,
            spriteHeight = 233,
            spriteWidth = 189)

However, when I run the same rmarkdown::render code in the terminal, the rendered HTML does not contain/show the plot. I have loaded markdown and knitr library. Could you please help? Thanks a lot.

By terminal you mean outside of RStudio ?

If that is the case, you may not be using the Pandoc version or same R or R packages.

Could you check that outside of RStudio in your terminal you have:

  • Same R version
  • Same library (.libPaths())
  • Same package version (xfun::session_info("rmarkdown")). This should print the Pandoc version too.

Otherwise, can you provide a Rmd file that we can use to reproduce the issue ? Thank you

1 Like

Thanks a lot @cderv

Yes, it's just a Linux terminal of the HPC I use. Rstudio server is also available on this HPC, that's how I tested the code in Rstudio.

I module load the same r version before opening R in the terminal or studio server. They have the same libPath.

[1] "/home/djangoc/R/x86_64-pc-linux-gnu-library/4.1"                                         
[2] "/cvmfs/soft.computecanada.ca/easybuild/software/2020/avx512/Core/r/4.1.2/lib64/R/library"

Below is the output of xfun::session_info("markdown") in Rstudio server, pandoc versions are different.

R version 4.1.2 (2021-11-01)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: CentOS Linux 7 (Core), RStudio 2022.3.999.999

Locale:
  LC_CTYPE=en_CA.UTF-8       LC_NUMERIC=C               LC_TIME=en_CA.UTF-8        LC_COLLATE=en_CA.UTF-8     LC_MONETARY=en_CA.UTF-8   
  LC_MESSAGES=en_CA.UTF-8    LC_PAPER=en_CA.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
  LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C       

Package version:
  base64enc_0.1.3 bslib_0.4.0     cachem_1.0.6    digest_0.6.29   evaluate_0.16   fastmap_1.1.0   fs_1.5.2        glue_1.6.2      graphics_4.1.2 
  grDevices_4.1.2 highr_0.9       htmltools_0.5.3 jquerylib_0.1.4 jsonlite_1.8.0  knitr_1.39      magrittr_2.0.3  memoise_2.0.1   methods_4.1.2  
  R6_2.5.1        rappdirs_0.3.3  rlang_1.0.4     rmarkdown_2.15  sass_0.4.2      stats_4.1.2     stringi_1.7.8   stringr_1.4.1   tinytex_0.41   
  tools_4.1.2     utils_4.1.2     xfun_0.32       yaml_2.3.5     

Pandoc version: 2.14.0.3

and in R opened via terminal

R version 4.1.2 (2021-11-01)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: CentOS Linux 7 (Core)

Locale:
  LC_CTYPE=en_CA.UTF-8       LC_NUMERIC=C              
  LC_TIME=en_CA.UTF-8        LC_COLLATE=en_CA.UTF-8    
  LC_MONETARY=en_CA.UTF-8    LC_MESSAGES=en_CA.UTF-8   
  LC_PAPER=en_CA.UTF-8       LC_NAME=C                 
  LC_ADDRESS=C               LC_TELEPHONE=C            
  LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C       

Package version:
  base64enc_0.1.3 bslib_0.4.0     cachem_1.0.6    digest_0.6.29  
  evaluate_0.16   fastmap_1.1.0   fs_1.5.2        glue_1.6.2     
  graphics_4.1.2  grDevices_4.1.2 highr_0.9       htmltools_0.5.3
  jquerylib_0.1.4 jsonlite_1.8.0  knitr_1.39      magrittr_2.0.3 
  memoise_2.0.1   methods_4.1.2   R6_2.5.1        rappdirs_0.3.3 
  rlang_1.0.4     rmarkdown_2.15  sass_0.4.2      stats_4.1.2    
  stringi_1.7.8   stringr_1.4.1   tinytex_0.41    tools_4.1.2    
  utils_4.1.2     xfun_0.32       yaml_2.3.5     

Pandoc version: 1.19.2.1

Here is a toy example to reproduce what I have

  1. installed brainspriteR package by devtools::install_github('oganm/brainspriteR')
  2. git clone https://github.com/oganm/brainspriteR.git just to get the demo figure ./brainspriteR/docs/sprite.jpg
  3. create an R script (testbrainsprite.R) as follows
#' ---
#' author: "aaa"
#' date: "`r format(Sys.time(), '%B, %Y')`"
#' linkcolor: blue
#' output:
#'   html_document:
#'     fig_caption: yes
#'     theme: spacelab #sandstone #spacelab #flatly
#'     highlight: default
#'     toc: FALSE
#'     toc_depth: 3
#'     number_sections: FALSE
#'     toc_float:
#'      smooth_scroll: FALSE
#' ---

#' ```{css, echo=FALSE}
#' pre {
#'   max-height: 300px;
#'   overflow-y: auto;
#' }
#' 
#' pre[class] {
#'   max-height: 600px;
#' }
#' ```

#+ include = FALSE
# use ragg for better figure quality, here I've tried both ragg and CairoPNG and converted demo figure to png
# knitr::opts_chunk$set(dev="CairoPNG")
#knitr::opts_chunk$set(dev="CairoPNG", out.width = "100%", fig.align = "center") 

#' ### Load library and utility functions
# ---------------------------------------------------------------------------------------------
#+ warning = FALSE, message = FALSE
library(ragg)
library(knitr)
library(rmarkdown)
library(brainspriteR)

#' ### Just to make sure the figure is accessable
filemap <- '/home/djangoc/brainspriteR/docs/sprite.jpg'
knitr::include_graphics(filemap)

brainsprite(sprites = filemap,
            flagCoordinates = F,
            spriteHeight = 233,
            spriteWidth = 189)

  1. render this testbrainsprite.R script in Rstudio or R opened in a terminal
rmarkdown::render('/home/testbrainsprite.R', output_format = "html_document", output_dir = '/home', output_file = 'testbrainsprite.html')

Would it be possible it is related to this issue Running rmarkdown from the command line without the need for X11 capability · Issue #1100 · rstudio/rmarkdown · GitHub? I have tried using CairoPNGbut it didn't solve the problem.

And here is some info of the rstudioserver module I loaded

help([[
Description
===========
RStudio Server enables you to provide a browser
based interface to a version of R running on a remote Linux
server, bringing the power and productivity of the
RStudio IDE to server-based deployments of R.


More information
================
 - Homepage: https://www.rstudio.com/
]])
whatis("Description: RStudio Server enables you to provide a browser
based interface to a version of R running on a remote Linux
server, bringing the power and productivity of the
RStudio IDE to server-based deployments of R.")
whatis("Homepage: https://www.rstudio.com/")
whatis("URL: https://www.rstudio.com/")
conflict("rstudio-server")
depends_on("flexiblascore/.3.0.4")
depends_on("java/13.0.2")
prepend_path("CMAKE_PREFIX_PATH","/cvmfs/soft.computecanada.ca/easybuild/software/2020/avx512/Core/rstudio-server/2021.09.1+372")
prepend_path("PATH","/cvmfs/soft.computecanada.ca/easybuild/software/2020/avx512/Core/rstudio-server/2021.09.1+372/bin")
setenv("EBROOTRSTUDIOMINSERVER","/cvmfs/soft.computecanada.ca/easybuild/software/2020/avx512/Core/rstudio-server/2021.09.1+372")
setenv("EBVERSIONRSTUDIOMINSERVER","2021.09.1+372")
setenv("EBDEVELRSTUDIOMINSERVER","/cvmfs/soft.computecanada.ca/easybuild/software/2020/avx512/Core/rstudio-server/2021.09.1+372/easybuild/avx512-Core-rstudio-server-2021.09.1+372-easybuild-devel")
depends_on("r/4.1.2")

If Pandoc version are different, then you'll have possibly different result. Especially when the version are really different!

This is really an old version. With Pandoc < 2.0, there will be a lot of feature that would not be working in your HPC environment.

Ideally, you need to make Pandoc version the same in both your environment, or at least have a recent Pandoc version in your HPC environment.

Pandoc is a self contained binary, which is quite portable and you can use an environment variable or a R command to make it found by rmarkdown - Putting it in PATH is better but not mandatory.

1 Like

Thanks a lot @cderv, it's now working by specifying the Pandoc path.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.