pharmaverse / logrx

Tools to facilitate logging in a clinical environment with the goal of making code easily traceable and reproducible.
https://pharmaverse.github.io/logrx/
Other
41 stars 6 forks source link

Bug: functions passed to `rlang::exprs` in an R script are not found #225

Open missuse opened 1 month ago

missuse commented 1 month ago

What happened?

Hi, and thanks for a great package.

Running logrx::axecute from a clean session on the admiral adsl script (https://pharmaverse.github.io/admiral/articles/adsl.html) results in an Error:

Caused by error in `format_eosstt()`:
! could not find function "format_eosstt"

A reproducible example with a smaller script is provided bellow.

Thank you

Session Information

R version 4.4.1 (2024-06-14 ucrt) Platform: x86_64-w64-mingw32/x64 Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale: [1] LC_COLLATE=English_United States.utf8 LC_CTYPE=English_United States.utf8 LC_MONETARY=English_United States.utf8 LC_NUMERIC=C
[5] LC_TIME=English_United States.utf8

time zone: Europe/Budapest tzcode source: internal

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] pharmaversesdtm_1.0.0 dplyr_1.1.4 admiral_1.1.1

loaded via a namespace (and not attached): [1] utf8_1.2.4 generics_0.1.3 tidyr_1.3.1 renv_1.0.9 tcltk_4.4.1 xml2_1.3.6 stringi_1.8.4 hms_1.1.3 digest_0.6.35 magrittr_2.0.3
[11] timechange_0.3.0 fastmap_1.2.0 processx_3.8.4 sessioninfo_1.2.2 backports_1.5.0 ps_1.7.6 promises_1.3.0 admiraldev_1.1.0 purrr_1.0.2 fansi_1.0.6
[21] lintr_3.1.2 lazyeval_0.2.2 cli_3.6.2 shiny_1.8.1.1 rlang_1.1.3 crayon_1.5.2 remotes_2.5.0 withr_3.0.0 tools_4.4.1 logrx_0.3.1
[31] httpuv_1.6.15 vctrs_0.6.5 waiter_0.2.5 cyclocomp_1.1.1 R6_2.5.1 mime_0.12 lifecycle_1.0.4 lubridate_1.9.3 stringr_1.5.1 miniUI_0.1.1.1
[41] pkgconfig_2.0.3 desc_1.4.3 callr_3.7.6 rex_1.2.1 pillar_1.9.0 later_1.3.2 data.table_1.15.4 glue_1.7.0 Rcpp_1.0.12 tibble_3.2.1
[51] tidyselect_1.2.1 rstudioapi_0.16.0 xtable_1.8-4 htmltools_0.5.8.1 compiler_4.4.1

Reproducible Example

Create an adsl_script.R with following contents:

library(admiral)
library(dplyr, warn.conflicts = FALSE)
library(pharmaversesdtm)

data("dm")
data("ds")

dm <- convert_blanks_to_na(dm)
ds <- convert_blanks_to_na(ds)

adsl <- dm %>%
  select(-DOMAIN)

format_eosstt <- function(x) {
  case_when(
    x %in% c("COMPLETED") ~ "COMPLETED",
    x %in% c("SCREEN FAILURE") ~ NA_character_,
    TRUE ~ "DISCONTINUED"
  )
}

adsl <- adsl %>%
  derive_vars_merged(
    dataset_add = ds,
    by_vars = exprs(STUDYID, USUBJID),
    filter_add = DSCAT == "DISPOSITION EVENT",
    new_vars = exprs(EOSSTT = format_eosstt(DSDECOD)),
    missing_values = exprs(EOSSTT = "ONGOING")
  )

from a clean R session create the log for the script:

rm(list = ls())

logrx::axecute(file = "adsl_script.R",
               log_name = "adsl_script.log")

the log will contain:

--------------------------------------------------------------------------------
-                             Errors and Warnings                              -
--------------------------------------------------------------------------------
Errors:
    In argument: `EOSSTT = format_eosstt(DSDECOD)`.

Warnings:

Full log contents:


--------------------------------------------------------------------------------
-                                logrx Metadata                                -
--------------------------------------------------------------------------------
This log was generated using logrx 0.3.1
logrx package version: 0.3.1
logrx build: CRAN (R 4.4.1)
logrx link to repository: https://github.com/pharmaverse/logrx
--------------------------------------------------------------------------------
-                          User and File Information                           -
--------------------------------------------------------------------------------
User: missu
File Name: adsl_script.R
File Path: D:/R/risk
File HashSum: 570a9f5e01c1f82d55098eacbea5c7b576540d74
--------------------------------------------------------------------------------
-                             Session Information                              -
--------------------------------------------------------------------------------
─ Session info ─────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.4.1 (2024-06-14 ucrt)
 os       Windows 10 x64 (build 19045)
 system   x86_64, mingw32
 ui       RStudio
 language (EN)
 collate  English_United States.utf8
 ctype    English_United States.utf8
 tz       Europe/Budapest
 date     2024-10-03
 rstudio  2024.09.0+375 Cranberry Hibiscus (desktop)
 pandoc   3.2 @
      C:\\PROGRA~1\\RStudio\\RESOUR~1\\app\\bin\\quarto\\bin\\tools\\pandoc.exe

─ Packages ─────────────────────────────────────────────────────────────────────
 package         * version date (UTC) lib source
 admiral         * 1.1.1   2024-06-17 [1] CRAN (R 4.4.1)
 admiraldev        1.1.0   2024-06-06 [1] CRAN (R 4.4.1)
 backports         1.5.0   2024-05-23 [1] CRAN (R 4.4.0)
 callr             3.7.6   2024-03-25 [1] CRAN (R 4.4.0)
 cli               3.6.2   2023-12-11 [1] CRAN (R 4.4.0)
 crayon            1.5.2   2022-09-29 [1] CRAN (R 4.4.0)
 cyclocomp         1.1.1   2023-08-30 [1] CRAN (R 4.4.1)
 data.table        1.15.4  2024-03-30 [1] CRAN (R 4.4.0)
 desc              1.4.3   2023-12-10 [1] CRAN (R 4.4.0)
 digest            0.6.35  2024-03-11 [1] CRAN (R 4.4.0)
 dplyr           * 1.1.4   2023-11-17 [1] CRAN (R 4.4.1)
 fansi             1.0.6   2023-12-08 [1] CRAN (R 4.4.0)
 fastmap           1.2.0   2024-05-15 [1] CRAN (R 4.4.0)
 generics          0.1.3   2022-07-05 [1] CRAN (R 4.4.0)
 glue              1.7.0   2024-01-09 [1] CRAN (R 4.4.0)
 hms               1.1.3   2023-03-21 [1] CRAN (R 4.4.0)
 htmltools         0.5.8.1 2024-04-04 [1] CRAN (R 4.4.0)
 httpuv            1.6.15  2024-03-26 [1] CRAN (R 4.4.0)
 later             1.3.2   2023-12-06 [1] CRAN (R 4.4.0)
 lazyeval          0.2.2   2019-03-15 [1] CRAN (R 4.4.1)
 lifecycle         1.0.4   2023-11-07 [1] CRAN (R 4.4.0)
 lintr             3.1.2   2024-03-25 [1] CRAN (R 4.4.1)
 logrx             0.3.1   2024-04-12 [1] CRAN (R 4.4.1)
 lubridate         1.9.3   2023-09-27 [1] CRAN (R 4.4.0)
 magrittr          2.0.3   2022-03-30 [1] CRAN (R 4.4.0)
 mime              0.12    2021-09-28 [1] CRAN (R 4.4.0)
 miniUI            0.1.1.1 2018-05-18 [1] CRAN (R 4.4.0)
 pharmaversesdtm * 1.0.0   2024-06-06 [1] CRAN (R 4.4.1)
 pillar            1.9.0   2023-03-22 [1] CRAN (R 4.4.0)
 pkgconfig         2.0.3   2019-09-22 [1] CRAN (R 4.4.0)
 processx          3.8.4   2024-03-16 [1] CRAN (R 4.4.0)
 promises          1.3.0   2024-04-05 [1] CRAN (R 4.4.0)
 ps                1.7.6   2024-01-18 [1] CRAN (R 4.4.0)
 purrr             1.0.2   2023-08-10 [1] CRAN (R 4.4.0)
 R6                2.5.1   2021-08-19 [1] CRAN (R 4.4.0)
 Rcpp              1.0.12  2024-01-09 [1] CRAN (R 4.4.0)
 remotes           2.5.0   2024-03-17 [1] CRAN (R 4.4.0)
 renv              1.0.9   2024-09-23 [1] CRAN (R 4.4.1)
 rex               1.2.1   2021-11-26 [1] CRAN (R 4.4.1)
 rlang             1.1.3   2024-01-10 [1] CRAN (R 4.4.0)
 rstudioapi        0.16.0  2024-03-24 [1] CRAN (R 4.4.0)
 sessioninfo       1.2.2   2021-12-06 [1] CRAN (R 4.4.0)
 shiny             1.8.1.1 2024-04-02 [1] CRAN (R 4.4.0)
 stringi           1.8.4   2024-05-06 [1] CRAN (R 4.4.0)
 stringr           1.5.1   2023-11-14 [1] CRAN (R 4.4.0)
 tibble            3.2.1   2023-03-20 [1] CRAN (R 4.4.0)
 tidyr             1.3.1   2024-01-24 [1] CRAN (R 4.4.0)
 tidyselect        1.2.1   2024-03-11 [1] CRAN (R 4.4.0)
 timechange        0.3.0   2024-01-18 [1] CRAN (R 4.4.0)
 utf8              1.2.4   2023-10-22 [1] CRAN (R 4.4.0)
 vctrs             0.6.5   2023-12-01 [1] CRAN (R 4.4.0)
 waiter            0.2.5   2022-01-03 [1] CRAN (R 4.4.1)
 withr             3.0.0   2024-01-16 [1] CRAN (R 4.4.0)
 xml2              1.3.6   2023-12-04 [1] CRAN (R 4.4.0)
 xtable            1.8-4   2019-04-21 [1] CRAN (R 4.4.0)

 [1] C:/Users/missu/AppData/Local/R/win-library/4.4
 [2] C:/Program Files/R/R-4.4.1/library

─ External software ────────────────────────────────────────────────────────────
 setting        value
 cairo          1.18.0
 cairoFT
 pango
 png            1.6.42
 jpeg           9.6
 tiff           LIBTIFF, Version 4.6.0
 tcl            8.6.13
 curl           8.6.0
 zlib           1.3.1
 bzlib          1.0.8, 13-Jul-2019
 xz             5.4.6
 deflate        1.19
 PCRE           10.43 2024-02-16
 ICU            74.2
 TRE            TRE 0.8.0 R_fixes (BSD)
 iconv          win_iconv
 readline
 BLAS
 lapack
 lapack_version 3.12.0

─ Python configuration ─────────────────────────────────────────────────────────
 Python is not available

────────────────────────────────────────────────────────────────────────────────
--------------------------------------------------------------------------------
-                               Masked Functions                               -
--------------------------------------------------------------------------------
function `ts` from {package:stats} by package:pharmaversesdtm
function `cm` from {package:grDevices} by package:pharmaversesdtm
function `%>%` from {package:admiral} by package:dplyr
function `desc` from {package:admiral} by package:dplyr
function `filter` from {package:stats} by package:dplyr
function `lag` from {package:stats} by package:dplyr
function `intersect` from {package:base} by package:dplyr
function `setdiff` from {package:base} by package:dplyr
function `setequal` from {package:base} by package:dplyr
function `union` from {package:base} by package:dplyr
function `plot` from {package:base} by package:graphics
function `body<-` from {package:base} by package:methods
function `kronecker` from {package:base} by package:methods
--------------------------------------------------------------------------------
-                          Used Package and Functions                          -
--------------------------------------------------------------------------------
{!!! NOT FOUND !!!} format_eosstt
{package:admiral} convert_blanks_to_na, derive_vars_merged, exprs
{package:base} library, c, %in%
{package:dplyr} %>%, select, case_when
{package:utils} data
--------------------------------------------------------------------------------
-                         Program Run Time Information                         -
--------------------------------------------------------------------------------
Start time: 2024-10-03 07:16:26 CEST
End time: 2024-10-03 07:16:27 CEST
Run time: 1 seconds
--------------------------------------------------------------------------------
-                             Errors and Warnings                              -
--------------------------------------------------------------------------------
Errors:
    In argument: `EOSSTT = format_eosstt(DSDECOD)`.

Warnings:

--------------------------------------------------------------------------------
-                         Messages, Output, and Result                         -
--------------------------------------------------------------------------------
Messages:

Output:

Result:
    NULL
--------------------------------------------------------------------------------
-                               Log Output File                                -
--------------------------------------------------------------------------------
Log name: adsl_script.log
Log path: D:/R/risk
bms63 commented 1 month ago

Are you running logrx::axecute() in the same file as this adsl script?

missuse commented 1 month ago

I am not, should I? Per my understanding of the documentation logrx::axecute() should be able to execute an R script provided as argument, and create a log based on the script run.

bms63 commented 1 month ago

Umm... I was able to run this code with no issues.

image

R version 4.4.1 (2024-06-14)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 20.04.6 LTS

Matrix products: default
BLAS/LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.8.so;  LAPACK version 3.9.0

locale:
 [1] LC_CTYPE=C.UTF-8       LC_NUMERIC=C           LC_TIME=C.UTF-8       
 [4] LC_COLLATE=C.UTF-8     LC_MONETARY=C.UTF-8    LC_MESSAGES=C.UTF-8   
 [7] LC_PAPER=C.UTF-8       LC_NAME=C              LC_ADDRESS=C          
[10] LC_TELEPHONE=C         LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C   

time zone: UTC
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] pharmaversesdtm_1.0.0 dplyr_1.1.4           admiral_1.1.1        

loaded via a namespace (and not attached):
 [1] miniUI_0.1.1.1    compiler_4.4.1    promises_1.3.0    logrx_0.3.1       tidyselect_1.2.1 
 [6] Rcpp_1.0.13       xml2_1.3.6        stringr_1.5.1     later_1.3.2       tidyr_1.3.1      
[11] fastmap_1.2.0     readxl_1.4.3      mime_0.12         R6_2.5.1          tcltk_4.4.1      
[16] generics_0.1.3    admiraldev_1.1.0  tibble_3.2.1      lubridate_1.9.3   shiny_1.9.1      
[21] pillar_1.9.0      rlang_1.1.4       utf8_1.2.4        stringi_1.8.4     httpuv_1.6.15    
[26] timechange_0.3.0  cli_3.6.3         withr_3.0.1       magrittr_2.0.3    digest_0.6.37    
[31] rstudioapi_0.16.0 xtable_1.8-4      metacore_0.1.3    hms_1.1.3         lifecycle_1.0.4  
[36] waiter_0.2.5      vctrs_0.6.5       glue_1.7.0        cellranger_1.1.0  sessioninfo_1.2.2
[41] fansi_1.0.6       purrr_1.0.2       tools_4.4.1       pkgconfig_2.0.3   htmltools_0.5.8.1
bms63 commented 1 month ago

@kodesiba @nicholas-masel maybe an OS issue??? Do you guys have windows machines - I only have R on Linux :(

missuse commented 1 month ago

I wouldn't say its a OS issue, I have the same problem on windows and ubuntu. For me the function format_eosstt is picked up by logrx only if it is already loaded in the environment from which logrx::axecute is called, if it is not then the error occurs.

when clean global env error about function not found: logrx_problem

if format_eosstt is in the environment no problems: logrx_problem2

Please let me know if there is any action from me that would help in reproducing the problem on your side.

bms63 commented 1 month ago

I was able to reproduce error now - I thought I had cleared environment and restarted session.

image

While not ideal - I found that source worked as a workaround

image

bms63 commented 1 month ago

Also @missuse many thanks for the feedback!! I forgot to say that!! :)

missuse commented 1 month ago

Thank you Ben!

Will there be attempts to correct the behavior in logrx, or will using source to import user defined functions stay as the recommended way to overcome this problem?

bms63 commented 1 month ago

Yes we will look into! I'm not sure on the timeframe, as I think we will have to dive deep here to figure out why the search path is wonky here. That might be a more @kodesiba or @nicholas-masel as they are way more technical then me!!