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

Primitive implementatin of text and image inclusion

parent 66a8612e
No related branches found
No related tags found
No related merge requests found
Pipeline #109530 passed
......@@ -16,7 +16,8 @@ Imports:
knitr,
flexdashboard,
yaml,
assertive.sets
assertive.sets,
stringr
Suggests:
highcharter,
plotly,
......
#' Method to add a component to a page of an i2dashboard
#' Method to add a component to a page of an i2dashboard.
#'
#' Components can be created by evaluating a function, or by including a text or image file.
#' The function tries to guess the intended usage by applying regular expressions to the argument \code{component} (see below for details).
#'
#' @section Adding a component by evaluating a function:
#' If the argument \code{component} is a valid function name, the function will be called and its return value is used as component content.
#'
#' @section Adding plain text as a component:
#' If the argument \code{component} ends with \code{.md} or \code{.txt}, the function will try to open a file and use its content as the components content.
#'
#' @section Adding images as a component:
#' If the argument \code{component} end matches \code{\\.[png|jpg|jpeg|gif]}, the function will try to include an image as the components content.
#'
#' @param dashboard A \linkS4class{i2dash::i2dashboard}.
#' @param page The name of the page to add the component to.
#' @param component The name of the component.
#' @param component The name of a function of a path to a file.
#' @param copy Whether or not to copy images to \code{dashboard@datadir}.
#' @param ... Additional parameters passed to the components render function.
#'
#' @rdname i2dashboard-methods
#' @rdname add-component
#' @export
setMethod("add_component",
signature = signature(dashboard = "i2dashboard", component = "character"),
function(dashboard, component, page = "default", ...) {
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)
}
function(dashboard, component, page = "default", 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"
}
if(length(dashboard@pages[[name]]$components) + 1 > dashboard@pages[[name]]$max_components) {
warning(sprintf("Not enough space left on page '%s'", name))
return(dashboard)
}
# Create page
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)
}
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")
}
if(length(dashboard@pages[[name]]$components) + 1 > dashboard@pages[[name]]$max_components) {
warning(sprintf("Not enough space left on page '%s'", name))
return(dashboard)
}
component <- do.call(eval_function, args = list(dashboard, ...))
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")
}
}
if(is.list(component)) {
assertive.sets::is_subset(c("appendix", "component", "sidebar"), names(component))
dashboard@pages[[name]]$components <- append(dashboard@pages[[name]]$components, component$component)
dashboard@pages[[name]]$sidebar <- paste0(dashboard@pages[[name]]$sidebar, component$sidebar)
# ToDo: Handle appendix
} else {
dashboard@pages[[name]]$components <- append(dashboard@pages[[name]]$components, component)
}
return(dashboard)
})
component <- switch(mode,
"function" = do.call(eval_function, args = list(dashboard, ...)),
"text" = do.call("render_text", args = list(component, ...)),
"image" = do.call("render_image", args = list(component, ...)))
if(is.list(component)) {
assertive.sets::is_subset(c("appendix", "component", "sidebar"), names(component))
dashboard@pages[[name]]$components <- append(dashboard@pages[[name]]$components, component$component)
dashboard@pages[[name]]$sidebar <- paste0(dashboard@pages[[name]]$sidebar, component$sidebar)
# ToDo: Handle appendix
} else {
dashboard@pages[[name]]$components <- append(dashboard@pages[[name]]$components, component)
}
return(dashboard)
})
#' Method to download embed files into an Rmd-file
#'
......@@ -47,8 +80,40 @@ setMethod("add_component",
#' @param ... Additional parameters.
#'
#' @export
embed_var = function(x, ...) {
embed_var <- function(x, ...) {
f = tempfile(fileext = '.csv')
write.csv(x, f)
xfun::embed_file(f, text = 'Download full data as .csv', ...)
}
#' Method to embed content from a text file in a component
#'
#' @param file The file containing the text content.
#' @param title The components title.
#'
#' @return A character string containing the evaluated component
render_text <- function(file, title = NULL) {
content <- readLines(con = file)
knitr::knit_expand(file = system.file("templates", "text_component.Rmd", package = "i2dash"),
delim = c("<%", "%>"),
content = content,
title = title)
}
#' Method to embed an image file in a component
#'
#' @param image The path to the image file.
#' @param image_alt_text The alt text of the image.
#' @param title The components title.
#'
#' @return A character string containing the evaluated component
render_image <- function(image, image_alt_text = NULL, title = NULL) {
if(is.null(image_alt_text)) {
image_alt_text <- image
}
knitr::knit_expand(file = system.file("templates", "image_component.Rmd", package = "i2dash"),
delim = c("<%", "%>"),
image = image,
image_alt_text = image_alt_text,
title = title)
}
### <% title %>
![<% image_alt_text %>](<% image %>)
### <% title %>
<% content %>
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/components.R
\docType{methods}
\name{add_component,i2dashboard,character-method}
\alias{add_component,i2dashboard,character-method}
\title{Method to add a component to a page of an i2dashboard.}
\usage{
\S4method{add_component}{i2dashboard,character}(dashboard, component,
page = "default", copy = FALSE, ...)
}
\arguments{
\item{dashboard}{A \linkS4class{i2dash::i2dashboard}.}
\item{component}{The name of a function of a path to a file.}
\item{page}{The name of the page to add the component to.}
\item{copy}{Whether or not to copy images to \code{dashboard@datadir}.}
\item{...}{Additional parameters passed to the components render function.}
}
\description{
Components can be created by evaluating a function, or by including a text or image file.
The function tries to guess the intended usage by applying regular expressions to the argument \code{component} (see below for details).
}
\section{Adding a component by evaluating a function}{
If the argument \code{component} is a valid function name, the function will be called and its return value is used as component content.
}
\section{Adding plain text as a component}{
If the argument \code{component} ends with \code{.md} or \code{.txt}, the function will try to open a file and use its content as the components content.
}
\section{Adding images as a component}{
If the argument \code{component} end matches \code{\\.[png|jpg|jpeg|gif]}, the function will try to include an image as the components content.
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/assemble.R, R/components.R, R/pages.R
% Please edit documentation in R/assemble.R, R/pages.R
\docType{methods}
\name{assemble,i2dashboard-method}
\alias{assemble,i2dashboard-method}
\alias{add_component,i2dashboard,character-method}
\alias{add_page,i2dashboard-method}
\alias{remove_page,i2dashboard-method}
\title{Method to assemble a dashboard to a Rmd file.}
......@@ -12,9 +11,6 @@
pages = names(dashboard@pages), file = dashboard@file,
exclude = NULL, render = FALSE, ...)
\S4method{add_component}{i2dashboard,character}(dashboard, component,
page = "default", ...)
\S4method{add_page}{i2dashboard}(dashboard, page, title,
layout = "default", menu = NULL, sidebar = NULL, ...)
......@@ -33,9 +29,7 @@
\item{...}{Additional arguments passed on to \code{rmarkdown::render}.}
\item{component}{The name of the component.}
\item{page}{The name of the page to add the component to.}
\item{page}{The name of the page to be added.}
\item{title}{The title of the page to be added.}
......@@ -45,12 +39,6 @@
\item{dashboard}{A \linkS4class{i2dash::i2dashboard}.}
\item{...}{Additional parameters passed to the components render function.}
\item{dashboard}{A \linkS4class{i2dash::i2dashboard}.}
\item{page}{The name of the page to be added.}
\item{dashboard}{A \linkS4class{i2dash::i2dashboard}.}
\item{page}{The name of the page to be removed.}
......@@ -58,8 +46,6 @@
\description{
Method to assemble a dashboard to a Rmd file.
Method to add a component to a page of an i2dashboard
Method to add a page to an i2dashboard
Method to remove a page to an i2dashboard
......
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/components.R
\name{render_image}
\alias{render_image}
\title{Method to embed an image file in a component}
\usage{
render_image(image, image_alt_text = NULL, title = NULL)
}
\arguments{
\item{image}{The path to the image file.}
\item{image_alt_text}{The alt text of the image.}
\item{title}{The components title.}
}
\value{
A character string containing the evaluated component
}
\description{
Method to embed an image file in a component
}
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/components.R
\name{render_text}
\alias{render_text}
\title{Method to embed content from a text file in a component}
\usage{
render_text(file, title = NULL)
}
\arguments{
\item{file}{The file containing the text content.}
\item{title}{The components title.}
}
\value{
A character string containing the evaluated component
}
\description{
Method to embed content from a text file in a 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