Skip to content

Difference in behavior for before/after_chapter_script and new_session: true/false #1252

@singmann

Description

@singmann

Sorry for the chunky title, but this seems to be a bug then only shows up in a very specific situation: If using the Knit-Merge strategy (i.e., new_session: true in _bookdown.yml) and a before_chapter_script (e.g., _common.R), fig.width in the before_chapter_script seems to be ignored. This does not seem to apply to other chunk options as far as I can tell, but particularly to fig.width. fig.width can also still be used directly in the chunk and works, but the global one is ignored.

The easiest way to reproduce the bug is the following steps:

  1. Create a new project using the new bs4_book through the RStudio menu interface (New Project, ...). This ensures that _common.R is present and new_session: true.
  2. Add the following code chunk to the index.Rmd file:
    ```{r tmp}
    par(mar = c(4, 4, .1, .1))
    plot(pressure, type = 'b', pch = 19)
    ```
    
  3. Change the global knitr options in _common.R to:
    # example chunk options set globally
    knitr::opts_chunk$set(
      comment = "#>",
      collapse = TRUE, 
      out.width = "25%", 
      fig.width = 3, 
      fig.asp = 0.9
      )
    

Feel free to play around with fig.width and see that changing it does not change the width of the figure in the output, unless you change new_session: true to new_session: false in _bookdown.yml or set it directly in the chunk. As far as I can tell the other figure options changed here (i.e., out.width and fig.asp) seem to work.

Also, this seems to be related to a comment in #36, #36 (comment)_

It also seems that an example of the bug can be found in the wild in the current version of "R for data science". At the moment, the width of the first figure in the data visualisation chapter is 1344 pixels. However, it appears it should be 1152 pixels because the _common.R file sets the fig.width to 6 (at least when setting fig.width of a chunk to 7, I also get 1344, and with 6, 1152).

My system:

> xfun::session_info('bookdown')
R version 4.1.1 (2021-08-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043), RStudio 1.4.1707

Locale:
  LC_COLLATE=English_United Kingdom.1252 
  LC_CTYPE=English_United Kingdom.1252   
  LC_MONETARY=English_United Kingdom.1252
  LC_NUMERIC=C                           
  LC_TIME=English_United Kingdom.1252    

Package version:
  base64enc_0.1.3 bookdown_0.24.1 digest_0.6.27   evaluate_0.14  
  fastmap_1.1.0   glue_1.4.2      graphics_4.1.1  grDevices_4.1.1
  highr_0.9       htmltools_0.5.2 jquerylib_0.1.4 jsonlite_1.7.2 
  knitr_1.33      magrittr_2.0.1  markdown_1.1    methods_4.1.1  
  mime_0.11       rlang_0.4.11    rmarkdown_2.10  stats_4.1.1    
  stringi_1.7.4   stringr_1.4.0   tinytex_0.33    tools_4.1.1    
  utils_4.1.1     xfun_0.25       yaml_2.2.1 

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions