export: Write CourseKata data to file

exportR Documentation

Write CourseKata data to file

Description

Processed data from the course often has one or more list-columns in it. A list-column is a column in a data frame where each of the rows (elements) in that column is a list. These columns have some ambiguity in how they should be written to file. As a sensible default, the function provides a mask for utils::write.csv where the data is first passed through convert_lists. See the details section for more information about the conversion process.

Usage

write.csv(
  x,
  file,
  na = "NA",
  row.names = FALSE,
  converter = jsonlite::toJSON,
  ...
)

convert_lists(x, converter = jsonlite::toJSON, ...)

Arguments

x

the object to be written, preferably a matrix or data frame. If not, it is attempted to coerce x to a data frame.

file

either a character string naming a file or a connection open for writing. "" indicates output to the console.

na

the string to use for missing values in the data.

row.names

either a logical value indicating whether the row names of x are to be written along with x, or a character vector of row names to be written.

converter

An function that takes a list and returns a string. By default, it uses jsonlite::toJSON.

...

Arguments passed on to the converter function.

Details

Data from CourseKata classes often has JSON (Javascript Object Notation) in some of the columns. Though it is called JavaScript object notation, this is a standard and common format for sending and receiving data because it has clear rules for representing various data structures. When this kind of data is read-in using one of the ⁠process_*()⁠ functions like process_data, it is converted to a list, which is a more natural form of data in R. Unfortunately, when a data frame has a list-column, there is ambiguity in how to write that column to file. JSON is a sensible default because it does not lose any information in the conversion process, even in more complex cases.

If you would like to write your own converted, the export functions here will let you specify one to use. An example of a custom converter and how to specify it are provided below.

Value

The converted data frame (where lists are formatted as strings) is returned. This makes it easier to use this function in a pipe %>%).

Examples


# example table with a list-column
tbl_nested <- tibble::tibble(x = list(list(1, 2, 3), list(4, 5, 6)))

# convert the column to a JSON string representation
convert_lists(tbl_nested)

# custom converter that just comma-separates top-level values
to_comma <- function(x) paste0(x, collapse = ",")
convert_lists(tbl_nested, to_comma)

# an example of a more deeply nested structure being converted to JSON
tbl_deep_nested <- tibble::tibble(x = list(
  # first element/row of column x
  list(
    list(1, 2, 3),
    list(4, 5, 6)
  ),
  # second element/row of column x
  list(
    list(7, 8, 9)
  )
))
convert_lists(tbl_deep_nested)

UCLATALL/CourseKataData documentation built on Dec. 4, 2023, 2:25 a.m.