ThinkR-open / attachment

Tools to deal with dependencies in scripts, Rmd and packages
https://thinkr-open.github.io/attachment/
Other
108 stars 13 forks source link

Call to fusen::inflate modifies warning level in global options #94

Closed gdurif closed 1 year ago

gdurif commented 1 year ago

It appears that calling the function fusen::inflate() modifies the warning level in the user global R options from 0 (default) to -1 (ignore all warnings), without advertising it, which is a problem when you are then doing some development (e.g. in Rstudio) expecting some code to throw a warning.

I encountered this problem both inside Rstudio and in R console from the command line.

Here is a minimum working example (from the fusen::inflate() man page).

# warning option
options("warn")

    ## $warn
    ## [1] 0

# Create a new project
dummypackage <- tempfile("dummypackage")
dir.create(dummypackage)

# {fusen} steps 1
dev_file <- fusen::add_flat_template(
    template = "full", pkg = dummypackage, overwrite = TRUE)

    ## Created file .here in /tmp/Rtmpzolcwa/dummypackage4188925db7dfa . Please start a new R session in the new project directory.

# warning option
options("warn")

    ## $warn
    ## [1] 0

# {fusen} steps 2
fusen::fill_description(
    pkg = dummypackage, fields = list(Title = "Dummy Package"))

    ## [1] "/tmp/Rtmpzolcwa/dummypackage4188925db7dfa/DESCRIPTION"

# warning option
options("warn")

    ## $warn
    ## [1] 0

# {fusen} steps 3
flat_file <- dev_file[grepl("flat", dev_file)]
fusen::inflate(
    pkg = dummypackage, flat_file = flat_file,
    vignette_name = "Exploration of my Data", check = FALSE
)

    ## ✔ Setting active project to '/tmp/RtmpgzmK7n/dummypackage419b7345c4985'
    ## ℹ Loading dummypackage419b7345c4985
    ## Writing NAMESPACE
    ## Loading required namespace: testthat
    ## ✔ Adding 'knitr' to Suggests field in DESCRIPTION
    ## ✔ Setting VignetteBuilder field in DESCRIPTION to 'knitr'
    ## ✔ Adding 'inst/doc' to '.gitignore'
    ## Updating dummypackage419b7345c4985 documentation
    ## ℹ Loading dummypackage419b7345c4985
    ## Writing NAMESPACE
    ## Writing my_median.Rd
    ## Writing my_other_median.Rd
    ## Writing sub_median.Rd
    ## ℹ Loading dummypackage419b7345c4985
    ## [+] 3 package(s) added: stats, rmarkdown, testthat.
    ## [1] "/tmp/RtmpgzmK7n/dummypackage419b7345c4985"

# warning option
options("warn")

    ## $warn
    ## [1] -1

And here is my sessionInfo():

## R version 4.2.2 (2022-10-31)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Arch Linux
## 
## Matrix products: default
## BLAS:   /usr/lib/libblas.so.3.11.0
## LAPACK: /usr/lib/liblapack.so.3.11.0
## 
## locale:
##  [1] LC_CTYPE=en_IE.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_IE.UTF-8        LC_COLLATE=en_IE.UTF-8    
##  [5] LC_MONETARY=en_IE.UTF-8    LC_MESSAGES=en_IE.UTF-8   
##  [7] LC_PAPER=en_IE.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_IE.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] dummypackage41a847f926621_0.0.0.9000 conflicted_1.1.0                    
## 
## loaded via a namespace (and not attached):
##  [1] tidyselect_1.2.0 xfun_0.36        parsermd_0.1.2   purrr_1.0.1     
##  [5] vctrs_0.5.1      generics_0.1.3   testthat_3.1.6   htmltools_0.5.4 
##  [9] usethis_2.1.6    yaml_2.3.6       utf8_1.2.2       rlang_1.0.6     
## [13] pillar_1.8.1     glue_1.6.2       withr_2.5.0      DBI_1.1.3       
## [17] fusen_0.4.1      lifecycle_1.0.3  stringr_1.5.0    commonmark_1.8.1
## [21] memoise_2.0.1    evaluate_0.19    knitr_1.41       tzdb_0.3.0      
## [25] fastmap_1.1.0    fansi_1.0.3      attachment_0.3.1 Rcpp_1.0.9      
## [29] readr_2.1.3      backports_1.4.1  checkmate_2.1.0  cachem_1.0.6    
## [33] desc_1.4.2       pkgload_1.3.2    fs_1.5.2         brio_1.1.3      
## [37] digest_0.6.31    hms_1.1.2        stringi_1.7.12   dplyr_1.0.10    
## [41] rprojroot_2.0.3  here_1.0.1       cli_3.6.0        tools_4.2.2     
## [45] magrittr_2.0.3   tibble_3.1.8     crayon_1.5.2     tidyr_1.2.1     
## [49] pkgconfig_2.0.3  ellipsis_0.3.2   xml2_1.3.3       assertthat_0.2.1
## [53] rmarkdown_2.19   roxygen2_7.2.3   rstudioapi_0.14  R6_2.5.1        
## [57] compiler_4.2.2
statnmap commented 1 year ago

That is a good point.
This is due to {attachment} as att_from_rmd() changes the warn level.
We will need to change this part of the code to set back the warning level to the one of the user, even if we suggest to purl with level -1 to avoid to much information for this purl() part: https://github.com/ThinkR-open/attachment/blob/4772336e1163fc81e40006c50c7dd4d2da553024/R/att_from_rmds.R#L37

We will get into this.

At the end, that would be something like


  cat(
    paste0('warn_user <- getOption("warn");',
           'options(warn=', warn, ');',
           'invisible(knitr::purl("', gsub("\\", "\\\\", path, fixed = TRUE), '"',
           ', output = "', gsub("\\", "\\\\", r_file, fixed = TRUE), '"',
           ', encoding = "', encoding, '"',
           ', documentation = 0, quiet = TRUE));',
           'options(warn=warn_user)'
    , file = runR)

And we will want to add a unit test to verify that getOption("warn") is the same before and after att_from_rmd(), and also before and after att_amend_desc()

statnmap commented 1 year ago

In PR https://github.com/ThinkR-open/attachment/pull/96, this was fixed Soon on 'r-universe', then CRAN :smiley:

Validation

# warning option
options("warn")
#> $warn
#> [1] 0

# Create a new project
dummypackage <- tempfile("dummypackage")
dir.create(dummypackage)

# {fusen} steps 1
dev_file <- fusen::add_flat_template(
  template = "full", pkg = dummypackage, overwrite = TRUE)
#> Created file .here in /tmp/RtmpLKbCc0/dummypackage94cd4ae2f425 . Please start a new R session in the new project directory.

# warning option
options("warn")
#> $warn
#> [1] 0

# {fusen} steps 2
fusen::fill_description(
  pkg = dummypackage, fields = list(Title = "Dummy Package"))
#> [1] "/tmp/RtmpLKbCc0/dummypackage94cd4ae2f425/DESCRIPTION"

# warning option
options("warn")
#> $warn
#> [1] 0

# {fusen} steps 3
flat_file <- dev_file[grepl("flat", dev_file)]
fusen::inflate(
  pkg = dummypackage, flat_file = flat_file,
  vignette_name = "Exploration of my Data", check = FALSE
)
#> ✔ Setting active project to '/tmp/RtmpLKbCc0/dummypackage94cd4ae2f425'
#> ℹ Loading dummypackage94cd4ae2f425
#> Writing 'NAMESPACE'
#> Le chargement a nécessité le package : testthat
#> 
#> ✔ Adding 'knitr' to Suggests field in DESCRIPTION
#> ✔ Setting VignetteBuilder field in DESCRIPTION to 'knitr'
#> ✔ Adding 'inst/doc' to '.gitignore'
#> ── Updating config file for dev/flat_full.Rmd ──────────────────────────────────
#> ✔ path: dev/flat_full.Rmd was added to the config file
#> ✔ state: active was added to the config file
#> ✔ R: R/my_median.R was added to the config file
#> ✔ R: R/my_other_median.R was added to the config file
#> ✔ tests: tests/testthat/test-my_median.R was added to the config file
#> ✔ tests: tests/testthat/test-my_other_median.R was added to the config file
#> ✔ vignettes: vignettes/exploration-of-my-data.Rmd was added to the config file
#> ℹ config file created: dev/config_fusen.yaml
#> Saving attachment parameters to yaml config file
#> Updating dummypackage94cd4ae2f425 documentation
#> ℹ Loading dummypackage94cd4ae2f425Writing 'NAMESPACE'Writing 'my_median.Rd'Writing 'my_other_median.Rd'Writing 'sub_median.Rd'ℹ Loading dummypackage94cd4ae2f425[+] 3 package(s) added: stats, rmarkdown, testthat.
#> [1] "/tmp/RtmpLKbCc0/dummypackage94cd4ae2f425"

# warning option
options("warn")
#> $warn
#> [1] 0

Created on 2023-04-21 with reprex v2.0.2