R/write_rcdf_as.R

Defines functions write_rcdf_sqlite write_rcdf_sav write_rcdf_dta write_rcdf_xlsx write_rcdf_json write_rcdf_tsv write_rcdf_csv write_rcdf_as

Documented in write_rcdf_as write_rcdf_csv write_rcdf_dta write_rcdf_json write_rcdf_sav write_rcdf_sqlite write_rcdf_tsv write_rcdf_xlsx

#' Write RCDF data to multiple formats
#'
#' Exports RCDF-formatted data to one or more supported open data formats. The function automatically dispatches to the appropriate writer function based on the `formats` provided.
#'
#' @param data A named list or RCDF object. Each element should be a table or tibble-like object (typically a `dbplyr` or `dplyr` table).
#' @param path The target directory where output files should be saved.
#' @param formats A character vector of file formats to export to. Supported formats include: `"csv"`, `"tsv"`, `"json"`, `"parquet"`, `"xlsx"`, `"dta"`, `"sav"`, and `"sqlite"`.
#' @param ... Additional arguments passed to the respective writer functions.
#'
#' @return Invisibly returns `NULL`. Files are written to disk.
#' @export
#'
#' @seealso \link[rcdf]{write_rcdf_csv}  \link[rcdf]{write_rcdf_tsv}  \link[rcdf]{write_rcdf_json}  \link[rcdf]{write_rcdf_xlsx}  \link[rcdf]{write_rcdf_dta}  \link[rcdf]{write_rcdf_sav}  \link[rcdf]{write_rcdf_sqlite}
#'
#' @examples
#' dir <- system.file("extdata", package = "rcdf")
#' rcdf_path <- file.path(dir, 'mtcars.rcdf')
#' private_key <- file.path(dir, 'sample-private-key.pem')
#'
#' rcdf_data <- read_rcdf(path = rcdf_path, decryption_key = private_key)
#' temp_dir <- tempdir()
#'
#' write_rcdf_as(data = rcdf_data, path = temp_dir, formats = c("csv", "xlsx"))
#'
#' unlink(temp_dir, force = TRUE)


write_rcdf_as <- function(data, path, formats, ...) {

  valid_formats <- c("csv", "tsv", "json", "parquet", "xlsx", "dta", "sav", "sqlite")
  label_formats <- c("CSV", "TSV", "JSON", "Parquet", "Excel", "Stata", "SPSS", "SQLite")

  valid_format_args <- which(formats %in% valid_formats)

  if(length(valid_format_args) < length(formats)) {

    n_invalid <- length(formats) - length(valid_format_args)
    n_invalid_s <- ''
    if(n_invalid > 1) { n_invalid_s <- 's' }

    stop(glue::glue('{n_invalid} invalid format{n_invalid_s} found.'))

  }


  for(i in seq_along(formats)) {

    data_format <- formats[i]
    label_format <- label_formats[which(valid_formats == data_format)]

    write_rcdf_fn <- eval(parse(text = glue::glue("write_rcdf_{data_format}")))

    write_rcdf_fn(
      data = data,
      path = path,
      ...,
      parent_dir = label_format
    )

  }

}



#' Write RCDF data to CSV files
#'
#' Writes each table in the RCDF object as a separate `.csv` file.
#'
#' @param data A valid RCDF object.
#' @param path The base output directory.
#' @param ... Additional arguments passed to `write.csv()`.
#' @param parent_dir Optional subdirectory under `path` to group CSV files.
#'
#' @return Invisibly returns `NULL`. Files are written to disk.
#' @export
#'
#' @seealso \link[rcdf]{write_rcdf_as}
#'
#' @examples
#' dir <- system.file("extdata", package = "rcdf")
#' rcdf_path <- file.path(dir, 'mtcars.rcdf')
#' private_key <- file.path(dir, 'sample-private-key.pem')
#'
#' rcdf_data <- read_rcdf(path = rcdf_path, decryption_key = private_key)
#' temp_dir <- tempdir()
#'
#' write_rcdf_csv(data = rcdf_data, path = temp_dir)
#'
#' unlink(temp_dir, force = TRUE)

write_rcdf_csv <- function(data, path, ..., parent_dir = NULL) {

  check_if_rcdf(data)
  path <- dir_create_new(path, parent_dir)

  records <- names(data)

  for(i in seq_along(records)) {

    record <- records[i]

    utils::write.csv(
      x = dplyr::collect(data[[record]]),
      file = file.path(path, glue::glue("{record}.csv"))
    )
  }

}


#' Write RCDF data to TSV files
#'
#' Writes each table in the RCDF object as a separate tab-separated `.txt` file.
#'
#' @param data A valid RCDF object.
#' @param path The base output directory.
#' @param ... Additional arguments passed to `write.table()`.
#' @param parent_dir Optional subdirectory under `path` to group TSV files.
#'
#' @return Invisibly returns `NULL`. Files are written to disk.
#' @export
#'
#' @seealso \link[rcdf]{write_rcdf_as}
#'
#' @examples
#' dir <- system.file("extdata", package = "rcdf")
#' rcdf_path <- file.path(dir, 'mtcars.rcdf')
#' private_key <- file.path(dir, 'sample-private-key.pem')
#'
#' rcdf_data <- read_rcdf(path = rcdf_path, decryption_key = private_key)
#' temp_dir <- tempdir()
#'
#' write_rcdf_tsv(data = rcdf_data, path = temp_dir)
#'
#' unlink(temp_dir, force = TRUE)

write_rcdf_tsv <- function(data, path, ..., parent_dir = NULL) {

  check_if_rcdf(data)
  path <- dir_create_new(path, parent_dir)

  records <- names(data)

  for(i in seq_along(records)) {

    record <- records[i]

    utils::write.table(
      x = dplyr::collect(data[[record]]),
      file = file.path(path, glue::glue("{record}.txt")),
      sep = "\t"
    )
  }

}


#' Write RCDF data to JSON files
#'
#' Writes each table in the RCDF object as a separate `.json` file.
#'
#' @param data A valid RCDF object.
#' @param path The output directory for files.
#' @param ... Additional arguments passed to `jsonlite::write_json()`.
#' @param parent_dir Optional subdirectory under `path` to group JSON files.
#'
#' @return Invisibly returns `NULL`. Files are written to disk.
#' @export
#'
#' @seealso \link[rcdf]{write_rcdf_as}
#'
#' @examples
#' dir <- system.file("extdata", package = "rcdf")
#' rcdf_path <- file.path(dir, 'mtcars.rcdf')
#' private_key <- file.path(dir, 'sample-private-key.pem')
#'
#' rcdf_data <- read_rcdf(path = rcdf_path, decryption_key = private_key)
#' temp_dir <- tempdir()
#'
#' write_rcdf_json(data = rcdf_data, path = temp_dir)
#'
#' unlink(temp_dir, force = TRUE)


write_rcdf_json <- function(data, path, ..., parent_dir = NULL) {

  check_if_rcdf(data)
  path <- dir_create_new(path, parent_dir)

  records <- names(data)

  for(i in seq_along(records)) {

    record <- records[i]

    jsonlite::write_json(
      x = dplyr::collect(data[[record]]),
      path = file.path(path, glue::glue("{record}.json")),
      auto_unbox = TRUE,
      pretty = TRUE
    )
  }

}


#' Write RCDF data to Excel files
#'
#' Writes each table in the RCDF object as a separate `.xlsx` file using the `openxlsx` package.
#'
#' @param data A valid RCDF object.
#' @param path The output directory.
#' @param ... Additional arguments passed to `openxlsx::write.xlsx()`.
#' @param parent_dir Optional subdirectory under `path` to group Excel files.
#'
#' @return Invisibly returns `NULL`. Files are written to disk.
#' @export
#'
#' @seealso \link[rcdf]{write_rcdf_as}
#'
#' @examples
#' dir <- system.file("extdata", package = "rcdf")
#' rcdf_path <- file.path(dir, 'mtcars.rcdf')
#' private_key <- file.path(dir, 'sample-private-key.pem')
#'
#' rcdf_data <- read_rcdf(path = rcdf_path, decryption_key = private_key)
#' temp_dir <- tempdir()
#'
#' write_rcdf_xlsx(data = rcdf_data, path = temp_dir)
#'
#' unlink(temp_dir, force = TRUE)

write_rcdf_xlsx <- function(data, path, ..., parent_dir = NULL) {

  check_if_rcdf(data)
  path <- dir_create_new(path, parent_dir)

  records <- names(data)

  for(i in seq_along(records)) {

    record <- records[i]

    openxlsx::write.xlsx(
      x = dplyr::collect(data[[record]]),
      file = file.path(path, glue::glue("{record}.xlsx")),
      ...
    )
  }

}


#' Write RCDF data to Stata `.dta` files
#'
#' Writes each table in the RCDF object to a `.dta` file for use in Stata.
#'
#' @param data A valid RCDF object.
#' @param path Output directory for files.
#' @param ... Additional arguments passed to `foreign::write.dta()`.
#' @param parent_dir Optional subdirectory under `path` to group Stata files.
#'
#' @return Invisibly returns `NULL`. Files are written to disk.
#' @export
#'
#' @seealso \link[rcdf]{write_rcdf_as}
#'
#' @examples
#' dir <- system.file("extdata", package = "rcdf")
#' rcdf_path <- file.path(dir, 'mtcars.rcdf')
#' private_key <- file.path(dir, 'sample-private-key.pem')
#'
#' rcdf_data <- read_rcdf(path = rcdf_path, decryption_key = private_key)
#' temp_dir <- tempdir()
#'
#' write_rcdf_dta(data = rcdf_data, path = temp_dir)
#'
#' unlink(temp_dir, force = TRUE)

write_rcdf_dta <- function(data, path, ..., parent_dir = NULL) {

  check_if_rcdf(data)
  path <- dir_create_new(path, parent_dir)

  records <- names(data)

  for(i in seq_along(records)) {

    record <- records[i]

    haven::write_dta(
      data = dplyr::collect(data[[record]]),
      path = file.path(path, glue::glue("{record}.dta")),
      ...
    )
  }

}


#' Write RCDF data to SPSS `.sav` files
#'
#' Writes each table in the RCDF object to a `.sav` file using the `haven` package for compatibility with SPSS.
#'
#' @param data A valid RCDF object.
#' @param path Output directory for files.
#' @param ... Additional arguments passed to `haven::write_sav()`.
#' @param parent_dir Optional subdirectory under `path` to group SPSS files.
#'
#' @return Invisibly returns `NULL`. Files are written to disk.
#' @export
#'
#' @seealso \link[rcdf]{write_rcdf_as}
#'
#' @examples
#' dir <- system.file("extdata", package = "rcdf")
#' rcdf_path <- file.path(dir, 'mtcars.rcdf')
#' private_key <- file.path(dir, 'sample-private-key.pem')
#'
#' rcdf_data <- read_rcdf(path = rcdf_path, decryption_key = private_key)
#' temp_dir <- tempdir()
#'
#' write_rcdf_sav(data = rcdf_data, path = temp_dir)
#'
#' unlink(temp_dir, force = TRUE)


write_rcdf_sav <- function(data, path, ..., parent_dir = NULL) {

  check_if_rcdf(data)
  path <- dir_create_new(path, parent_dir)

  records <- names(data)

  for(i in seq_along(records)) {

    record <- records[i]

    haven::write_sav(
      data = dplyr::collect(data[[record]]),
      path = file.path(path, glue::glue("{record}.sav")),
      ...
    )
  }

}


#' Write RCDF data to a SQLite database
#'
#' Writes all tables in the RCDF object to a single SQLite database file.
#'
#' @param data A valid RCDF object.
#' @param path Output directory for the database file.
#' @param db_name Name of the SQLite database file (without extension).
#' @param ... Additional arguments passed to `DBI::dbWriteTable()`.
#' @param parent_dir Optional subdirectory under `path` to store the SQLite file.
#'
#' @return Invisibly returns `NULL`. A `.db` file is written to disk.
#' @export
#'
#' @seealso \link[rcdf]{write_rcdf_as}
#'
#' @examples
#' dir <- system.file("extdata", package = "rcdf")
#' rcdf_path <- file.path(dir, 'mtcars.rcdf')
#' private_key <- file.path(dir, 'sample-private-key.pem')
#'
#' rcdf_data <- read_rcdf(path = rcdf_path, decryption_key = private_key)
#' temp_dir <- tempdir()
#'
#' write_rcdf_sqlite(data = rcdf_data, path = temp_dir)
#'
#' unlink(temp_dir, force = TRUE)

write_rcdf_sqlite <- function(data, path, db_name = "cbms_data", ..., parent_dir = NULL) {

  check_if_rcdf(data)
  path <- dir_create_new(path, parent_dir)

  conn <- DBI::dbConnect(RSQLite::SQLite(), file.path(path, glue::glue("{db_name}.db")))

  records <- names(data)

  for(i in seq_along(records)) {

    record <- records[i]

    DBI::dbWriteTable(
      conn = conn,
      name = record,
      value = dplyr::collect(data[[record]]),
      ...
    )

  }

  DBI::dbDisconnect(conn, force = TRUE)

}

Try the rcdf package in your browser

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

rcdf documentation built on Aug. 28, 2025, 9:09 a.m.