Learn R Programming

dataset (version 0.4.1)

xsd_convert: Convert to XML Schema Definition (XSD) Types

Description

Converts R vectors, data frames, and dataset_df objects to XML Schema Definition (XSD) compatible string representations such as xsd:decimal, xsd:boolean, xsd:date, and xsd:dateTime.

Usage

xsd_convert(x, idcol = NULL, shortform = TRUE, ...)

# S3 method for haven_labelled_defined xsd_convert(x, idcol = NULL, shortform = TRUE, ...)

# S3 method for data.frame xsd_convert(x, idcol = NULL, shortform = TRUE, ...)

# S3 method for dataset_df xsd_convert(x, idcol = "rowid", shortform = TRUE, ...)

# S3 method for tbl_df xsd_convert(x, idcol = NULL, shortform = TRUE, ...)

# S3 method for character xsd_convert(x, idcol = NULL, shortform = TRUE, ...)

# S3 method for numeric xsd_convert(x, idcol = NULL, shortform = TRUE, ...)

# S3 method for integer xsd_convert(x, idcol = NULL, shortform = TRUE, ...)

# S3 method for logical xsd_convert(x, idcol = NULL, shortform = TRUE, ...)

# S3 method for factor xsd_convert(x, idcol = NULL, shortform = TRUE, ...)

# S3 method for POSIXct xsd_convert(x, idcol = NULL, shortform = TRUE, ...)

# S3 method for Date xsd_convert(x, idcol = NULL, shortform = TRUE, ...)

# S3 method for difftime xsd_convert(x, idcol = NULL, shortform = TRUE, ...)

Value

A character vector or data frame with values serialized as XSD-compatible RDF literals.

Arguments

x

An object (vector, data frame, tibble, or dataset_df).

idcol

Column name or position to use as row (observation) identifier. If NULL, row names are used.

shortform

Logical. If TRUE (default), datatypes are abbreviated with the xsd: prefix (e.g. "42"^^<xsd:integer>). If FALSE, datatypes are expanded to full URIs (e.g. "42"^^<http://www.w3.org/2001/XMLSchema#integer>).

...

Additional arguments passed to methods.

Class-specific examples

xsd_convert(42L)                   # integer -> xsd:integer
xsd_convert(c(TRUE, FALSE, NA))    # logical -> xsd:boolean
xsd_convert(Sys.Date())            # Date -> xsd:date
xsd_convert(Sys.time())            # POSIXct -> xsd:dateTime
xsd_convert(factor("apple"))       # factor -> xsd:string
xsd_convert(c("apple", "banana"))  # character -> xsd:string

Details

This is primarily used for generating RDF-compatible typed literals.

  • For vectors, returns a character vector of typed literals.

  • For data frames or tibbles, returns a data frame with the same structure but with all values converted to XSD strings.

  • For dataset_df objects, behaves like the data frame method but preserves dataset-level attributes.

Examples

Run this code
# Simple data frame with mixed types
df <- data.frame(
  id     = 1:2,
  value  = c(3.14, 2.71),
  active = c(TRUE, FALSE),
  date   = as.Date(c("2020-01-01", "2020-12-31"))
)

# Short vs long-form URI:
xsd_convert(120L, shortform = TRUE)
xsd_convert(121L, shortform = FALSE)

Run the code above in your browser using DataLab