Skip to content

POC Redesign of teal.reporter #862

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

Draft
wants to merge 48 commits into
base: main
Choose a base branch
from
Draft

POC Redesign of teal.reporter #862

wants to merge 48 commits into from

Conversation

m7pr
Copy link
Contributor

@m7pr m7pr commented Feb 24, 2025

Companion to

Covered modules

  • tm_a_pca @m7pr
  • tm_a_regression @m7pr (needs cleanup as is used to test multiple artificial outputs)
  • tm_data_table
  • tm_file_viewer
  • tm_front_page
  • tm_g_association
  • tm_g_bivariate
  • tm_g_distribution
  • tm_g_response
  • tm_g_scatterplot @m7pr
  • tm_g_scatterplotmatrix
  • tm_missing_data
  • tm_outliers
  • tm_t_crosstable
  • tm_variable_browser
code
devtools::load_all('../teal.reporter')
devtools::load_all('../teal.widgets')
devtools::load_all('../teal.code')
devtools::load_all('../teal')
devtools::load_all('.')

# general data example
data <- teal_data()
data <- within(data, {
  require(nestcolor)
  CO2 <- CO2
  USArrests <- USArrests
})

app <- init(
  data = data,
  modules = modules(
    tm_a_regression(
      label = "Regression",
      response = data_extract_spec(
        dataname = "CO2",
        select = select_spec(
          label = "Select variable:",
          choices = "uptake",
          selected = "uptake",
          multiple = FALSE,
          fixed = TRUE
        )
      ),
      regressor = data_extract_spec(
        dataname = "CO2",
        select = select_spec(
          label = "Select variables:",
          choices = variable_choices(data[["CO2"]], c("conc", "Treatment")),
          selected = "conc",
          multiple = TRUE,
          fixed = FALSE
        )
      )
    ),
    tm_a_pca(
      "PCA",
      dat = data_extract_spec(
        dataname = "USArrests",
        select = select_spec(
          choices = variable_choices(
            data = data[["USArrests"]], c("Murder", "Assault", "UrbanPop", "Rape")
          ),
          selected = c("Murder", "Assault"),
          multiple = TRUE
        ),
        filter = NULL
      )
    ),
    tm_g_scatterplot(
      label = "Scatterplot Choices",
      x = data_extract_spec(
        dataname = "CO2",
        select = select_spec(
          label = "Select variable:",
          choices = variable_choices(data[["CO2"]], c("conc", "uptake")),
          selected = "conc",
          multiple = FALSE,
          fixed = FALSE
        )
      ),
      y = data_extract_spec(
        dataname = "CO2",
        select = select_spec(
          label = "Select variable:",
          choices = variable_choices(data[["CO2"]], c("conc", "uptake")),
          selected = "uptake",
          multiple = FALSE,
          fixed = FALSE
        )
      ),
      color_by = data_extract_spec(
        dataname = "CO2",
        select = select_spec(
          label = "Select variable:",
          choices = variable_choices(
            data[["CO2"]],
            c("Plant", "Type", "Treatment", "conc", "uptake")
          ),
          selected = NULL,
          multiple = FALSE,
          fixed = FALSE
        )
      ),
      size_by = data_extract_spec(
        dataname = "CO2",
        select = select_spec(
          label = "Select variable:",
          choices = variable_choices(data[["CO2"]], c("conc", "uptake")),
          selected = "uptake",
          multiple = FALSE,
          fixed = FALSE
        )
      ),
      row_facet = data_extract_spec(
        dataname = "CO2",
        select = select_spec(
          label = "Select variable:",
          choices = variable_choices(data[["CO2"]], c("Plant", "Type", "Treatment")),
          selected = NULL,
          multiple = FALSE,
          fixed = FALSE
        )
      ),
      col_facet = data_extract_spec(
        dataname = "CO2",
        select = select_spec(
          label = "Select variable:",
          choices = variable_choices(data[["CO2"]], c("Plant", "Type", "Treatment")),
          selected = NULL,
          multiple = FALSE,
          fixed = FALSE
        )
      )
    ),
    tm_g_bivariate(
      x = data_extract_spec(
        dataname = "CO2",
        select = select_spec(
          label = "Select variable:",
          choices = variable_choices(data[["CO2"]]),
          selected = "conc",
          fixed = FALSE
        )
      ),
      y = data_extract_spec(
        dataname = "CO2",
        select = select_spec(
          label = "Select variable:",
          choices = variable_choices(data[["CO2"]]),
          selected = "uptake",
          multiple = FALSE,
          fixed = FALSE
        )
      ),
      row_facet = data_extract_spec(
        dataname = "CO2",
        select = select_spec(
          label = "Select variable:",
          choices = variable_choices(data[["CO2"]]),
          selected = "Type",
          fixed = FALSE
        )
      ),
      col_facet = data_extract_spec(
        dataname = "CO2",
        select = select_spec(
          label = "Select variable:",
          choices = variable_choices(data[["CO2"]]),
          selected = "Treatment",
          fixed = FALSE
        )
      )
    )
  )
)
if (interactive()) {
  shinyApp(app$ui, app$server, enableBookmarking = "server")
}

@m7pr m7pr marked this pull request as draft February 24, 2025 13:49
@m7pr m7pr added the core label Feb 24, 2025
@m7pr m7pr changed the title [WIP] teal.reporter redesign poc POC Redesign of teal.reporter Feb 26, 2025
Merge branch 'test@bslib@main' into report_redesign_poc@main

# Conflicts:
#	R/tm_a_regression.R
@m7pr m7pr changed the base branch from main to test@bslib@main March 6, 2025 11:01
@m7pr m7pr changed the base branch from test@bslib@main to main March 6, 2025 11:02
@m7pr m7pr changed the base branch from main to test@bslib@main March 6, 2025 11:43
Base automatically changed from test@bslib@main to main March 11, 2025 08:12
@donyunardi
Copy link
Contributor

donyunardi commented Apr 18, 2025

Viewing the structure of the report document for a module

Currently, users have to look at the source code or inspect the server element to understand the structure of card_fun in a module, in order to see how the report document is structured for a teal module.

Example:
r$> x <- my_custom_module()

r$> body(x$server)
{
    moduleServer(id, function(input, output, session) {
        shiny::updateSelectInput(inputId = "datasets", choices = datanames(data()))
        observeEvent(input$datasets, {
            req(input$datasets)
            only_numeric <- sapply(data()[[input$datasets]], 
                is.numeric)
            shiny::updateSelectInput(inputId = "variables", choices = names(data()[[input$datasets]])[only_numeric])
        })
        result <- reactive({
            req(input$datasets)
            req(input$variables %in% names(data()[[input$datasets]]))
            new_data <- within(data(), {
                my_plot <- ggplot(input_dataset, aes(x = input_vars)) + 
                  geom_histogram(binwidth = input_binwidth, fill = "skyblue", 
                    color = "black")
            }, input_dataset = as.name(input$datasets), input_vars = as.name(input$variables), 
                input_binwidth = input$binwidth, label = "plot")
            new_data
        })
        output$plt <- shiny::renderPlot({
            result()[["my_plot"]]
        })
        teal.widgets::verbatim_popup_srv(id = "rcode", verbatim_content = reactive(get_code(result())), 
            title = "Code to reproduce the analysis")
        plot_code_r <- pull_code(result, labels = "plot")
        card_fun <- reactive({         ###### <---- This is the part that user will have to look for
            teal.reporter::report_document("## Plot", teal.reporter::code_chunk(plot_code_r()), 
                result()[["my_plot"]])
        })
        list(report_card = card_fun)
    })
}

Is it possible to provide a better way for users to view the current structure of the reporter for a module?
Can we extend our format.teal_module method to include this information?
image


"If you don't want to include code for head(mtcars) in report - just don't include it.",

"If you want the object to be kept in the report as loaded from .rds use keep_in_report()",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"If you want the object to be kept in the report as loaded from .rds use keep_in_report()",
"If you want the object to be kept in the rendered output (pdf, pptx, etc.) use keep_in_report()",

For me high-level explaination is. If head(mtcars) |> keep_in_report(FALSE):

  • it will be included in the Reporter object (because it is in the list of items provided to report_document)
  • will be shown when previewing the results in the report_previewer (to avoid double evaluation of the code chunk)
  • will be included in rendered in pdf, pptx, ... files directly to avoid double evaluation of the code chunk
  • Will be excluded from saved .Rmd file content because .Rmd doesn't need to include a rendered output - output will be rendered when runnning .Rmd file

If keep_in_report(TRUE) - then always included

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, your assumptions are right. I wonder if that's gonna be easy to grasp for the final user 🤔

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.

4 participants