Question

Simple question, I have stored PMML code of an R object using pmmlcode <- pmml(my.object), and I would like some way to save it directly to a text file. The usual write.table method isn't working because the data is not a table.

Was it helpful?

Solution 2

Try toString.XMLNode from XML package and then write to file with writeLines. You'll need to provide example data for a more complete answer.

OTHER TIPS

You can simply use SaveXML as in the example below:

library(randomForest)

library(pmml)

data(airquality)

ozone.out <- randomForest(Ozone ~ Wind+Temp+Month, data=na.omit(airquality), ntree=200)

saveXML(pmml(ozone.out, data=airquality), "airquality_rf.pmml")

I am using the iris data just to generate a dummy pmml file and sink command to put your pmml output into a .pmml file,

    R > library(pmml)
    R > lml <- lm(iris$Sepal.Length~iris$Sepal.Width)
    R > sink("myPmml.pmml")
    R > cat("<?xml version=\"1.0\"?>\n")
    R > pmml(lml)
    R > sink()

The output myPmml.pmml should be saved wherever your setwd is set on your .Rprofile , the default is "Mydocuments" in windows. Offcourse this will work even if you put .txt instead of .pmml in the sink() command , something like:

sink("mypmml.txt")

Edit: Added cat command to put xml tags on top, Thanks to J.Dimeo

In the absence of test code to create this but after solving my earlier problem with the availability of the pmml package on the UCLA CRAN mirror. This produces acceptable output for human readability although not in a format that will be interpretable my a PMML-aware application:

cat(paste(unlist(pmmlcode),"\n"), file="yourfile.txt")

Neither of these worked:

If it's just a character vector:

cat(pmmlcode, file="yourfile.txt")

Or if it's a list:

lapply(pmmlcode, cat, file="yourfile.txt", append=TRUE)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top