Learn R Programming

xmlwriter (version 0.1.1)

data_frag: Create an xml_fragment from a data.frame

Description

Create a xml_fragment() from a data.frame, in which each row is a set of xml elements (columns).

Usage

data_frag(df, row_tags = "row", .attr = NULL)

Value

xml_fragment() object

Arguments

df

data frame that will be stored as set of xml elements

row_tags

character the tag name that is used for each row. Note that this can be a single value or a vector of length equal to the number of rows in the data.frame.

.attr

optional data.frame with xml row attributes

See Also

Other xml_fragment: add_child_fragment(), as.character.xml_fragment(), as_frag(), as_xml_nodeset(), frag(), xml_fragment()

Examples

Run this code
persons <- data.frame(
  name = c("John Doe", "Jane Doe"),
  age = c(30, 25),
  stringsAsFactors = FALSE
)

df <- data_frag(persons, row_tag = "person")
print(df)

# setting ids on rows
persons <- data.frame(
  name = c("John Doe", "Jane Doe"),
  age = c(30, 25),
  id = c("p1", "p2"),
  stringsAsFactors = FALSE
)

df <- data_frag(
  persons[1:2],
  row_tag = "person",
  .attr = persons[3]
)

print(df)

# turning it into a document
doc <- xml_doc("study", id = "1") / frag(
  source = "homeless db",
  data = df
)

cat(as.character(doc))

Run the code above in your browser using DataLab