diff --git a/NAMESPACE b/NAMESPACE index 4cdbdf5..2b38235 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/swimmerplot.R b/R/swimmerplot.R index c4440ac..3b831af 100644 --- a/R/swimmerplot.R +++ b/R/swimmerplot.R @@ -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 @@ -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 diff --git a/man/edc_swimmerplot.Rd b/man/edc_swimmerplot.Rd index 8b403d7..1249bfb 100644 --- a/man/edc_swimmerplot.Rd +++ b/man/edc_swimmerplot.Rd @@ -54,6 +54,6 @@ p2 = edc_swimmerplot(.lookup, origin="db0$date_naissance", time_unit="weeks", 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") } } diff --git a/man/save_plotly.Rd b/man/save_plotly.Rd new file mode 100644 index 0000000..3c59be8 --- /dev/null +++ b/man/save_plotly.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/swimmerplot.R +\name{save_plotly} +\alias{save_plotly} +\title{Save a plotly to an HTML file} +\usage{ +save_plotly(p, file, ...) +} +\arguments{ +\item{p}{a plot object (\code{plotly} or \code{ggplot})} + +\item{...}{passed on to \link[htmlwidgets:saveWidget]{htmlwidgets::saveWidget}} + +\item{filename}{a file path to save the HTML file} +} +\description{ +Save a plotly to an HTML file +} +\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") +} +}