From d2d3653bf6cfed03907e39746170543d85a21e26 Mon Sep 17 00:00:00 2001 From: Jordi Rosell Date: Tue, 6 Aug 2024 15:34:17 +0200 Subject: [PATCH] WIP plotly example --- NAMESPACE | 1 + R/components-amb.R | 4 + R/components-htmlwidgets.R | 39 ++++ R/dev.R | 6 +- R/rendering.R | 4 + docs/pkgdown.yml | 2 +- docs/reference/amb_htmlwidget.html | 8 + docs/reference/components.html | 20 +- docs/reference/index.html | 2 +- docs/search.json | 2 +- inst/examples/10-plotly.R | 224 +++---------------- man/components.Rd | 13 +- tests/testthat/test-components-htmlwidgets.R | 37 +++ 13 files changed, 155 insertions(+), 207 deletions(-) create mode 100644 R/components-htmlwidgets.R create mode 100644 docs/reference/amb_htmlwidget.html create mode 100644 tests/testthat/test-components-htmlwidgets.R diff --git a/NAMESPACE b/NAMESPACE index f2023b5..734f883 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,6 +2,7 @@ export(amb_button) export(amb_card) +export(amb_htmlwidget) export(amb_input_text) export(ambhtmx) export(button) diff --git a/R/components-amb.R b/R/components-amb.R index d072a6a..1103225 100644 --- a/R/components-amb.R +++ b/R/components-amb.R @@ -81,3 +81,7 @@ amb_button <- \(..., class = "rounded-1", type = "button") { ... ) } + + + + diff --git a/R/components-htmlwidgets.R b/R/components-htmlwidgets.R new file mode 100644 index 0000000..8f6b712 --- /dev/null +++ b/R/components-htmlwidgets.R @@ -0,0 +1,39 @@ +#' @keywords components +#' @rdname components +#' @param widget htmlwidget to convert as a shiny.tag +#' @param ... attributes to add to the container +#' @param width to customeize the width of the container +#' @param height to customeize the width of the container +#' @export +amb_htmlwidget <- \(widget, ..., width = "100%", height = "400px") { + temp_widget_file <- NULL + widget_chr <- withr::with_tempfile("temp_widget_file", fileext = ".html", { + htmlwidgets::saveWidget(widget, temp_widget_file, selfcontained = TRUE) + lines_read <- readLines(temp_widget_file) + full_html <- paste0(lines_read, "\n", collapse = "") + full_html |> + stringr::str_replace_all("", "") |> + stringr::str_replace_all('', "") |> + stringr::str_replace_all(".*", "") |> + stringr::str_replace_all("", "") |> + stringr::str_replace_all("", "") |> + stringr::str_replace_all("", "") |> + stringr::str_replace_all("", "") |> + stringr::str_replace_all("", "") |> + stringr::str_replace_all("", "") + }) + widget_html <- htmltools::HTML(widget_chr) + widget_tags <- htmltools::tags$div(widget_html, width = width, height = height, ...) + widget_tags +} + +# #'@noRd +# amb_htmlwidget_data <- \(widget, ..., width = "100%", height = "400px") { +# widget_chr <- withr::with_tempfile("temp_widget_file", fileext = ".html", { +# htmlwidgets::saveWidget(widget, temp_widget_file, selfcontained = TRUE) +# readLines(temp_widget_file) |> +# paste0("\n", collapse = "") +# }) +# start_pattern <- '