Skip to content

Commit

Permalink
Add save_plotly() helper
Browse files Browse the repository at this point in the history
fixes #15
  • Loading branch information
DanChaltiel committed Feb 21, 2024
1 parent be1acd1 commit bef94bd
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export(read_tm_all_xpt)
export(read_trialmaster)
export(reset_manual_correction)
export(save_list)
export(save_plotly)
export(split_mixed_datasets)
export(tibble)
export(unify)
Expand Down
30 changes: 29 additions & 1 deletion R/swimmerplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#' p3 = edc_swimmerplot(.lookup, group="db0$group", aes_color="label")
#' \dontrun{
#' #save the plotly plot as HTML to share it
#' htmlwidgets::saveWidget(p, "edc_swimmerplot.html", selfcontained=TRUE)
#' save_plotly(p, "edc_swimmerplot.html")
#' }
#' @importFrom cli cli_abort cli_warn
#' @importFrom dplyr between left_join mutate rename select slice
Expand Down Expand Up @@ -174,6 +174,34 @@ edc_swimmerplot = function(.lookup=getOption("edc_lookup"), ...,
}


# Helper ------------------------------------------------------------------

#' Save a plotly to an HTML file
#'
#' @param p a plot object (`plotly` or `ggplot`)
#' @param filename a file path to save the HTML file
#' @param ... passed on to [htmlwidgets::saveWidget]
#'
#' @export
#'
#' @examples
#' \dontrun{
#' tm = edc_example_plot()
#' p = edc_swimmerplot(tm$.lookup, id_lim=c(5,45))
#' save_plotly(p, "graph/swimplots/edc_swimmerplot.html", title="My Swimmerplot")
#' }
save_plotly = function(p, file, ...){
check_installed("plotly", reason="for `save_plotly()` to work.")
check_installed("htmlwidgets", reason="for `save_plotly()` to work.")
if(inherits(p, "ggplot")) p = plotly::ggplotly(p)
dir.create(dirname(file), showWarnings=FALSE, recursive=TRUE)
wd = setwd(dirname(file))
on.exit(setwd(wd))
htmlwidgets::saveWidget(p, file=basename(file), ...)
}


# Utils -------------------------------------------------------------------

#' @importFrom cli cli_abort
#' @importFrom dplyr select
Expand Down
2 changes: 1 addition & 1 deletion man/edc_swimmerplot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions man/save_plotly.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bef94bd

Please sign in to comment.