After some experimentation, I found the best way (for my code) to convert an r data frame into XML is with the following script:
ConvertDataFrameToXML <- function(dataFrame,fileName) {
cat("<L><i>", file = fileName, append = TRUE)
write.table(dataFrame, fileName, append = TRUE, row.names=FALSE, col.names=FALSE, na="",sep="</i><i>",eol="</i></L><L><i>", quote = FALSE)
cat("</i></L>", file = fileName, append = TRUE)
}
The only problem is that this results in an extra blank row at the end of the table. Is there any way I can open the file and delete the last 6 characters (e.g. as a replacement for the last line of the script)?
Example code:
ConvertDataFrameToXML <- function(dataFrame,fileName) {
cat("<L><i>", file = fileName, append = TRUE)
write.table(dataFrame, fileName, append = TRUE, row.names=FALSE, col.names=FALSE, na="",sep="</i><i>",eol="</i></L><L><i>", quote = FALSE)
cat("</i></L>", file = fileName, append = TRUE)
}
a = data.frame(a= c(1,2,3),b=c(4,5,6))
fileName = "hi.txt"
file.create(fileName, overwrite = TRUE)
cat("<Tables>", file = fileName, append = TRUE)
ConvertDataFrameToXML(a,fileName)
cat("</Tables>", file = fileName, append = TRUE)
print( readChar(fileName, file.info(fileName)$size))
Current output:
<Tables><L><i>1</i><i>4</i></L><L><i>2</i><i>5</i></L><L><i>3</i><i>6</i></L><L><i></i></L></Tables>
Desired output:
<Tables><L><i>1</i><i>4</i></L><L><i>2</i><i>5</i></L><L><i>3</i><i>6</i></L></Tables>