Create or edit TOML documents from R using tomledit.
tomledit is written in Rust using extendr and the toml_edit crate.
Installation
Install the package from CRAN using
install.packages("tomledit")or, install the development version using
remotes::install_github("extendr/tomledit")Usage
TOML can be created using either the as_toml() or toml() functions.
Use as_toml() to convert a list to TOML:
Create TOML directly by passing key values to toml():
Or, parse a string as TOML while preserving comments:
raw_toml <- '# Top-level table begins.
name = "Fido"
breed = "pug"
# Top-level table ends.
[owner]
name = "Regina Dogman"
member_since = 1999-08-04'
x <- parse_toml(raw_toml)
x<Toml>
# Top-level table begins.
name = "Fido"
breed = "pug"
# Top-level table ends.
[owner]
name = "Regina Dogman"
member_since = 1999-08-04Write a Toml object to a file using write_toml().
tmp <- tempfile(fileext = ".toml")
write_toml(x, tmp)Read a TOML file using read_toml().
read_toml(tmp)<Toml>
# Top-level table begins.
name = "Fido"
breed = "pug"
# Top-level table ends.
[owner]
name = "Regina Dogman"
member_since = 1999-08-04Items can be inserted into a Toml document using insert_items()
y <- x |>
insert_items(
date = Sys.Date(),
date_parts = list(year = 2015L, month = "February", day = 7L)
)
y<Toml>
# Top-level table begins.
name = "Fido"
breed = "pug"
date = 2025-03-03
# Top-level table ends.
[owner]
name = "Regina Dogman"
member_since = 1999-08-04
[date_parts]
year = 2015
month = "February"
day = 7Or items can be removed as well using remove_items()
remove_items(y, c("date", "date_parts"))<Toml>
# Top-level table begins.
name = "Fido"
breed = "pug"
# Top-level table ends.
[owner]
name = "Regina Dogman"
member_since = 1999-08-04Individual items can be fetched recursively from the Toml document.
Or the entire Toml document can be converted to a list. Note, though, that it is not always possible to perform a perfect round trip of R objects and TOML.
from_toml(y)Array of Tables
By default tomledit converts data.frame objects to an array of tables.
toml(iris = iris[1:3,])<Toml>
[[iris]]
"Sepal.Length" = 5.1
"Sepal.Width" = 3.5
"Petal.Length" = 1.4
"Petal.Width" = 0.2
Species = "setosa"
[[iris]]
"Sepal.Length" = 4.9
"Sepal.Width" = 3.0
"Petal.Length" = 1.4
"Petal.Width" = 0.2
Species = "setosa"
[[iris]]
"Sepal.Length" = 4.7
"Sepal.Width" = 3.2
"Petal.Length" = 1.3
"Petal.Width" = 0.2
Species = "setosa"This is the default behavior as it is most consistent with TOML files that are encountered in the wild. To create a single table from a data.frame, set the argument df_as_array = FALSE.
toml(
iris = iris[1:3,],
df_as_array = FALSE
)Missing Values
One reason why array of tables are recommended for data.frames is because there is no concept of a missing or null value in TOML.
Take the following example:
x <- data.frame(
x = c(1L, NA, 2L),
y = letters[1:3]
) Notice that when this data.frame is serialized to TOML the missing x value is omitted:
toml(table = x)Whereas when serializing to a single table the x array has 2 elements whereas the y element has 3 elements.
toml(table = x, df_as_array = FALSE)