formatters function

Pre-supplied formatting generators

Pre-supplied formatting generators

This set of functions can be used to construct formatting functions adhering to the Response$format() requirements.

format_json( dataframe = "rows", matrix = "rowmajor", Date = "ISO8601", POSIXt = "string", factor = "string", complex = "string", raw = "base64", null = "list", na = "null", auto_unbox = FALSE, digits = 4, pretty = FALSE, force = FALSE ) format_plain(sep = "\n") format_xml(encoding = "UTF-8", options = "as_xml") format_html(encoding = "UTF-8", options = "as_html") format_table(...)

Arguments

  • dataframe: how to encode data.frame objects: must be one of 'rows', 'columns' or 'values'

  • matrix: how to encode matrices and higher dimensional arrays: must be one of 'rowmajor' or 'columnmajor'.

  • Date: how to encode Date objects: must be one of 'ISO8601' or 'epoch'

  • POSIXt: how to encode POSIXt (datetime) objects: must be one of 'string', 'ISO8601', 'epoch' or 'mongo'

  • factor: how to encode factor objects: must be one of 'string' or 'integer'

  • complex: how to encode complex numbers: must be one of 'string' or 'list'

  • raw: how to encode raw objects: must be one of 'base64', 'hex' or 'mongo'

  • null: how to encode NULL values within a list: must be one of 'null' or 'list'

  • na: how to print NA values: must be one of 'null' or 'string'. Defaults are class specific

  • auto_unbox: automatically unbox() all atomic vectors of length 1. It is usually safer to avoid this and instead use the unbox() function to unbox individual elements. An exception is that objects of class AsIs (i.e. wrapped in I()) are not automatically unboxed. This is a way to mark single values as length-1 arrays.

  • digits: max number of decimal digits to print for numeric values. Use I() to specify significant digits. Use NA for max precision.

  • pretty: adds indentation whitespace to JSON output. Can be TRUE/FALSE or a number specifying the number of spaces to indent. See prettify()

  • force: unclass/skip objects of classes with no defined JSON mapping

  • sep: The line separator. Plain text will be split into multiple strings based on this.

  • encoding: The character encoding to use in the document. The default encoding is UTF-8 . Available encodings are specified at http://xmlsoft.org/html/libxml-encoding.html#xmlCharEncoding.

  • options: default: format . Zero or more of

    • format: Format output
    • no_declaration: Drop the XML declaration
    • no_empty_tags: Remove empty tags
    • no_xhtml: Disable XHTML1 rules
    • require_xhtml: Force XHTML rules
    • as_xml: Force XML output
    • as_html: Force HTML output
    • format_whitespace: Format with non-significant whitespace
  • ...: parameters passed on to write.table()

Returns

A function accepting an R object

Examples

fake_rook <- fiery::fake_request( 'http://example.com/test', content = '', headers = list( Content_Type = 'text/plain', Accept = 'application/json, text/csv' ) ) req <- Request$new(fake_rook) res <- req$respond() res$body <- mtcars res$format(json = format_json(), csv = format_table(sep=',')) res$body # Cleaning up connections rm(fake_rook, req, res) gc()

See Also

parsers for converting Request bodies into R objects

default_formatters for a list that maps the most common mime types to their respective formatters

  • Maintainer: Thomas Lin Pedersen
  • License: MIT + file LICENSE
  • Last published: 2022-08-19