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

🗃️ decorators feature branch #795

Open
wants to merge 47 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
661585d
WIP
gogonzo Oct 1, 2024
de6f56c
reorg regression for decoration
gogonzo Oct 1, 2024
f014f3e
feat: refactored outliers to allow for decorators (#788)
kpagacz Oct 4, 2024
6bb26f5
regression with validate
gogonzo Oct 4, 2024
d045e7b
elo
gogonzo Oct 4, 2024
95e0fbf
fixes
gogonzo Oct 4, 2024
bf87833
conditionalPanel for regression
gogonzo Oct 7, 2024
7c4996c
fix after teal
gogonzo Oct 9, 2024
8e8457d
fix error
gogonzo Oct 15, 2024
40f053a
Merge branch 'main' into 1187_decorate_output@main
gogonzo Nov 12, 2024
b6d9bff
update tm_a_regression and tm_outliers after changes in teal for the …
m7pr Nov 18, 2024
5196b7f
Update R/tm_outliers.R
m7pr Nov 18, 2024
f28d40d
unify decorators usage with outliers
m7pr Nov 18, 2024
bf0af48
Apply suggestions from code review
m7pr Nov 18, 2024
d52361b
add req statements to get_code
m7pr Nov 19, 2024
a3d3de8
remove examples and link to the vignette
m7pr Nov 19, 2024
7aed6cd
[skip style] [skip vbump] Restyle files
github-actions[bot] Nov 19, 2024
b26a29d
[skip roxygen] [skip vbump] Roxygen Man Pages Auto Update
dependabot-preview[bot] Nov 19, 2024
29eae48
Update R/tm_outliers.R
m7pr Nov 20, 2024
e598755
[skip roxygen] [skip vbump] Roxygen Man Pages Auto Update
dependabot-preview[bot] Nov 20, 2024
c491777
updates for req and documentation
m7pr Nov 20, 2024
09bb242
Merge branch '1187_decorate_output@main' of https://github.com/insigh…
m7pr Nov 20, 2024
1c10233
[skip roxygen] [skip vbump] Roxygen Man Pages Auto Update
dependabot-preview[bot] Nov 20, 2024
97e60ef
move tm_outliers changes to different PR
m7pr Nov 20, 2024
3d48ec6
[skip roxygen] [skip vbump] Roxygen Man Pages Auto Update
dependabot-preview[bot] Nov 20, 2024
4c4d214
add req statement so that when srv_teal_transform_data returns NULL y…
m7pr Nov 21, 2024
ce1ba17
[skip style] [skip vbump] Restyle files
github-actions[bot] Nov 21, 2024
6dcb2ef
[skip roxygen] [skip vbump] Roxygen Man Pages Auto Update
dependabot-preview[bot] Nov 21, 2024
cf4371f
introduce decorators for `tm_g_bivariate` (#797)
m7pr Nov 21, 2024
612bb06
introduce decorators for `tm_g_response` (#802)
averissimo Nov 21, 2024
006b374
introduce decorators for `tm_g_distribution` (#801)
averissimo Nov 21, 2024
56be63f
[skip style] [skip vbump] Restyle files
github-actions[bot] Nov 21, 2024
7deda6d
[skip roxygen] [skip vbump] Roxygen Man Pages Auto Update
dependabot-preview[bot] Nov 21, 2024
824efcf
introduce decorators for `tm_t_crosstable` (#806)
m7pr Nov 22, 2024
0fcddf0
introduce decorators for `tm_a_pca` (#798)
m7pr Nov 22, 2024
f915e29
[skip roxygen] [skip vbump] Roxygen Man Pages Auto Update
dependabot-preview[bot] Nov 22, 2024
ba642f0
chore: rename ui/srv_teal_transform_data to ui/srv_transform_teal_data
averissimo Nov 22, 2024
11d9ea8
[skip roxygen] [skip vbump] Roxygen Man Pages Auto Update
dependabot-preview[bot] Nov 22, 2024
5f2d532
fix: move print statement after decoration
averissimo Nov 22, 2024
89f1d44
clean up print(plot) in `tm_g_distribution` module (#810)
m7pr Nov 22, 2024
9375353
[skip roxygen] [skip vbump] Roxygen Man Pages Auto Update
dependabot-preview[bot] Nov 22, 2024
09968a8
introduce decorators for `tm_g_scatterplotmatrix` (#808)
m7pr Nov 22, 2024
0870002
introduce decorators for `tm_g_scatterplot` (#807)
m7pr Nov 22, 2024
b6ca759
introduce decorators for `tm_g_association` (#800)
m7pr Nov 22, 2024
fb28571
introduce decorators for `tm_data_table` (#799)
m7pr Nov 22, 2024
9ef1032
[skip roxygen] [skip vbump] Roxygen Man Pages Auto Update
dependabot-preview[bot] Nov 22, 2024
9ae70c4
simplify usage for regression
m7pr Nov 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 72 additions & 41 deletions R/tm_a_regression.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,50 @@
#'
#' @inherit shared_params return
#'
#' @section Decorating module's output:
#' Outputs produced by the module can be modified for any application. To do so, one needs to provide
#' [teal_transform_module()] with `server` modifying outputed object in `data` (`teal_data`). Each
#' module might have a different output object name and it's type, so there is no standard code to achieve this.
#' However, `teal` provides couple wrappers which can simplify the process of decorating module's outputs:
#' - `decorator` as a `language`: provide a simple expression to modify object of interests. For example
#' `g <- g + ggtitle("Custom Title")`. Provided expression must be a working expression within module internals.
#' - `decorator` as a `function`: provide a function which will take the output object and modify it in desired way.
#' When function is provided object names don't need to match module's internal naming. For example
#' `function(x) x <- x + ggtitle("Custom Title")`.
#' @section Decorating Module Outputs:
#'
#' Decorating module outputs involves modifying the tables and plots generated by a module. This module provides the
#' ability to execute custom R code to adjust the visual or structural properties of objects displayed within the
#' application.
#'
#' The code specified in [`teal_transform_module`] is executed prior to rendering the outputs in the application. This
#' allows developers to modify attributes such as titles, labels, sizes, limits, and other features of rendered tables
#' and plots. However, decorators should be applied with careful consideration of the module's internal object names
#' to ensure compatibility.
m7pr marked this conversation as resolved.
Show resolved Hide resolved
#'
#' To customize an output, developers need to identify the name of the table or plot to be modified within the
#' `teal_transform_module`. This requires specifying a `server` function that modifies the targeted object in the
#' `data` object (of class `teal_data`). Since each module may use different internal object names or types for its
#' outputs, there is no universal code to achieve this. However, `teal` provides convenient wrappers to simplify the
#' process of decorating module outputs:
#'
#' - **Decorator as a Language/Expression**: Specify a simple R expression to modify the object of interest.
#' For example,`plot <- plot + ggtitle("Custom Title")`. The expression must be valid and compatible with the module's
#' internal environment.
#' - **Decorator as a Function**: Provide a function that accepts the output object and modifies it as desired.
#' When using this approach, the function does not need to align with the module’s internal naming conventions for its
#' objects.
m7pr marked this conversation as resolved.
Show resolved Hide resolved
#'
#' For additional details and examples of decorators, refer to the vignette
#' `vignette("decorate-modules-output", package = "teal")` or the [`teal_transform_module()`] documentation.
#'
#' @section Decorating `tm_a_regression`:
#'
#' This module creates below objects that can be modified with decorators:
#' - `plot` (`ggplot2`)#'
#'
#' @examplesShinylive
#' library(teal.modules.general)
#' interactive <- function() TRUE
#' {{ next_example }}
#' @examples
#'
#' footnote_regression <- teal_transform_module(
#' server = make_teal_transform_server(expression(
#' plot <- plot + labs(caption = deparse(summary(fit)[[1]]))
#' ))
#' )
#'
#' # general data example
#' data <- teal_data()
#' data <- within(data, {
Expand Down Expand Up @@ -90,7 +118,8 @@
#' multiple = TRUE,
#' fixed = FALSE
#' )
#' )
#' ),
#' decorators = list(footnote_regression)
#' )
#' )
#' )
Expand Down Expand Up @@ -135,7 +164,8 @@
#' multiple = TRUE,
#' fixed = FALSE
#' )
#' )
#' ),
#' decorators = list(footnote_regression)
#' )
#' )
#' )
Expand Down Expand Up @@ -295,37 +325,37 @@ ui_a_regression <- function(id, decorators, ...) {
conditionalPanel(
condition = "input.plot_type == 'Response vs Regressor'",
ns = ns,
ui_transform_data(ns("d_0"), transforms = decorators[[1]])
ui_teal_transform_data(ns("d_0"), transformators = decorators[[1]])
),
conditionalPanel(
condition = "input.plot_type == 'Residuals vs Fitted'",
ns = ns,
ui_transform_data(ns("d_1"), transforms = decorators[[1]])
ui_teal_transform_data(ns("d_1"), transformators = decorators[[1]])
),
conditionalPanel(
condition = "input.plot_type == 'Normal Q-Q'",
ns = ns,
ui_transform_data(ns("d_2"), transforms = decorators[[1]])
ui_teal_transform_data(ns("d_2"), transformators = decorators[[1]])
),
conditionalPanel(
condition = "input.plot_type == 'Scale-Location'",
ns = ns,
ui_transform_data(ns("d_3"), transforms = decorators[[1]])
ui_teal_transform_data(ns("d_3"), transformators = decorators[[1]])
),
conditionalPanel(
condition = "input.plot_type == 'Cook\\'s distance'",
ns = ns,
ui_transform_data(ns("d_4"), transforms = decorators[[1]])
ui_teal_transform_data(ns("d_4"), transformators = decorators[[1]])
),
conditionalPanel(
condition = "input.plot_type == 'Residuals vs Leverage'",
ns = ns,
ui_transform_data(ns("d_5"), transforms = decorators[[1]])
ui_teal_transform_data(ns("d_5"), transformators = decorators[[1]])
),
conditionalPanel(
condition = "input.plot_type == 'Cook\\'s dist vs Leverage'",
ns = ns,
ui_transform_data(ns("d_6"), transforms = decorators[[1]])
ui_teal_transform_data(ns("d_6"), transformators = decorators[[1]])
),
),
checkboxInput(ns("show_outlier"), label = "Display outlier labels", value = TRUE),
Expand Down Expand Up @@ -610,6 +640,7 @@ srv_a_regression <- function(id,
})

output_plot_0 <- reactive({

fit <- fit_r()[["fit"]]
ANL <- anl_merged_q()[["ANL"]]

Expand Down Expand Up @@ -677,10 +708,10 @@ srv_a_regression <- function(id,
expr = {
class(fit$residuals) <- NULL
data <- fortify(fit)
g <- plot
plot <- graph
},
env = list(
plot = Reduce(function(x, y) call("+", x, y), c(plot, parsed_ggplot2_args))
graph = Reduce(function(x, y) call("+", x, y), c(plot, parsed_ggplot2_args))
)
)
)
Expand Down Expand Up @@ -721,10 +752,10 @@ srv_a_regression <- function(id,
substitute(
expr = {
smoothy <- smooth(data$.fitted, data$.resid)
g <- plot
plot <- graph
},
env = list(
plot = Reduce(function(x, y) call("+", x, y), c(plot, parsed_ggplot2_args))
graph = Reduce(function(x, y) call("+", x, y), c(plot, parsed_ggplot2_args))
)
)
)
Expand Down Expand Up @@ -780,10 +811,10 @@ srv_a_regression <- function(id,
plot_base,
substitute(
expr = {
g <- plot
plot <- graph
},
env = list(
plot = Reduce(function(x, y) call("+", x, y), c(plot, parsed_ggplot2_args))
graph = Reduce(function(x, y) call("+", x, y), c(plot, parsed_ggplot2_args))
)
)
)
Expand Down Expand Up @@ -823,10 +854,10 @@ srv_a_regression <- function(id,
substitute(
expr = {
smoothy <- smooth(data$.fitted, sqrt(abs(data$.stdresid)))
g <- plot
plot <- graph
},
env = list(
plot = Reduce(function(x, y) call("+", x, y), c(plot, parsed_ggplot2_args))
graph = Reduce(function(x, y) call("+", x, y), c(plot, parsed_ggplot2_args))
)
)
)
Expand Down Expand Up @@ -889,10 +920,10 @@ srv_a_regression <- function(id,
plot_base,
substitute(
expr = {
g <- plot
plot <- graph
},
env = list(
plot = Reduce(function(x, y) call("+", x, y), c(plot, parsed_ggplot2_args))
graph = Reduce(function(x, y) call("+", x, y), c(plot, parsed_ggplot2_args))
)
)
)
Expand Down Expand Up @@ -944,10 +975,10 @@ srv_a_regression <- function(id,
substitute(
expr = {
smoothy <- smooth(data$.hat, data$.stdresid)
g <- plot
plot <- graph
},
env = list(
plot = Reduce(function(x, y) call("+", x, y), c(plot, parsed_ggplot2_args))
graph = Reduce(function(x, y) call("+", x, y), c(plot, parsed_ggplot2_args))
)
)
)
Expand Down Expand Up @@ -994,22 +1025,22 @@ srv_a_regression <- function(id,
substitute(
expr = {
smoothy <- smooth(data$.hat, data$.cooksd)
g <- plot
plot <- graph
},
env = list(
plot = Reduce(function(x, y) call("+", x, y), c(plot, parsed_ggplot2_args))
graph = Reduce(function(x, y) call("+", x, y), c(plot, parsed_ggplot2_args))
)
)
)
})

decorated_output_0 <- srv_transform_data(id = "d_0", data = output_plot_0, transforms = decorators[[1]])
decorated_output_1 <- srv_transform_data(id = "d_1", data = output_plot_1, transforms = decorators[[1]])
decorated_output_2 <- srv_transform_data(id = "d_2", data = output_plot_2, transforms = decorators[[1]])
decorated_output_3 <- srv_transform_data(id = "d_3", data = output_plot_3, transforms = decorators[[1]])
decorated_output_4 <- srv_transform_data(id = "d_4", data = output_plot_4, transforms = decorators[[1]])
decorated_output_5 <- srv_transform_data(id = "d_5", data = output_plot_5, transforms = decorators[[1]])
decorated_output_6 <- srv_transform_data(id = "d_6", data = output_plot_6, transforms = decorators[[1]])
decorated_output_0 <- srv_teal_transform_data(id = "d_0", data = output_plot_0, transformators = decorators[[1]])
averissimo marked this conversation as resolved.
Show resolved Hide resolved
decorated_output_1 <- srv_teal_transform_data(id = "d_1", data = output_plot_1, transformators = decorators[[1]])
decorated_output_2 <- srv_teal_transform_data(id = "d_2", data = output_plot_2, transformators = decorators[[1]])
decorated_output_3 <- srv_teal_transform_data(id = "d_3", data = output_plot_3, transformators = decorators[[1]])
decorated_output_4 <- srv_teal_transform_data(id = "d_4", data = output_plot_4, transformators = decorators[[1]])
decorated_output_5 <- srv_teal_transform_data(id = "d_5", data = output_plot_5, transformators = decorators[[1]])
decorated_output_6 <- srv_teal_transform_data(id = "d_6", data = output_plot_6, transformators = decorators[[1]])


output_q <- reactive({
Expand All @@ -1026,7 +1057,7 @@ srv_a_regression <- function(id,
})

fitted <- reactive(output_q()[["fit"]])
plot_r <- reactive(output_q()[["g"]])
plot_r <- reactive(output_q()[["plot"]])

# Insert the plot into a plot_with_settings module from teal.widgets
pws <- teal.widgets::plot_with_settings_srv(
Expand Down
Loading
Loading