Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to set datanames #824

Open
wants to merge 63 commits into
base: main
Choose a base branch
from
Open

Conversation

llrs-roche
Copy link
Contributor

@llrs-roche llrs-roche commented Jan 8, 2025

Pull Request

Fixes #821

This PR add a parameter (or uses existing ones) to use on teal::module(datanames) to select which datasets are shown in each module.

tm_file_viewer() doesn't have and I think shouldn't have.

Acceptance criteria:

tm_missing_data example
# general example data

library("devtools")
load_all("../teal.modules.general")
load_all("../teal.code")
load_all("../teal.transform")
data <- teal_data()
data <- within(data, {
  require(nestcolor)
  
  add_nas <- function(x) {
    x[sample(seq_along(x), floor(length(x) * runif(1, .05, .17)))] <- NA
    x
  }
  
  iris <- iris
  .iris <- iris
  iris2 <- iris
  mtcars <- mtcars
  
  iris[] <- lapply(iris, add_nas)
  .iris[] <- lapply(.iris, add_nas)
  iris2[] <- lapply(iris2, add_nas)
  mtcars[] <- lapply(mtcars, add_nas)
  mtcars[["cyl"]] <- as.factor(mtcars[["cyl"]])
  mtcars[["gear"]] <- as.factor(mtcars[["gear"]])
})

app <- init(
  data = data,
  modules = modules(
    tm_missing_data(datasets_selected = c("iris2", "iris"))
  )
)
if (interactive()) {
  shinyApp(app$ui, app$server)
}
tm_variable_browser example
library("devtools")
load_all("../teal.modules.general")
load_all("../teal.code")
load_all("../teal.transform")
# general data example
data <- teal_data()
data <- within(data, {
  iris <- iris
  mtcars <- mtcars
  women <- women
  faithful <- faithful
  CO2 <- CO2
})

app <- init(
  data = data,
  modules = modules(
    tm_variable_browser(
      label = "Variable browser",
      datasets_selected = c("iris", "mtcars")
    )
  )
)
if (interactive()) {
  shinyApp(app$ui, app$server)
}
tm_front_page (additional modifications to enable granular selection): Example
library("devtools")
load_all("../teal.modules.general")
load_all("../teal.code")
load_all("../teal.transform")
data <- teal_data()
data <- within(data, {
  require(nestcolor)
  ADSL <- teal.data::rADSL
  ADSL2 <- ADSL
  attr(ADSL, "metadata") <- list("Author" = "NEST team", "data_source" = "synthetic data")
})
join_keys(data) <- default_cdisc_join_keys[names(data)]

table_1 <- data.frame(Info = c("A", "B"), Text = c("A", "B"))
table_2 <- data.frame(`Column 1` = c("C", "D"), `Column 2` = c(5.5, 6.6), `Column 3` = c("A", "B"))
table_3 <- data.frame(Info = c("E", "F"), Text = c("G", "H"))

table_input <- list(
  "Table 1" = table_1,
  "Table 2" = table_2,
  "Table 3" = table_3
)

app <- init(
  data = data,
  modules = modules(
    tm_front_page(
      header_text = c(
        "Important information" = "It can go here.",
        "Other information" = "Can go here."
      ),
      tables = table_input,
      additional_tags = HTML("Additional HTML or shiny tags go here <br>"),
      footnotes = c("X" = "is the first footnote", "Y is the second footnote"),
      show_metadata = TRUE,
      datasets_selected = "ADSL2"
    )
  ),
  header = tags$h1("Sample Application"),
  footer = tags$p("Application footer"),
)

if (interactive()) {
  shinyApp(app$ui, app$server)
}

I checked via searching datanames = that there are now at least 15 cases in module() corresponding to the 15 modules exported.
image

There is one that is not configurable (datanames = NULL on the picture above), tm_file_viewer. In my opinion it doesn't matter the datasets available on the module to visualize files. But if needed we can add a dataset_selected argument.

Note that I added the dataset_selected argument to two modules on the best place I thought, if a package/user is using positional arguments it might break (and also teal.gallery).

Note that it doesn't show on shinylive (on Rstudio, preview, not sure if once fully installed) and also that it doesn't show the url (this could be an issue with my machine):

tmg_tm_file_viewer_datanames

@llrs-roche llrs-roche added the core label Jan 8, 2025
Copy link
Contributor

github-actions bot commented Jan 8, 2025

Unit Tests Summary

  1 files   22 suites   12m 58s ⏱️
145 tests 145 ✅ 0 💤 0 ❌
477 runs  477 ✅ 0 💤 0 ❌

Results for commit 4f8e966.

♻️ This comment has been updated with latest results.

Copy link
Contributor

github-actions bot commented Jan 8, 2025

Unit Test Performance Difference

Test Suite $Status$ Time on main $±Time$ $±Tests$ $±Skipped$ $±Failures$ $±Errors$
examples 💔 $0.59$ $+5.83$ $+1$ $-38$ $0$ $0$
shinytest2-tm_a_pca 💚 $118.46$ $-1.13$ $0$ $0$ $0$ $0$
shinytest2-tm_a_regression 💚 $52.35$ $-1.04$ $0$ $0$ $0$ $0$
shinytest2-tm_g_distribution 💚 $60.35$ $-1.70$ $0$ $0$ $0$ $0$
shinytest2-tm_g_scatterplotmatrix 💚 $27.88$ $-1.13$ $0$ $0$ $0$ $0$
shinytest2-tm_misssing_data 💚 $48.39$ $-1.58$ $0$ $0$ $0$ $0$
shinytest2-tm_outliers 💚 $113.79$ $-4.69$ $0$ $0$ $0$ $0$
shinytest2-tm_t_crosstable 💚 $32.05$ $-1.36$ $0$ $0$ $0$ $0$
shinytest2-tm_variable_browser 💚 $58.68$ $-1.34$ $0$ $0$ $0$ $0$
Additional test case details
Test Suite $Status$ Time on main $±Time$ Test Case
examples 💔 $0.08$ $+1.63$ example_add_facet_labels.Rd
examples 💔 $0.01$ $+1.00$ example_tm_g_distribution.Rd
shinytest2-tm_g_distribution 💚 $32.52$ $-1.33$ e2e_tm_g_distribution_Module_is_initialised_with_the_specified_defaults.

Results for commit 88bde23

♻️ This comment has been updated with latest results.

R/tm_front_page.R Outdated Show resolved Hide resolved
R/tm_missing_data.R Outdated Show resolved Hide resolved
@gogonzo gogonzo self-assigned this Jan 10, 2025
R/tm_data_table.R Outdated Show resolved Hide resolved
R/tm_missing_data.R Outdated Show resolved Hide resolved
R/tm_missing_data.R Outdated Show resolved Hide resolved
Co-authored-by: Dawid Kałędkowski <[email protected]>
Signed-off-by: Lluís Revilla <[email protected]>
Copy link
Contributor Author

@llrs-roche llrs-roche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I'll fix the style checks for the documentation too.

R/tm_missing_data.R Outdated Show resolved Hide resolved
NEWS.md Outdated Show resolved Hide resolved
R/tm_data_table.R Outdated Show resolved Hide resolved
R/tm_data_table.R Show resolved Hide resolved
R/tm_missing_data.R Outdated Show resolved Hide resolved
R/tm_variable_browser.R Outdated Show resolved Hide resolved
R/tm_variable_browser.R Outdated Show resolved Hide resolved
R/utils.R Outdated Show resolved Hide resolved
R/tm_variable_browser.R Outdated Show resolved Hide resolved
Co-authored-by: Dawid Kałędkowski <[email protected]>
Signed-off-by: Lluís Revilla <[email protected]>
Copy link
Contributor

@gogonzo gogonzo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the tests for deprecated messages. Warning is thrown by the tm_ constructor. Testing it through the app driver doesn't make any sense as the message is not thrown in shiny runtime.

@llrs-roche
Copy link
Contributor Author

When I'm re-running the cases above, I'm still facing an issue with the tm_front_page, it seems to come from show_metadata = deprecated(), I see on the terminal:

[WARN] 2025-01-14 15:05:22.8165 pid:26548 token:[] teal.modules.general Error in <Anonymous>: argument is missing, with no default
  87: <Anonymous> [C:/Code/Rprojects/teal.modules.general/R/tm_front_page.R#116]
  81: ui_teal_module.teal_module
  73: FUN
  72: lapply
  68: ui_teal_module.teal_modules
  59: ui_teal
  58: ui
   1: runApp

I'm exploring why this is different from other modules

@gogonzo
Copy link
Contributor

gogonzo commented Jan 14, 2025

When I'm re-running the cases above, I'm still facing an issue with the tm_front_page, it seems to come from show_metadata = deprecated(), I see on the terminal:

[WARN] 2025-01-14 15:05:22.8165 pid:26548 token:[] teal.modules.general Error in <Anonymous>: argument is missing, with no default
  87: <Anonymous> [C:/Code/Rprojects/teal.modules.general/R/tm_front_page.R#116]
  81: ui_teal_module.teal_module
  73: FUN
  72: lapply
  68: ui_teal_module.teal_modules
  59: ui_teal
  58: ui
   1: runApp

I'm exploring why this is different from other modules

You've stepped into funny bug. lifecycle::deprecated is of language type and it is too-early-evaluated in teal. Run the example with branch from this PR.

@llrs-roche
Copy link
Contributor Author

I approved the PR mentioned. If there is anything else to improve let me know

Copy link
Contributor

@gogonzo gogonzo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍

@llrs-roche
Copy link
Contributor Author

I can reproduce the test failing locally now (before they were sporadic/ not consistent) running through all this branch's commits to tests which one broke them (git bisect run testthat_cli.R where testthat_cli.R just has devtools::load_all(".");devtools::test()) but not sure if this is a similar ghost action as in insightsengineering/teal.modules.clinical#1309.

@llrs-roche
Copy link
Contributor Author

Bisect finished:

4cab9d5cdf6f19d2fd1d33f7712337dbfc1b2f9a was both good and bad
error: bisect run failed: 'git bisect good' exited with error code -1

But results don't seem deterministic, as the commit can be both good and bad to run the tests.
This is with locally installed versions from github, next step will be to install nest packages from pharmaverse.r-universe.org (if firewall allows it).

@llrs-roche
Copy link
Contributor Author

I can now reproduce the failing checks locally (both on Rstudio R terminal and on the check window pane).

Running the tests manually, shows additional information. From this failing test, whose error is reported as:

Error (test-shinytest2-tm_misssing_data.R:154:3): e2e - tm_missing_data: Validate 'By Variable Levels' table values
Error in `UseMethod("read_xml")`: no applicable method for 'read_xml' applied to an object of class "NULL"

I run the code manually and see more details, the app app_driver_tm_missing_data() generates some warnings:

Details
load_all("../teal.modules.general")
data <- within(simple_teal_data(), {
  set.seed(123)
  
  add_nas <- function(x) {
    x[sample(seq_along(x), floor(length(x) * runif(1, .05, .17)))] <- NA
    x
  }
  
  iris[] <- lapply(iris, add_nas)
  mtcars[] <- lapply(mtcars, add_nas)
  mtcars[["cyl"]] <- as.factor(mtcars[["cyl"]])
  mtcars[["gear"]] <- as.factor(mtcars[["gear"]])
})
app <- teal::init(
  data = data,
  modules = tm_missing_data(
    label = "Missing data",
    plot_height = c(600, 400, 5000),
    plot_width = NULL,
    parent_dataname = "",
    ggtheme = "gray",
    ggplot2_args = list(
      "Combinations Hist" = teal.widgets::ggplot2_args(
        labs = list(subtitle = "Plot produced by Missing Data Module", caption = NULL)
      ),
      "Combinations Main" = teal.widgets::ggplot2_args(labs = list(title = NULL))
    ),
    pre_output = NULL,
    post_output = NULL
  )
)
## Initializing reporter_previewer_module
## Warning messages:
## 1: In rlang::hash(list(data = data, modules = modules)) :
##   'package:jsonlite' may not be available when loading
## 2: Dataset `` is missing for module 'Missing data'. Datasets available in data: `add_nas`, `iris` and `mtcars`. 

Created on 2025-01-16 with reprex v2.1.1

I fixed those and pushed to see if anything else come up.

@llrs-roche
Copy link
Contributor Author

I thought that the remaining failing tests could be due to the code generated by each branch. But running them and comparing the (html) code generated, didn't help to identify the issue.

diff tmg_source_code_main.html tmg_source_code_branch.html

It might be either some hidden component, but I'm not familiar with this part of shiny/testing/web development

@llrs-roche
Copy link
Contributor Author

@gogonzo All checks pass now. If there is no more comments I'll merge the PR this afternoon. Thanks for helping with the ordering issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ability to set datanames in module's constructor
2 participants