Skip to content
Snippets Groups Projects
Commit d97819ef authored by jens.preussner's avatar jens.preussner :ghost:
Browse files

Added add_component method for highchart class

parent 88c1b3a3
No related branches found
No related tags found
No related merge requests found
...@@ -18,3 +18,5 @@ Imports: ...@@ -18,3 +18,5 @@ Imports:
flexdashboard, flexdashboard,
yaml, yaml,
assertive.sets assertive.sets
Suggests:
highcharter
setGeneric("add_component", function(object, ...) standardGeneric("add_component")) setGeneric("add_component", function(object, component, ...) standardGeneric("add_component"))
#' Method to add a component to a page of an i2dashboard object #' Method to add a component to a page of an i2dashboard object
#' #'
...@@ -9,7 +9,9 @@ setGeneric("add_component", function(object, ...) standardGeneric("add_component ...@@ -9,7 +9,9 @@ setGeneric("add_component", function(object, ...) standardGeneric("add_component
#' #'
#' @rdname idashboard-class #' @rdname idashboard-class
#' @export #' @export
setMethod("add_component", "i2dashboard", function(object, page = "default", component, ...) { setMethod("add_component",
signature = signature(object = "i2dashboard", component = "character"),
function(object, component, page = "default", ...) {
name <- .create_page_name(page) name <- .create_page_name(page)
if (!(name %in% names(object@pages))) { if (!(name %in% names(object@pages))) {
warning(sprintf("i2dashboard object does not contain a page named '%s'", name)) warning(sprintf("i2dashboard object does not contain a page named '%s'", name))
......
#' Method to add highcharts as components of a page of an i2dashboard object.
#'
#' @param object The \linkS4class{i2dash::i2dashboard} object.
#' @param component The highcharts object.
#' @param page The name of the page to add the plot to.
#' @param title An optional component title.
#'
#' @export
setMethod("add_component",
signature = signature(object = "i2dashboard", component = "highchart"),
definition = function(object, component, page = "default", title = NULL, ...) {
sanitised_page <- i2dash:::.create_page_name(page)
if (!(sanitised_page %in% names(object@pages))) {
warning(sprintf("i2dashboard object does not contain a page named '%s'", sanitised_page))
return(object)
}
if(length(object@pages[[sanitised_page]]$components) + 1 > object@pages[[sanitised_page]]$max_components) {
warning(sprintf("Not enough space left on page '%s'", sanitised_page))
return(object)
}
# Create random component for RDS filename
component_id <- paste0("hc_", stringi::stri_rand_strings(1, 6))
# Save plot as RDS
saveRDS(component, file = file.path(object@workdir, "envs", paste0(component_id, ".rds")))
# Expand template
timestamp <- Sys.time()
expanded_component <- knitr::knit_expand(file = system.file("templates", "highcharts.Rmd", package = "i2dash"),
delim = c("<%", "%>"),
title = title,
component_id = component_id,
timestamp = timestamp)
# Add component to page
object@pages[[sanitised_page]]$components <- append(object@pages[[sanitised_page]]$components, expanded_component)
return(object)
})
### <% if(!is.null(title)) title %>
<!-- Component created on <% timestamp %> -->
```{r}
if (!requireNamespace("highcharter", quietly = TRUE)) {
stop("The package highcharter is needed to embed objects of class 'highchart'.", call. = FALSE)
}
readRDS("envs/<% component_id %>.rds")
```
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/highcharts.R
\docType{methods}
\name{add_component,i2dashboard,highchart-method}
\alias{add_component,i2dashboard,highchart-method}
\title{Method to add highcharts as components of a page of an i2dashboard object.}
\usage{
\S4method{add_component}{i2dashboard,highchart}(object, component,
page = "default", title = NULL, ...)
}
\arguments{
\item{object}{The \linkS4class{i2dash::i2dashboard} object.}
\item{component}{The highcharts object.}
\item{page}{The name of the page to add the plot to.}
\item{title}{An optional component title.}
}
\description{
Method to add highcharts as components of a page of an i2dashboard object.
}
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
\docType{methods} \docType{methods}
\name{assemble,i2dashboard-method} \name{assemble,i2dashboard-method}
\alias{assemble,i2dashboard-method} \alias{assemble,i2dashboard-method}
\alias{add_component,i2dashboard-method} \alias{add_component,i2dashboard,character-method}
\alias{idashboard-class} \alias{idashboard-class}
\alias{add_page,i2dashboard-method} \alias{add_page,i2dashboard-method}
\alias{remove_page,i2dashboard-method} \alias{remove_page,i2dashboard-method}
...@@ -12,8 +12,8 @@ ...@@ -12,8 +12,8 @@
\usage{ \usage{
\S4method{assemble}{i2dashboard}(object, output_file, pages, ...) \S4method{assemble}{i2dashboard}(object, output_file, pages, ...)
\S4method{add_component}{i2dashboard}(object, page = "default", \S4method{add_component}{i2dashboard,character}(object, component,
component, ...) page = "default", ...)
\S4method{add_page}{i2dashboard}(object, page, title, layout = "default", \S4method{add_page}{i2dashboard}(object, page, title, layout = "default",
menu = NULL, sidebar = NULL, ...) menu = NULL, sidebar = NULL, ...)
...@@ -29,10 +29,10 @@ ...@@ -29,10 +29,10 @@
\item{...}{Additional parameters passed to the components render function.} \item{...}{Additional parameters passed to the components render function.}
\item{page}{The name of the page to add the component to.}
\item{component}{The name of the component.} \item{component}{The name of the component.}
\item{page}{The name of the page to add the component to.}
\item{title}{The title of the page to be added.} \item{title}{The title of the page to be added.}
\item{layout}{The page layout (see below).} \item{layout}{The page layout (see below).}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment