Getting started with Rokogiri

library(rokogiri)

Rokogiri is an R package that aims to make it incredibly easy to read and write XML files from R. It is written in homage to and spirit of the well-known Ruby gem, Nokogiri.

To get started with Rokogiri, load up your R console and try the following simple example.

library(rokogiri)

cat(rokogiri({
  catalog({
    cd({
      tiitle("Empire Burlesque")
      artist("Bob Dylan")
      country("USA")
      company("Columbia")
      price(10.90)
      year(1985)
    })

    cd({
      tiitle("Hide your heart")
      artist("Bonnie Tyler")
      country("UK")
      company("CBS Records")
      price(9.90)
      year(1988)
    })
  })
}))

As you can see, the output is the appropriate XML. The advantage of using Rokogiri over other XML generators is that the code is very readable to write; it almost looks like English.

If you wish to provide your own XML converter, you can also produce a list as output.

rokogiri(output_type = 'list', {
  note({
    to("Tove")
    from("Jani")
    heading("Reminder")
    msg("Don't forget me this weekend!")
  })
})

Note that lists in R can share the same name multiple times! This means the list produced will not lose multiple records.

output <- rokogiri(output_type = 'list', {
  note({
    to("Tove")
  })
  note({
    to("Jim")
  })
})
names(output)

If you have unorthodox tags in your XML, you can take advantage of another quirk of R. Any string surrounded in backticks or quotes can be used as a variable name.

cat(rokogiri({
  "what-if-we-need"({
    "xml-tags"()
    "like-this"(2)
  })
}))

The above example also shows that we can leave the argument to the function blank if it is an empty tag, and Rokogiri will produce the trailing slash.



robertzk/rokogiri documentation built on May 27, 2019, 11:37 a.m.