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

Guidance on how to generate figures and tables in a for loop (DRY principle) #156

Open
kelliemac opened this issue May 22, 2024 · 2 comments
Assignees

Comments

@kelliemac
Copy link
Contributor

kelliemac commented May 22, 2024

To follow the DRY (don't repeat yourself) principle, it is sometimes helpful to generate figures and tables in a for loop. For example, in the B-cell reports we are usually generating the same tables and figures repeatedly for different endpoints.

I think this challenge probably also applies in other assays. One approach is as follows:

``{r prepare-for-iterating-over-endpoints, results='asis'}

insert_fig_subchunk <- function(fig, fig_chunk_name, fig_caption_short, fig_caption_long) {
  
  fig_deparsed <- paste0(deparse(function(){fig}), collapse = '')
  fig_sub_chunk <- paste0(
    "\n```{r fig-", fig_chunk_name, ', ', 
    "fig.scap='", fig_caption_short, "', ",
    "fig.cap='", fig_caption_long, "'}",
    "\n(", fig_deparsed, ")()", "\n```\n")
  cat(knit(text = knit_expand(text = fig_sub_chunk), quiet = TRUE))
  
}

fig_caption_common_text <- 'Some description of the figure content that applies to every figure, such as the color schemes/shapes/methods used'

endpoint_list <- c('Percent of IgG B-cells that are antigen-specific', 'Percent of IgG B-cells that are VRC01-class')
``


``{r iterate-over-endpoints, results='asis'}

  for (endpoint in endpoint_list) {
    
    cat('## ', endpoint, ' \n') # subsection title
    
    # main figure
    fig_caption_short <- endpoint
    fig_caption_long <- paste0(endpoint, ". ", fig_caption_common_text)
    fig <- plot_responses(endpoint) # this function defined elsewhere
    fig_chunk_name <- gsub(' ', '-', endpoint)
    insert_fig_subchunk(fig, fig_chunk_name, fig_caption_short, fig_caption_long)
    
    cat("\n\n\\pagebreak\n")
    
    # tables  
    tabulate_response_timepoint_comparisons(endpoint) # this function defined elsewhere
    tabulate_response_group_comparisons(endpoint) # this function defined elsewhere
    
    cat("\n\n\\pagebreak\n")
    
  }
``

Is this broadly applicable enough that it would be helpful to incorporate some version of this into the report template?

If so, any improvements to suggest?

@kelliemac
Copy link
Contributor Author

@mayerbry we discussed this yesterday, would be great to have your thoughts.

@kelliemac kelliemac self-assigned this Sep 4, 2024
@kelliemac
Copy link
Contributor Author

A better example from conversation with Bryan on July 31st:

insert_fig_subchunk <- function(fig, fig_chunk_name, fig_caption_short, fig_caption_long) {
  
  fig_deparsed <- paste0(deparse(function(){fig}), collapse = '')
  fig_sub_chunk <- paste0(
    "\n```{r fig-", fig_chunk_name, ', ', 
    "fig.scap='", fig_caption_short, "', ",
    "fig.cap='", fig_caption_long, "'}",
    "\n(", fig_deparsed, ")()", "\n```\n")
  cat(knit(text = knit_expand(text = fig_sub_chunk), quiet = TRUE))
  
}

for (endpoint in endpoint_list) {
  
  cat('## ', endpoint, ' \n')
  
  ### main figure
  fig <- plot_responses(endpoint)
  fig_chunk_name <- gsub(' ', '-', endpoint)
  fig_caption_short <- endpoint
  fig_caption_long <- paste0(endpoint, ". ", plot_description_details)
  insert_fig_subchunk(fig, fig_chunk_name, fig_caption_short, fig_caption_long)

  ### table
  tab_label <- endpoint
  tab_caption_short <- endpoint
  tab_caption_long <- paste0(endpoint, ". ", tab_description_details)
  tab <- df %>%
      kable(format = output_type,
            caption = tab_caption_long,
            caption_short = tab_caption_short,
            label = tab_label)
  if (output_type == "latex") {
    cat(tab)
  } else {
    cat(knit_print(tab))
  }
  
  cat("\n\n \\clearpage \n")
  
}

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

No branches or pull requests

1 participant