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

See merge request !9

parents 89a44a73 07e17e06
No related branches found
No related tags found
1 merge request!9fixed sidebar
Pipeline #132040 failed
...@@ -109,7 +109,7 @@ setMethod("assemble", "i2dashboard", function(dashboard, pages = names(dashboard ...@@ -109,7 +109,7 @@ setMethod("assemble", "i2dashboard", function(dashboard, pages = names(dashboard
#' @return A markdown string with the final page. #' @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) { .render_page <- function(title, components, layout = c("default", "storyboard", "focal_left", "2x2_grid"), menu = NULL, sidebar = NULL) {
if(!is.null(sidebar)) { 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("<%", "%>"), delim = c("<%", "%>"),
content = sidebar, datawidth = 250) content = sidebar, datawidth = 250)
} }
......
...@@ -23,14 +23,14 @@ ...@@ -23,14 +23,14 @@
#' @param page The name of the page to add the component or sidebar to. #' @param page The name of the page to add the component or sidebar to.
#' @param component An R object, function, or string. #' @param component An R object, function, or string.
#' @param copy Whether or not to copy images to \code{dashboard@datadir}. #' @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. #' @return The (modified) \linkS4class{i2dashboard} object.
#' #'
#' @rdname i2dashboard-content #' @rdname i2dashboard-content
setMethod("add_component", setMethod("add_component",
signature = signature(dashboard = "i2dashboard", component = "character"), 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 # Logic to guess intended usage
mode <- NULL mode <- NULL
if(stringr::str_detect(tolower(component), "\\.[md|txt]+$")) { if(stringr::str_detect(tolower(component), "\\.[md|txt]+$")) {
...@@ -66,7 +66,7 @@ setMethod("add_component", ...@@ -66,7 +66,7 @@ setMethod("add_component",
#' @rdname i2dashboard-content #' @rdname i2dashboard-content
setMethod("add_component", signature(dashboard = "i2dashboard", component = "function"), setMethod("add_component", signature(dashboard = "i2dashboard", component = "function"),
function(dashboard, page = "default", component, ...) { function(dashboard, component, page = "default", ...) {
# validate "page" input # validate "page" input
name <- .create_page_name(page) name <- .create_page_name(page)
if (!(name %in% names(dashboard@pages))) { if (!(name %in% names(dashboard@pages))) {
...@@ -194,13 +194,21 @@ render_text <- function(file, title = NULL, raw = FALSE) { ...@@ -194,13 +194,21 @@ render_text <- function(file, title = NULL, raw = FALSE) {
#' @param image_alt_text The alt text of the image. #' @param image_alt_text The alt text of the image.
#' @param title The components title. #' @param title The components title.
#' @param raw Whether or not to emit solely the markdown image code. #' @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 #' @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)) { if(is.null(image_alt_text)) {
image_alt_text <- image image_alt_text <- image
} }
content <- glue::glue("![{image_alt_text}]({image})\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) if(raw) return(content)
knitr::knit_expand(file = system.file("templates", "component.Rmd", package = "i2dash"), knitr::knit_expand(file = system.file("templates", "component.Rmd", package = "i2dash"),
......
#' @include components.R #' @include components.R
#' @param global Whether or not to add the content to the global sidebar. #' @param global Whether or not to add the content to the global sidebar.
#'
#' @rdname i2dashboard-content #' @rdname i2dashboard-content
setMethod("add_to_sidebar", "i2dashboard", function(dashboard, component, page = "default", global = FALSE, copy = FALSE, ...) { setMethod("add_to_sidebar",
# Logic to guess intended usage signature = signature(dashboard = "i2dashboard", component = "character"),
mode <- "function" function(dashboard, component, page = "default", global = FALSE, copy = FALSE, ...) {
if(stringr::str_detect(tolower(component), "\\.[md|txt]+$")) { # Logic to guess intended usage
mode <- "text" mode <- NULL
} if(stringr::str_detect(tolower(component), "\\.[md|txt]+$")) {
if(stringr::str_detect(tolower(component), "\\.[png|jpg|jpeg|gif]+$")) { mode <- "text"
if(copy) { }
location <- file.path(dashboard@datadir, basename(component)) if(stringr::str_detect(tolower(component), "\\.[png|jpg|jpeg|gif]+$")) {
file.copy(component, location) if(copy) {
component <- location location <- file.path(dashboard@datadir, basename(component))
} file.copy(component, location)
mode <- "image" component <- location
} }
mode <- "image"
}
if(mode == "function") { component <- switch(mode,
pn <- strsplit(component, "::")[[1]] "text" = render_text(component, ...),
eval_function <- if(length(pn) == 1) { "image" = render_image(component, ...))
get(pn[[1]], envir = asNamespace("i2dash"), mode = "function")
} else {
get(pn[[2]], envir = asNamespace(pn[[1]]), mode = "function")
}
}
component <- switch(mode, if(global) {
"function" = do.call(eval_function, args = list(dashboard, ...)), dashboard@sidebar <- paste0(dashboard@sidebar, component)
"text" = do.call("render_text", args = list(component, raw = TRUE)), } else {
"image" = do.call("render_image", args = list(component, raw = TRUE))) # 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)) { if(is.list(content)) {
warning(sprintf("Component function returned unsupported content for sidebar.")) warning(sprintf("Component function returned unsupported content for a sidebar."))
return(dashboard) return(dashboard)
} }
if(global) { if(global) {
dashboard@sidebar <- paste0(dashboard@sidebar, component) dashboard@sidebar <- paste0(dashboard@sidebar, content)
} else { } else {
name <- .create_page_name(page) # validate "page" input
if (!(name %in% names(dashboard@pages))) { name <- .create_page_name(page)
warning(sprintf("i2dashboard dashboard does not contain a page named '%s'", name)) if (!(name %in% names(dashboard@pages))) {
return(dashboard) warning(sprintf("i2dashboard dashboard does not contain a page named '%s'", name))
} return(dashboard)
dashboard@pages[[name]]$sidebar <- paste0(dashboard@pages[[name]]$sidebar, component) }
} dashboard@pages[[name]]$sidebar <- paste0(dashboard@pages[[name]]$sidebar, content)
return(dashboard) }
}) return(dashboard)
})
...@@ -15,7 +15,8 @@ ...@@ -15,7 +15,8 @@
\alias{add_component,i2dashboard,ANY-method} \alias{add_component,i2dashboard,ANY-method}
\alias{add_link,i2dashboard-method} \alias{add_link,i2dashboard-method}
\alias{add_colormap,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.} \title{Add content to an i2dashboard object.}
\usage{ \usage{
add_component(dashboard, component, ...) add_component(dashboard, component, ...)
...@@ -26,9 +27,9 @@ add_colormap(dashboard, ...) ...@@ -26,9 +27,9 @@ add_colormap(dashboard, ...)
add_link(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", ...) \S4method{add_component}{i2dashboard,gg}(dashboard, component, page = "default", ...)
...@@ -51,7 +52,16 @@ add_link(dashboard, ...) ...@@ -51,7 +52,16 @@ add_link(dashboard, ...)
\S4method{add_colormap}{i2dashboard}(dashboard, map, name) \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, dashboard,
component, component,
page = "default", page = "default",
...@@ -65,7 +75,7 @@ add_link(dashboard, ...) ...@@ -65,7 +75,7 @@ add_link(dashboard, ...)
\item{component}{An R object, function, or string.} \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.} \item{page}{The name of the page to add the component or sidebar to.}
......
...@@ -4,7 +4,14 @@ ...@@ -4,7 +4,14 @@
\alias{render_image} \alias{render_image}
\title{Method to embed an image file in a component} \title{Method to embed an image file in a component}
\usage{ \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{ \arguments{
\item{image}{The path to the image file.} \item{image}{The path to the image file.}
...@@ -14,6 +21,10 @@ render_image(image, image_alt_text = NULL, title = NULL, raw = FALSE) ...@@ -14,6 +21,10 @@ render_image(image, image_alt_text = NULL, title = NULL, raw = FALSE)
\item{title}{The components title.} \item{title}{The components title.}
\item{raw}{Whether or not to emit solely the markdown image code.} \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{ \value{
A character string containing the evaluated component A character string containing the evaluated component
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment