insightsengineering / teal.data

Data model for teal applications
https://insightsengineering.github.io/teal.data/
Other
8 stars 7 forks source link

[Bug]: `get_code` does not retrieve code in some cases #310

Closed chlebowa closed 4 months ago

chlebowa commented 4 months ago

What happened?

Code retrieval fails if code passed as character but not if passed as language.

1. Create language obejct and evaluate to create data in global environment.
library(teal)

code <- bquote({
  ADSL <- teal.modules.clinical::tmc_ex_adsl
  ADRS <- teal.modules.clinical::tmc_ex_adrs |>
    dplyr::mutate(
      AVALC = tern::d_onco_rsp_label(AVALC) |>
        formatters::with_label("Character Result/Finding")
    ) |>
    dplyr::filter(PARAMCD != "OVRINV" | AVISIT == "FOLLOW UP")
})
eval(code)
2. Create teal_data by passing code as is.

Code retrieved correctly.

data1 <- teal.data::cdisc_data(
  ADRS = ADRS,
  ADSL = ADSL,
  code = code
)
teal.data::get_code(data, dataname = "ADRS")

[1] "warning('Code was not verified for reproducibility.')\nADRS <- dplyr::filter(dplyr::mutate(teal.modules.clinical::tmc_ex_adrs, AVALC = formatters::with_label(tern::d_onco_rsp_label(AVALC), \"Character Result/Finding\")), PARAMCD != \"OVRINV\" | AVISIT == \"FOLLOW UP\")"
3. Create teal_data by passing code as character.

Code retrieved fails.

data2 <- teal.data::cdisc_data(
  ADRS = ADRS,
  ADSL = ADSL,
  code = code |> deparse()
)
teal.data::get_code(data, dataname = "ADRS")

[1] "warning('Code was not verified for reproducibility.')"
Warning message:
In sym_cond > ass_cond :
  longer object length is not a multiple of shorter object length

This only occurs with datanames specified.

sessionInfo()

R version 4.3.3 (2024-02-29 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)

Matrix products: default

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

time zone: Europe/Warsaw
tzcode source: internal

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

other attached packages:
[1] teal_0.15.2      teal.slice_0.5.1 teal.data_0.6.0  teal.code_0.5.0
[5] shiny_1.8.1.1

loaded via a namespace (and not attached):
 [1] sandwich_3.1-0              utf8_1.2.4
 [3] generics_0.1.3              tidyr_1.3.1
 [5] tern.gee_0.1.3              lattice_0.22-5
 [7] digest_0.6.35               magrittr_2.0.3
 [9] estimability_1.5            grid_4.3.3
[11] mvtnorm_1.2-4               fastmap_1.1.1
[13] Matrix_1.6-5                backports_1.4.1
[15] survival_3.5-8              multcomp_1.4-25
[17] promises_1.3.0              purrr_1.0.2
[19] fansi_1.0.6                 scales_1.3.0
[21] TH.data_1.1-2               codetools_0.2-19
[23] Rdpack_2.6                  cli_3.6.2
[25] teal.transform_0.5.0        rlang_1.1.3
[27] rbibutils_2.2.16            splines_4.3.3
[29] munsell_0.5.1               geepack_1.3.10
[31] tools_4.3.3                 checkmate_2.3.1
[33] teal.modules.clinical_0.9.1 dplyr_1.1.4
[35] colorspace_2.1-0            ggplot2_3.5.0
[37] httpuv_1.6.15               formatters_0.5.6
[39] broom_1.0.5                 rtables_0.6.7
[41] vctrs_0.6.5                 logger_0.3.0
[43] R6_2.5.1                    mime_0.12
[45] zoo_1.8-12                  emmeans_1.10.1
[47] lifecycle_1.0.4             MASS_7.3-60.0.1
[49] pkgconfig_2.0.3             teal.logger_0.2.0
[51] pillar_1.9.0                later_1.3.2
[53] gtable_0.3.4                glue_1.7.0
[55] Rcpp_1.0.12                 tibble_3.2.1
[57] tidyselect_1.2.1            xtable_1.8-4
[59] nlme_3.1-164                htmltools_0.5.8.1
[61] compiler_4.3.3              tern_0.9.4

Relevant log output

No response

Code of Conduct

Contribution Guidelines

Security Policy

chlebowa commented 4 months ago

Oddly, this works just fine:

ADSL <- teal.modules.clinical::tmc_ex_adsl
ADRS <- teal.modules.clinical::tmc_ex_adrs |>
  dplyr::mutate(
    AVALC = tern::d_onco_rsp_label(AVALC) |>
      formatters::with_label("Character Result/Finding")
  ) |>
  dplyr::filter(PARAMCD != "OVRINV" | AVISIT == "FOLLOW UP")

code <- '
  ADSL <- teal.modules.clinical::tmc_ex_adsl
  ADRS <- teal.modules.clinical::tmc_ex_adrs |>
    dplyr::mutate(
      AVALC = tern::d_onco_rsp_label(AVALC) |>
        formatters::with_label("Character Result/Finding")
    ) |>
    dplyr::filter(PARAMCD != "OVRINV" | AVISIT == "FOLLOW UP")
'
data3 <- teal.data::cdisc_data(
  ADRS = ADRS,
  ADSL = ADSL,
  code = code
)
teal.data::get_code(data3, dataname = "ADRS")

[1] "warning('Code was not verified for reproducibility.')\nADRS <- dplyr::filter(dplyr::mutate(teal.modules.clinical::tmc_ex_adrs, AVALC = formatters::with_label(tern::d_onco_rsp_label(AVALC), \"Character Result/Finding\")), PARAMCD != \"OVRINV\" | AVISIT == \"FOLLOW UP\")"
m7pr commented 4 months ago

@chlebowa thanks for pointing that.

It actually works for

code <- '
  ADSL <- teal.modules.clinical::tmc_ex_adsl
  ADRS <- teal.modules.clinical::tmc_ex_adrs |>
    dplyr::mutate(
      AVALC = tern::d_onco_rsp_label(AVALC) |>
        formatters::with_label("Character Result/Finding")
    ) |>
    dplyr::filter(PARAMCD != "OVRINV" | AVISIT == "FOLLOW UP")
'
teal.data::get_code(teal_data(ADRS = ADRS, ADSL = ADSL, code = code), dataname = "ADRS")

and fails for

code2 <- 
'{     
  ADSL <- teal.modules.clinical::tmc_ex_adsl
  ADRS <- teal.modules.clinical::tmc_ex_adrs |>
    dplyr::mutate(
      AVALC = tern::d_onco_rsp_label(AVALC) |>
        formatters::with_label("Character Result/Finding")
    ) |>
    dplyr::filter(PARAMCD != "OVRINV" | AVISIT == "FOLLOW UP")
}'
teal.data::get_code(teal_data(ADRS = ADRS, ADSL = ADSL, code = code2), dataname = "ADRS")

where the only difference is that the second case has {} at the beginning and at the end. This is also the case with code |> deparse() that adds those parenthesis around the code. I think we should investigate and improve the parser so that it removes {} curly brackets before it parses the code

m7pr commented 4 months ago

Provided a fix for this in https://github.com/insightsengineering/teal.data/pull/311