diff --git a/R/assemble.R b/R/assemble.R index 9320b3ebc1cb4e456dfce6da54f875e3756ea632..3e526ba868ae5d7bc09ceaeaf4e41609335bfd28 100644 --- a/R/assemble.R +++ b/R/assemble.R @@ -109,7 +109,7 @@ setMethod("assemble", "i2dashboard", function(dashboard, pages = names(dashboard #' @return A markdown string with the final page. .render_page <- function(title, components, layout = c("default", "storyboard", "focal_left", "2x2_grid"), menu = NULL, sidebar = NULL) { if(!is.null(sidebar)) { - sidebar <- knitr::knit_expand(file = system.file("templates", "local_sidebar", package = "i2dash"), + sidebar <- knitr::knit_expand(file = system.file("templates", "local_sidebar.Rmd", package = "i2dash"), delim = c("<%", "%>"), content = sidebar, datawidth = 250) } diff --git a/R/components.R b/R/components.R index 32967821a8effc5cb1123d9aefc8f89a324e5395..5b1791599fd0ec887e40b90fff085abdf1120f3e 100644 --- a/R/components.R +++ b/R/components.R @@ -23,14 +23,14 @@ #' @param page The name of the page to add the component or sidebar to. #' @param component An R object, function, or string. #' @param copy Whether or not to copy images to \code{dashboard@datadir}. -#' @param ... Additional parameters passed to the components render function. +#' @param ... Additional parameters passed to the components render function. In case of an image, parameters \code{height} and \code{width} can be used to define the dimensions of the image with CSS or provide an alternative text with \code{image_alt_text}. #' #' @return The (modified) \linkS4class{i2dashboard} object. #' #' @rdname i2dashboard-content setMethod("add_component", signature = signature(dashboard = "i2dashboard", component = "character"), - function(dashboard, page = "default", component, copy = FALSE, ...) { + function(dashboard, component, page = "default", copy = FALSE, ...) { # Logic to guess intended usage mode <- NULL if(stringr::str_detect(tolower(component), "\\.[md|txt]+$")) { @@ -66,7 +66,7 @@ setMethod("add_component", #' @rdname i2dashboard-content setMethod("add_component", signature(dashboard = "i2dashboard", component = "function"), - function(dashboard, page = "default", component, ...) { + function(dashboard, component, page = "default", ...) { # validate "page" input name <- .create_page_name(page) if (!(name %in% names(dashboard@pages))) { @@ -194,13 +194,21 @@ render_text <- function(file, title = NULL, raw = FALSE) { #' @param image_alt_text The alt text of the image. #' @param title The components title. #' @param raw Whether or not to emit solely the markdown image code. +#' @param width Width defined with CSS in the HTML img-tag. +#' @param height Height defined with CSS in the HTML img-tag. #' #' @return A character string containing the evaluated component -render_image <- function(image, image_alt_text = NULL, title = NULL, raw = FALSE) { +render_image <- function(image, image_alt_text = NULL, title = NULL, raw = FALSE, width = "100%", height = "auto") { if(is.null(image_alt_text)) { image_alt_text <- image } - content <- glue::glue("\n", image_alt_text = image_alt_text, image = image) + + content <- glue::glue(as.character( + htmltools::img( + src = image, + alt = image_alt_text, + style = paste0('height:', height, ';width:', width) + )),as.character(htmltools::br())) if(raw) return(content) knitr::knit_expand(file = system.file("templates", "component.Rmd", package = "i2dash"), diff --git a/R/sidebar.R b/R/sidebar.R index a9a8933267400a974244f99fa440df23d50538f4..1bb1b9754eff5c8c701c5356b2ec6f8735886a1d 100644 --- a/R/sidebar.R +++ b/R/sidebar.R @@ -1,51 +1,62 @@ #' @include components.R #' @param global Whether or not to add the content to the global sidebar. -#' #' @rdname i2dashboard-content -setMethod("add_to_sidebar", "i2dashboard", function(dashboard, component, page = "default", global = FALSE, copy = FALSE, ...) { - # Logic to guess intended usage - mode <- "function" - if(stringr::str_detect(tolower(component), "\\.[md|txt]+$")) { - mode <- "text" - } - if(stringr::str_detect(tolower(component), "\\.[png|jpg|jpeg|gif]+$")) { - if(copy) { - location <- file.path(dashboard@datadir, basename(component)) - file.copy(component, location) - component <- location - } - mode <- "image" - } +setMethod("add_to_sidebar", + signature = signature(dashboard = "i2dashboard", component = "character"), + function(dashboard, component, page = "default", global = FALSE, copy = FALSE, ...) { + # Logic to guess intended usage + mode <- NULL + if(stringr::str_detect(tolower(component), "\\.[md|txt]+$")) { + mode <- "text" + } + if(stringr::str_detect(tolower(component), "\\.[png|jpg|jpeg|gif]+$")) { + if(copy) { + location <- file.path(dashboard@datadir, basename(component)) + file.copy(component, location) + component <- location + } + mode <- "image" + } - if(mode == "function") { - pn <- strsplit(component, "::")[[1]] - eval_function <- if(length(pn) == 1) { - get(pn[[1]], envir = asNamespace("i2dash"), mode = "function") - } else { - get(pn[[2]], envir = asNamespace(pn[[1]]), mode = "function") - } - } + component <- switch(mode, + "text" = render_text(component, ...), + "image" = render_image(component, ...)) - component <- switch(mode, - "function" = do.call(eval_function, args = list(dashboard, ...)), - "text" = do.call("render_text", args = list(component, raw = TRUE)), - "image" = do.call("render_image", args = list(component, raw = TRUE))) + if(global) { + dashboard@sidebar <- paste0(dashboard@sidebar, component) + } else { + # validate "page" input + name <- .create_page_name(page) + if (!(name %in% names(dashboard@pages))) { + warning(sprintf("i2dashboard dashboard does not contain a page named '%s'", name)) + return(dashboard) + } + dashboard@pages[[name]]$sidebar <- paste0(dashboard@pages[[name]]$sidebar, component) + } + return(dashboard) + }) +#' @rdname i2dashboard-content +setMethod("add_to_sidebar", + signature = signature(dashboard = "i2dashboard", component = "function"), + function(dashboard, component, page = "default", global = FALSE, copy = FALSE, ...) { + content <- component(dashboard, ...) - if(is.list(component)) { - warning(sprintf("Component function returned unsupported content for sidebar.")) - return(dashboard) - } + if(is.list(content)) { + warning(sprintf("Component function returned unsupported content for a sidebar.")) + return(dashboard) + } - if(global) { - dashboard@sidebar <- paste0(dashboard@sidebar, component) - } else { - name <- .create_page_name(page) - if (!(name %in% names(dashboard@pages))) { - warning(sprintf("i2dashboard dashboard does not contain a page named '%s'", name)) - return(dashboard) - } - dashboard@pages[[name]]$sidebar <- paste0(dashboard@pages[[name]]$sidebar, component) - } - return(dashboard) -}) + if(global) { + dashboard@sidebar <- paste0(dashboard@sidebar, content) + } else { + # validate "page" input + name <- .create_page_name(page) + if (!(name %in% names(dashboard@pages))) { + warning(sprintf("i2dashboard dashboard does not contain a page named '%s'", name)) + return(dashboard) + } + dashboard@pages[[name]]$sidebar <- paste0(dashboard@pages[[name]]$sidebar, content) + } + return(dashboard) + }) diff --git a/man/i2dashboard-content.Rd b/man/i2dashboard-content.Rd index 02543abefdb48e9f0507fa216402dbb90ecae000..20c113da029e95ac87c464d109e0478cdd1bfc60 100644 --- a/man/i2dashboard-content.Rd +++ b/man/i2dashboard-content.Rd @@ -15,7 +15,8 @@ \alias{add_component,i2dashboard,ANY-method} \alias{add_link,i2dashboard-method} \alias{add_colormap,i2dashboard-method} -\alias{add_to_sidebar,i2dashboard-method} +\alias{add_to_sidebar,i2dashboard,character-method} +\alias{add_to_sidebar,i2dashboard,function-method} \title{Add content to an i2dashboard object.} \usage{ add_component(dashboard, component, ...) @@ -26,9 +27,9 @@ add_colormap(dashboard, ...) add_link(dashboard, ...) -\S4method{add_component}{i2dashboard,character}(dashboard, page = "default", component, copy = FALSE, ...) +\S4method{add_component}{i2dashboard,character}(dashboard, component, page = "default", copy = FALSE, ...) -\S4method{add_component}{i2dashboard,`function`}(dashboard, page = "default", component, ...) +\S4method{add_component}{i2dashboard,`function`}(dashboard, component, page = "default", ...) \S4method{add_component}{i2dashboard,gg}(dashboard, component, page = "default", ...) @@ -51,7 +52,16 @@ add_link(dashboard, ...) \S4method{add_colormap}{i2dashboard}(dashboard, map, name) -\S4method{add_to_sidebar}{i2dashboard}( +\S4method{add_to_sidebar}{i2dashboard,character}( + dashboard, + component, + page = "default", + global = FALSE, + copy = FALSE, + ... +) + +\S4method{add_to_sidebar}{i2dashboard,`function`}( dashboard, component, page = "default", @@ -65,7 +75,7 @@ add_link(dashboard, ...) \item{component}{An R object, function, or string.} -\item{...}{Additional parameters passed to the components render function.} +\item{...}{Additional parameters passed to the components render function. In case of an image, parameters \code{height} and \code{width} can be used to define the dimensions of the image with CSS or provide an alternative text with \code{image_alt_text}.} \item{page}{The name of the page to add the component or sidebar to.} diff --git a/man/render_image.Rd b/man/render_image.Rd index 3eee0774b9b009dc556b54d7dbbee3e8cbbce3df..5684a86e062250b27034fdc84bc15aaeb14caac6 100644 --- a/man/render_image.Rd +++ b/man/render_image.Rd @@ -4,7 +4,14 @@ \alias{render_image} \title{Method to embed an image file in a component} \usage{ -render_image(image, image_alt_text = NULL, title = NULL, raw = FALSE) +render_image( + image, + image_alt_text = NULL, + title = NULL, + raw = FALSE, + width = "100\%", + height = "auto" +) } \arguments{ \item{image}{The path to the image file.} @@ -14,6 +21,10 @@ render_image(image, image_alt_text = NULL, title = NULL, raw = FALSE) \item{title}{The components title.} \item{raw}{Whether or not to emit solely the markdown image code.} + +\item{width}{Width defined with CSS in the HTML img-tag.} + +\item{height}{Height defined with CSS in the HTML img-tag.} } \value{ A character string containing the evaluated component