R/set_grobFromGtable.R

Defines functions set_grobFromGtable.l_facet_grid set_grobFromGtable.l_facet_wrap set_grobFromGtable.l_facet_ggplot set_grobFromGtable.default set_grobFromGtable

set_grobFromGtable <- function(gtable, newGrobs, plotRegionBackground, arrangeGrobArgs) {
  l_className <- gtable$name
  class(l_className) <- l_className
  UseMethod("set_grobFromGtable", l_className)
}


set_grobFromGtable.default <- function(gtable, newGrobs, plotRegionBackground, arrangeGrobArgs) {

  arrangeGrobArgs$grobs <- newGrobs

  gTree(
    children = grid::gList(
      grid::rectGrob(gp  = grid::gpar(fill = plotRegionBackground,
                                      col = NA),
                     name = "bounding box"),
      do.call(gridExtra::arrangeGrob, arrangeGrobArgs)
    ), name = "l_shiny"
  )
}

set_grobFromGtable.l_facet_ggplot <- function(gtable, newGrobs, plotRegionBackground, arrangeGrobArgs) {

  namesNewGrobs <- vapply(newGrobs, function(ng) ng$name, character(1L))

  if(!is.gtable(gtable)) return(set_grobFromGtable.default(gtable, newGrobs, plotRegionBackground, arrangeGrobArgs))

  grobs <- gtable$grobs
  len <- length(grobs)

  if(len == 0) return(set_grobFromGtable.default(gtable, newGrobs, plotRegionBackground, arrangeGrobArgs))

  for(i in seq(len)) {

    grob <- grobs[[i]]

    if(is.gtable(grob)) {

      childGrobs <- grob$grobs

      grob$grobs <- lapply(childGrobs,
                           function(cg) {
                             ith <- which(namesNewGrobs %in% cg$name)
                             if(length(ith) > 0) {
                               newGrobs[[ith]]
                             } else cg
                           })

    } else NULL

    grobs[[i]] <- grob
  }

  gtable$grobs <- grobs
  return(gtable)
}

set_grobFromGtable.l_facet_wrap <- function(gtable, newGrobs, plotRegionBackground, arrangeGrobArgs) {

  namesNewGrobs <- vapply(newGrobs, function(ng) ng$name, character(1L))

  if(!is.gtable(gtable)) return(set_grobFromGtable.default(gtable, newGrobs, plotRegionBackground, arrangeGrobArgs))

  grobs <- gtable$grobs
  len <- length(grobs)

  if(len == 0) return(set_grobFromGtable.default(gtable, newGrobs, plotRegionBackground, arrangeGrobArgs))

  for(i in seq(len)) {

    grob <- grobs[[i]]

    if(is.gtable(grob)) {

      childGrobs <- grob$grobs

      grob$grobs <- lapply(childGrobs,
                           function(cg) {
                             ith <- which(namesNewGrobs %in% cg$name)
                             if(length(ith) > 0) {
                               newGrobs[[ith]]
                             } else cg
                           })

    } else NULL

    grobs[[i]] <- grob
  }

  gtable$grobs <- grobs
  return(gtable)
}

set_grobFromGtable.l_facet_grid <- function(gtable, newGrobs, plotRegionBackground, arrangeGrobArgs) {

  namesNewGrobs <- vapply(newGrobs, function(ng) ng$name, character(1L))

  if(!is.gtable(gtable)) return(set_grobFromGtable.default(gtable, newGrobs, plotRegionBackground, arrangeGrobArgs))

  grobs <- gtable$grobs
  len <- length(grobs)

  if(len == 0) return(set_grobFromGtable.default(gtable, newGrobs, plotRegionBackground, arrangeGrobArgs))

  for(i in seq(len)) {

    grob <- grobs[[i]]

    if(is.gtable(grob)) {

      childGrobs <- grob$grobs

      grob$grobs <- lapply(childGrobs,
                           function(cg) {
                             ith <- which(namesNewGrobs %in% cg$name)
                             if(length(ith) > 0) {
                               newGrobs[[ith]]
                             } else cg
                           })

    } else NULL

    grobs[[i]] <- grob
  }

  gtable$grobs <- grobs
  return(gtable)
}

Try the loon.shiny package in your browser

Any scripts or data that you put into this service are public.

loon.shiny documentation built on Oct. 8, 2022, 5:05 p.m.