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

Moved datadir, added header template, bumped version

parent d08022dc
No related branches found
No related tags found
No related merge requests found
Pipeline #104809 failed
Package: i2dash
Type: Package
Title: Iterative and Interactive Dashboards in R
Version: 0.1.0
Version: 0.2
Authors@R: c(
person(given = "Arsenij", family = "Ustjanzew", email = "arsenij.ustjanzew@mpi-bn.mpg.de", role = c("aut", "cre")),
person(given = "Jens", family = "Preussner", email = "jens.preussner@mpi-bn.mpg.de", role = c("aut"), comment = c(ORCID = "0000-0003-1927-3458")),
person(given = "Mario", family = "Looso", email = "mario.looso@mpi-bn.mpg.de", role = "aut"))
Description: Iteratively create interactive flexdashboards in R.
Use four spaces when indenting paragraphs within the Description.
person(given = "Mario", family = "Looso", email = "mario.looso@mpi-bn.mpg.de", role = c("aut"), comment = c(ORCID = "0000-0003-1495-9530")))
Description: Easily build dashboards from individual components in HTML and Shiny.
License: MIT
Encoding: UTF-8
LazyData: true
......
#' Method to assemble a dashboard and write it to a file
#' Method to assemble a dashboard to a Rmd file.
#'
#' @param object A \linkS4class{i2dash::i2dashboard} object.
#' @param output_file The output filename (recommend that the suffix should be '.Rmd'). This file will be saved in the working directory.
#' @param pages A string or vector with the names of pages, which should be assemble to a report.
#' @param ... Additional parameters passed to the components render function.
#' @param file The output filename (recommend that the suffix should be '.Rmd'). This file will be saved in the working directory.
#'
#' @rdname idashboard-class
#' @export
setMethod("assemble", "i2dashboard", function(object, output_file, pages, ...) {
yaml_list <- list(title = object@title,
author = object@author,
output = list("flexdashboard::flex_dashboard" = list(theme = object@theme)))
if (object@interactive){
yaml_list[["runtime"]] <- "shiny"
}
yaml_part <- yaml::as.yaml(yaml_list)
header_string <- paste0("---\n", yaml_part, "---\n<!-- This dashboard was created with the R package 'i2dash'. https://gitlab.gwdg.de/loosolab/software/i2dash -->\n")
setMethod("assemble", "i2dashboard", function(object, pages = names(object@pages), file = object@file) {
tmp_document <- tempfile()
# create variable final_document
final_document <- file.path(object@workdir, output_file)
# write header to tempfile
cat(header_string,
file = tmp_document,
append = FALSE,
sep="")
# Add YAML header
knitr::knit_expand(file = system.file("templates", "yaml_header.Rmd", package = "i2dash"),
delim = c("<%", "%>"),
title = object@title,
author = object@author,
theme = object@theme,
interactive = object@interactive) %>%
cat(file = tmp_document, append = FALSE, sep = "\n")
# Add i2dash global setup
cat(readLines(system.file("templates", "i2dash-global-setup.Rmd", package = "i2dash")),
file = tmp_document,
append = TRUE,
sep = "\n")
knitr::knit_expand(file = system.file("templates", "i2dash-global-setup.Rmd", package = "i2dash"), delim = c("<%", "%>"), datadir = object@datadir) %>%
cat(file = tmp_document, append = TRUE, sep = "\n")
# write page to tempfile
for (pagename in pages){
......@@ -41,21 +28,20 @@ setMethod("assemble", "i2dashboard", function(object, output_file, pages, ...) {
if (name %in% names(object@pages)){
# Create a content string from all components
components <- paste0(object@pages[[name]]$components, sep = "")
#components <- "here are the components"
# Create variable "title" & "menu" & "layout" for readability
title <- object@pages[[name]]$title
menu <- object@pages[[name]]$menu
layout <- object@pages[[name]]$layout
sidebar <- object@pages[[name]]$sidebar
full_content <- .render_page(title = title, components = components, layout = layout, menu = menu, sidebar = sidebar)
cat(full_content, file = tmp_document, append = TRUE, sep='')
.render_page(title = title, components = components, layout = layout, menu = menu, sidebar = sidebar) %>%
cat(file = tmp_document, append = TRUE, sep='')
} else {
warning(sprintf("i2dashboard object does not contain a page named '%s'", pagename))
}
}
# copy tempfile to final_document
file.copy(from = tmp_document, to = final_document, overwrite = TRUE)
file.copy(from = tmp_document, to = file, overwrite = TRUE)
invisible(object)
})
......
......@@ -4,6 +4,8 @@
#' @slot author The author of the dashboard
#' @slot interactive If a shiny-based report should be created
#' @slot theme The theme of the dashboard
#' @slot data.dir
#' @slot file
#' @slot pages A list of dashboard pages
#'
#' @name idashboard-class
......@@ -15,13 +17,15 @@ setClass("i2dashboard",
author = "character",
interactive = "logical",
theme = "character",
workdir = "character",
datadir = "character",
file = "character",
pages = "list"
),
prototype=list(
title = "i2dashboard",
interactive = FALSE,
theme = "yeti",
workdir = getwd(),
datadir = file.path(getwd(), "report-data"),
pages = list(default = list(title = "Default page", layout = "default", menu = NULL, components = list(), sidebar = NULL, max_components = Inf))
)
)
......@@ -30,9 +34,13 @@ setMethod("initialize", "i2dashboard", function(.Object, ...) {
# Do prototyping
.Object <- callNextMethod()
# Create nice filename from title
if(!is.null(.Object@title)) {
.Object@title %>% tolower %>% gsub(pattern = " ", replacement = "-") %>% gsub(pattern = '[^a-zA-Z-]', replacement = '') %>% paste0(".Rmd") -> .Object@file
}
# Create working directory and directory for environments
dir.create(.Object@workdir, showWarnings = FALSE)
dir.create(file.path(.Object@workdir, "envs"), recursive = T, showWarnings = FALSE)
dir.create(.Object@datadir, showWarnings = FALSE, recursive = T)
# Validate object - tbd
return(.Object)
......
......@@ -6,5 +6,8 @@ if (any(grepl(pattern = ".switchr", x = .libPaths()))) {
# Make it possible to reuse chunk labels
options(knitr.duplicate.label = "allow")
# Set datadir variable, components should fetch their env from here!
datadir <- "<% datadir %>"
```
......@@ -7,6 +7,6 @@ if (!requireNamespace("<% package %>", quietly = TRUE)) {
stop('The package <% package %> is needed to embed objects of class <% class %>.', call. = FALSE)
}
readRDS('envs/<% component_id %>.rds')
readRDS(file.path(datadir, '<% component_id %>.rds'))
```
---
title: <% title %>
author: <% author %>
output:
flexdashboard::flex_dashboard:
theme: <% theme %>
<% if(interactive) sprintf('runtime: shiny') %>
---
<!-- This dashboard was created with the R package 'i2dash'. https://gitlab.gwdg.de/loosolab/software/i2dash -->
\ No newline at end of file
......@@ -8,9 +8,10 @@
\alias{idashboard-class}
\alias{add_page,i2dashboard-method}
\alias{remove_page,i2dashboard-method}
\title{Method to assemble a dashboard and write it to a file}
\title{Method to assemble a dashboard to a Rmd file.}
\usage{
\S4method{assemble}{i2dashboard}(object, output_file, pages, ...)
\S4method{assemble}{i2dashboard}(object, pages = names(object@pages),
file = object@file)
\S4method{add_component}{i2dashboard,character}(object, component,
page = "default", ...)
......@@ -23,16 +24,16 @@
\arguments{
\item{object}{A \linkS4class{i2dash::i2dashboard} object.}
\item{output_file}{The output filename (recommend that the suffix should be '.Rmd'). This file will be saved in the working directory.}
\item{pages}{A string or vector with the names of pages, which should be assemble to a report.}
\item{...}{Additional parameters passed to the components render function.}
\item{file}{The output filename (recommend that the suffix should be '.Rmd'). This file will be saved in the working directory.}
\item{component}{The name of the component.}
\item{page}{The name of the page to add the component to.}
\item{...}{Additional parameters passed to the components render function.}
\item{title}{The title of the page to be added.}
\item{layout}{The page layout (see below).}
......@@ -41,8 +42,6 @@
\item{object}{A \linkS4class{i2dash::i2dashboard} object.}
\item{...}{Additional parameters passed to the components render function.}
\item{object}{A \linkS4class{i2dash::i2dashboard} object.}
\item{page}{The name of the page to be added.}
......@@ -52,7 +51,7 @@
\item{page}{The name of the page to be removed.}
}
\description{
Method to assemble a dashboard and write it to a file
Method to assemble a dashboard to a Rmd file.
Method to add a component to a page of an i2dashboard object
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment