teal icon indicating copy to clipboard operation
teal copied to clipboard

Additional (unnecessary?) attached libraries in get r code

Open kpagacz opened this issue 3 years ago • 2 comments

When running this example:

library(scda)
library(dplyr)
library(teal.modules.clinical)

adsl <- synthetic_cdisc_data("latest")$adsl
adex <- synthetic_cdisc_data("latest")$adex

set.seed(1, kind = "Mersenne-Twister")
adex <- adex %>%
  distinct(USUBJID, .keep_all = TRUE) %>%
  mutate(
    PARAMCD = "TDURD",
    PARAM = "Overall duration (days)",
    AVAL = sample(x = seq(1, 200), size = n(), replace = TRUE),
    AVALU = "Days"
  ) %>%
  bind_rows(adex)

app <- init(
  data = cdisc_data(
    cdisc_dataset("ADSL", adsl, code = 'ADSL <- synthetic_cdisc_data("latest")$adsl'),
    cdisc_dataset("ADEX", adex, code = 'ADEX <- synthetic_cdisc_data("latest")$adex
  set.seed(1, kind = "Mersenne-Twister")
  ADEX <- ADEX %>%
  distinct(USUBJID, .keep_all = TRUE) %>%
  mutate(PARAMCD = "TDURD",
         PARAM = "Overall duration (days)",
         AVAL = sample(x = seq(1, 200), size = n(), replace = TRUE),
         AVALU = "Days") %>%
  bind_rows(ADEX)'),
    check = TRUE
  ),
  modules = modules(
    tm_t_exposure(
      label = "Duration of Exposure Table",
      dataname = "ADEX",
      paramcd = choices_selected(
        choices = value_choices(adex, "PARAMCD", "PARAM"),
        selected = "TDURD"
      ),
      col_by_var = choices_selected(
        choices = variable_choices(adex, subset = c("ARM")),
        selected = NULL
      ),
      row_by_var = choices_selected(
        choices = variable_choices(adex, subset = c("ETHNIC", "SEX")),
        selected = "ETHNIC"
      ),
      parcat = choices_selected(
        choices = value_choices(adex, "PARCAT2"),
        selected = "Drug A"
      ),
      add_total = FALSE
    )
  ),
  filter = list(
    ADSL = list(SAFFL = "Y")
  )
)

shinyApp(app$ui, app$server)

I noticed show r code attaches all the behind-the-scenes libraries which might not be needed image

kpagacz avatar Mar 24 '22 13:03 kpagacz

This comes from the design of how we recognize dependencies, utils::sessionInfo()$otherPkgs. https://github.com/insightsengineering/teal/blob/a0963efad810b5bf2f7e24c6abe2852366383d3f/R/get_rcode_utils.R#L41

I think we should leave it if there is no clever move to reduce the list of dependencies. The static removal of packages which we think are not needed do not have to be stable as we could change sth in the future. teal.data could be used e.g. to rise the connection. On the other hand I could imagine that teal.slice/shiny/testthat/... will be never used in the show R code.

dependencies <- utils::sessionInfo()$otherPkgs
# this process is dangerous
setdiff(dependencies, c("teal.X", "testthat"))

For sure there could be more advanced solutions, like specifying dependencies when chunks are built.

Polkas avatar Mar 28 '22 06:03 Polkas

From @mhallal1

Please refer to the comments by @Polkas in https://github.com/insightsengineering/teal/issues/752#issuecomment-1313285773

We should not add blindly all library calls for each SHow R Code call. We have many options to add only needed dependencies, for each module.

linked to https://github.com/insightsengineering/teal.code/issues/93

gogonzo avatar Nov 24 '22 10:11 gogonzo

Closing this one. We can't separate output-related and app-related libraries. We moving to the solution where we provide library calls by evaluating them in teal_data object.

teal_data() |> within({library(...)})

gogonzo avatar May 24 '24 07:05 gogonzo