R/utils.R

Defines functions check_compatibility

check_compatibility <- function(chart_id, remove = NULL) {

  chart_metadata <- DatawRappr::dw_retrieve_chart_metadata(chart_id)

  chart_type <- purrr::pluck(chart_metadata, "content", "type")

  compatible_chart_types <- list(
    line_charts = c("d3-lines", "d3-area"),
    bar_charts = c("d3-bars", "d3-bars-stacked"),
    column_charts = c(
      "column-chart",
      "grouped-column-chart",
      "stacked-column-chart"
    ),
    scatter_plot = "d3-scatter-plot"
  )

  if (!purrr::is_null(remove)) {
    stopifnot(any(remove == names(compatible_chart_types)))
    compatible_chart_types[[remove]] <- NULL
  }

  # Returns TRUE if the chart type is one of the compatible chart types.
  # Currently, the package is optimized for chart types I commonly use at
  # The Jakarta Post, namely line, bar and column charts, as well as
  # scatter plots
  compatibility <- any(chart_type == purrr::flatten(compatible_chart_types))

  type_compatibility <- list(
    chart_type = chart_type,
    compatibility = compatibility
  )

  invisible(type_compatibility)

}
dzulfiqarfr/jpwrapper documentation built on Dec. 20, 2021, 2:20 a.m.