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

model_dashboard: export code used to produce all the outputs #284

Open
etiennebacher opened this issue Aug 31, 2022 · 4 comments
Open

model_dashboard: export code used to produce all the outputs #284

etiennebacher opened this issue Aug 31, 2022 · 4 comments
Labels
Enhancement New feature or request

Comments

@etiennebacher
Copy link
Member

It would be nice to have an additional tab containing code that can simply be copy-pasted in R and produces the same outputs

@etiennebacher etiennebacher added the Enhancement New feature or request label Aug 31, 2022
@etiennebacher
Copy link
Member Author

My bad, I missed the "source code" button

@etiennebacher
Copy link
Member Author

Actually the "source code" button shows the source for the Rmd file, but I was thinking of something like:

# Assumption checks -------------
*Some code here*

# Indices of model fit -------------
*Some code here*

# Tabular summary -------------
*Some code here*

[...]

@etiennebacher etiennebacher reopened this Aug 31, 2022
@IndrajeetPatil
Copy link
Member

This should be simple enough. We just need to use knitr::purl():

https://bookdown.org/yihui/rmarkdown-cookbook/purl.html

@etiennebacher
Copy link
Member Author

knitr::purl() extracts the whole chunks, but most of the code there is not needed to reproduce the results (if everything works well).

For example, suppose that there are interactions in the model, then this if condition :

if (is.null(int_terms)) {
  model_terms <- con_terms
} else {
  model_terms <- clean_names(int_terms)
  int_terms <- unique(unlist(strsplit(clean_names(int_terms), ":", fixed = TRUE)))
  model_terms <- c(model_terms, setdiff(con_terms, int_terms))
}

could be reduced to:

model_terms <- clean_names(int_terms)
int_terms <- unique(unlist(strsplit(clean_names(int_terms), ":", fixed = TRUE)))
model_terms <- c(model_terms, setdiff(con_terms, int_terms))

in the exported code.

In the end, I have in mind a text output that doesn't contain any of the conditions/tryCatch that are in the Rmd file, just the code you would run if you had to write it yourself:

params <- list(model = "model", check_model_args = "check_model_args", 
    parameters_args = "parameters_args", performance_args = "performance_args")

#### Assumption checks -----------
do.call(performance::check_model, check_model_args)

#### Indices of model fit -----------
performance_args <- c(list(model), params$performance_args)
# add verbose, if not done yet
if (is.null(performance_args$verbose)) performance_args$verbose <- FALSE
table_performance <- do.call(performance::performance, performance_args)
print_md(table_performance, layout = "vertical", caption = NULL)

...

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

No branches or pull requests

2 participants