| Title: | Parse, Read, and Edit 'TOML' |
|---|---|
| Description: | A toolkit for working with 'TOML' files in R while preserving formatting, comments, and structure. 'tomledit' enables serialization of R objects such as lists, data.frames, numeric, logical, and date vectors. |
| Authors: | Josiah Parry [aut, cre] (ORCID: <https://orcid.org/0000-0001-9910-865X>), Devin Pastoor [ctb] |
| Maintainer: | Josiah Parry <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.1.9000 |
| Built: | 2026-06-10 07:39:02 UTC |
| Source: | https://github.com/extendr/tomledit |
Use as_toml() to convert a named list to a Toml object.
Or, create a Toml object by passing in named values to toml().
as_toml(x, df_as_array = TRUE) toml(..., df_as_array = TRUE)as_toml(x, df_as_array = TRUE) toml(..., df_as_array = TRUE)
x |
a named list |
df_as_array |
default |
... |
named items to be serialized to TOML. |
If you are serializing a data.frame to a single table with df_as_array = FALSE,
note that missing values are omitted when serializing a vector to an array as there is no
concept of missing values in TOML.
an object of class Toml
toml(person = list(age = 30L, name = "Wilma")) as_toml( list( person = list(age = 30L, name = "Wilma") ) )toml(person = list(age = 30L, name = "Wilma")) as_toml( list( person = list(age = 30L, name = "Wilma") ) )
Toml to an R objectUse from_toml() to convert a Toml document to an R object.
Note that that due to the encoding of values in the TOML specification
a perfect round trip from TOML to R is not always possible.
from_toml(x)from_toml(x)
x |
a |
a list
from_toml(toml(hello = "world"))from_toml(toml(hello = "world"))
Use parse_toml() to parse a string into a Toml document.
Use read_toml() to read a .toml file from disk.
parse_toml(x) read_toml(file)parse_toml(x) read_toml(file)
x |
a character scalar containing valid TOML |
file |
path to the file to write. |
an object of class Toml
# TOML string raw_toml <- '# Top-level table begins. name = "Fido" breed = "pug" # Top-level table ends. [owner] name = "Regina Dogman" member_since = 1999-08-04' # write the TOML string to a temp file tmp <- tempfile() writeLines(raw_toml, tmp) # parse the TOML string parse_toml(raw_toml) # read the TOML file read_toml(tmp)# TOML string raw_toml <- '# Top-level table begins. name = "Fido" breed = "pug" # Top-level table ends. [owner] name = "Regina Dogman" member_since = 1999-08-04' # write the TOML string to a temp file tmp <- tempfile() writeLines(raw_toml, tmp) # parse the TOML string parse_toml(raw_toml) # read the TOML file read_toml(tmp)
remove_items() removes one or more items from the TOML document.
Alternatively, insert_items() inserts key value pairs into the TOML
document.
remove_items(x, keys) insert_items(x, ..., df_as_array = TRUE) get_item(x, key)remove_items(x, keys) insert_items(x, ..., df_as_array = TRUE) get_item(x, key)
x |
an object of class |
keys |
a character vector of key names to remove. Cannot contain missing values. |
... |
named items to be serialized to TOML. |
df_as_array |
default |
key |
a character vector of key values. The keys are used recursively. For example with |
an object of class Toml
x <- toml( date = list( full = as.Date("2025-02-07"), parts = list(year = 2015L, month = "February", day = 7L) ), season = "winter" ) # fetch the date table get_item(x, "date") # fetch the month value get_item(x, c("date", "parts", "month")) # remove an item based on name remove_items(x, "season") # add multiple items insert_items(x, temperature = 31, cloudy = TRUE)x <- toml( date = list( full = as.Date("2025-02-07"), parts = list(year = 2015L, month = "February", day = 7L) ), season = "winter" ) # fetch the date table get_item(x, "date") # fetch the month value get_item(x, c("date", "parts", "month")) # remove an item based on name remove_items(x, "season") # add multiple items insert_items(x, temperature = 31, cloudy = TRUE)
Write a Toml object to a file or to a string. Use write_toml() to
write to a file on disk. Or, use to_toml() to create a string
containing TOML.
write_toml(x, file, format = TRUE) to_toml(x, format = TRUE)write_toml(x, file, format = TRUE) to_toml(x, format = TRUE)
x |
an object of class |
file |
path to the file to write. |
format |
whether to format the TOML output. |
write_toml() returns a Toml object invisibly. to_toml() returns a string.
tmp <- tempfile(fileext = ".toml") x <- toml( today = Sys.Date(), human = list(person = "Greg", age = 29, bday = "1969-07-02"), ) write_toml(x, tmp) read_toml(tmp) to_toml(x)tmp <- tempfile(fileext = ".toml") x <- toml( today = Sys.Date(), human = list(person = "Greg", age = 29, bday = "1969-07-02"), ) write_toml(x, tmp) read_toml(tmp) to_toml(x)