R/data_dependency.R

Defines functions get_data add_data data_dependency

Documented in add_data get_data

## experimental

data_dependency <- function(data, var_name) {
  src <- tempdir()
  filename <- tolower(var_name) %>% paste0(".js")
  paste0("var ", var_name, "=", jsonlite::toJSON(data, auto_unbox = TRUE, force = TRUE)) %>%
    readr::write_file(file.path(src, filename))
  list(
    htmltools::htmlDependency(
      paste0("data-", tolower(var_name)),
      version = "42",
      src = src,
      script = filename,
      all_files = FALSE
    )
  )
}

#' Add JavaScript data file
#'
#' EXPERIMENTAL
#' @param deckgl deckgl widget
#' @param data data object
#' @param var_name JavaScript variable name used to make the data available
#' @export
add_data <- function(deckgl, data, var_name = "thanksForAllTheFish") {
  .Deprecated("add_source")

  deckgl$dependencies <- c(
    deckgl$dependencies,
    data_dependency(data, var_name)
  )
  deckgl
}

#' Get data
#'
#' EXPERIMENTAL, usually used in conjunction with \link{add_data}
#' @param var_name JavaScript variable name
#' @export
get_data <- function(var_name = "thanksForAllTheFish") {
  .Deprecated("add_source")

  JS(var_name)
}

Try the deckgl package in your browser

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

deckgl documentation built on July 1, 2020, 7:36 p.m.