Jon
Jon

Reputation: 345

Export PMML to a text file?

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.

Upvotes: 3

Views: 3607

Answers (4)

user2262875
user2262875

Reputation: 121

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")

Upvotes: 10

PKumar
PKumar

Reputation: 11128

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

Upvotes: 2

IRTFM
IRTFM

Reputation: 263352

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)

Upvotes: 1

Thomas
Thomas

Reputation: 44525

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.

Upvotes: 2

Related Questions