R/brainy.R

#' Initialise
#'
#' Initialise a brain.
#'
#' @param elementId Id of DOM.
#'
#' @import htmlwidgets
#'
#' @export
brainy <- function(elementId = NULL){
  
  x = list()
  
  # create widget
  htmlwidgets::createWidget(
    name = 'brainy',
    x,
    width = NULL,
    height = NULL,
    package = 'brainy',
    elementId = elementId
  )
}

brainy_html <- function(id, style, class, ...){
  htmltools::tags$span(id = id, class = class)
}

#' Shiny bindings for brainy
#'
#' Output and render functions for using brainy within Shiny
#' applications and interactive Rmd documents.
#'
#' @param outputId output variable to read from
#' @param width,height Must be a valid CSS unit (like \code{'100\%'},
#'   \code{'400px'}, \code{'auto'}) or a number, which will be coerced to a
#'   string and have \code{'px'} appended.
#' @param expr An expression that generates a brainy
#' @param env The environment in which to evaluate \code{expr}.
#' @param quoted Is \code{expr} a quoted expression (with \code{quote()})? This
#'   is useful if you want to save an expression in a variable.
#'
#' @name brainy-shiny
#'
#' @export
brainyOutput <- function(outputId, width = '100%', height = '400px'){
  htmlwidgets::shinyWidgetOutput(outputId, 'brainy', width, height, package = 'brainy')
}

#' @rdname brainy-shiny
#' @export
renderBrainy <- function(expr, env = parent.frame(), quoted = FALSE) {
  if (!quoted) { expr <- substitute(expr) } # force quoted
  htmlwidgets::shinyRenderWidget(expr, brainyOutput, env, quoted = TRUE)
}
brain-r/brain.widget documentation built on May 28, 2019, 5:42 p.m.