0

I've been trying to use xmstarlet in R instead of running my xmlstarlet script in bash and then pipe it to R, but everything I've tried hasn't worked. I am not even sure if it is possible.

I've tried this:

shell(cmd=(xmlstarlet sel -t -c "someinput"), intern=TRUE)

The command alone in bash works, but when I try to do this, I get this error:

Error: unexpected symbol in "shell(cmd=(xmlstarlet sel"

I an not really sure if I should be using system() instead of shell. I've also used system, but without success

EDIT:

Full command example, can also be found here with full xml file

xmlstarlet sel -t -m "/bookstore/book/Description" -i "@stock='YES'" -v '/bookstore/book/Location/shelf'
Community
  • 1
  • 1
MLMH
  • 141
  • 8

1 Answers1

1

Save your command as a character vector and use system:

cmd <- "xml el http://stackoverflow.com"
system(command = cmd, intern = T)

This gives me

http://stackoverflow.com:12.163: EntityRef: expecting ';'
/cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded&a
                                                                               ^
[1] "html"                          "html/head"                     "html/head/title"              
[4] "html/head/link"                "html/head/link/link"           "html/head/link/link/link"     
[7] "html/head/link/link/link/meta"
attr(,"status")
[1] 4

If you are using double quotes inside the xmlstarlet command you have to escape them with a backslash. Using the example you added to the question:

cmd <- "xmlstarlet sel -t -m \"bookstore/book\" -i \"Description/stock='YES'\" -v \"Location/shelf\" -n /PATH/TO/books.xml"
system(command = cmd, intern=T)

Output:

[1] "30" "21" "11"
Martin Schmelzer
  • 23,283
  • 6
  • 73
  • 98