Skip to content
Snippets Groups Projects
pages.R 2.22 KiB
Newer Older
  • Learn to ignore specific revisions
  • #' Methods to add and remove pages of an i2dashboard.
    
    jens.preussner's avatar
    jens.preussner committed
    #'
    
    #' \code{add_page} creates a page and adds it to the \linkS4class{i2dashboard} object.
    #' \code{remove_page} removes a page from the \linkS4class{i2dashboard} object.
    
    jens.preussner's avatar
    jens.preussner committed
    #'
    
    #' @param dashboard A \linkS4class{i2dashboard}.
    #' @param page The name of the page to be added or removed.
    
    jens.preussner's avatar
    jens.preussner committed
    #' @param title The title of the page to be added.
    #' @param layout The page layout (see below).
    #' @param menu The name of the menu, under which the page should appear.
    
    #' @param sidebar A Markdown string. Preferably, use the function add_to_sidebar.
    #' @param ... Additional arguments.
    
    jens.preussner's avatar
    jens.preussner committed
    #'
    
    #' @return The \linkS4class{i2dashboard} object.
    #'
    #' @rdname i2dashboard-pages
    
    setMethod("add_page", "i2dashboard", function(dashboard, page, title, layout = "default", menu = NULL, sidebar = NULL, ...) {
    
    jens.preussner's avatar
    jens.preussner committed
      name <- .create_page_name(page)
    
    
      max_components <- switch(layout,
                               "default" = Inf,
                               "storyboard" = Inf,
                               "focal_left" = 3,
                               "2x2_grid" = 4
      )
    
    
      if(name %in% names(dashboard@pages)) {
    
        warning(paste("The page", name, "already exists and will be overwritten."))
    
        if(base::interactive()) {
    
          overwrite_page <- menu(c("Yes, overwrite page", "Cancel"), title = "Do you want to overwrite this page?")
          skip <- switch(overwrite_page,
    
                         "1" = FALSE,
                         "2" = TRUE)
          if (skip) stop("Aborted.")
    
      dashboard@pages[[name]] <- list(title = title, layout = layout, menu = menu, components = list(), max_components = max_components, sidebar = sidebar)
      return(dashboard)
    
    jens.preussner's avatar
    jens.preussner committed
    })
    
    
    #' @rdname i2dashboard-pages
    
    setMethod("remove_page", "i2dashboard", function(dashboard, page) {
    
    jens.preussner's avatar
    jens.preussner committed
      name <- .create_page_name(page)
    
      dashboard@pages[[name]] <- NULL
      return(dashboard)
    
    
    #' Sanitize component names
    #'
    #' This function takes a character string, replaces spaces by underscores and runs make.names.
    #'
    #' @param x A character string.
    #'
    #' @return A sanitized string.
    .create_page_name <- function(x) {
      . = NULL # workaround for R CMD check note: no visible binding for global variable '.'
      x %>% tolower %>% gsub(x = ., pattern = " ", replacement = "_") %>% make.names %>% return
    }