1

I am trying after the following code to find a way to save as a pdf the image output that appears after the fancyRpartPlot to plot the regression tree. Anybody knows how can I do that? I couldn't find any answer through the web.

regression_tree <- data.frame(stock_mag = as.numeric(resData$stock_mag), 
                   LT = as.numeric(resData$Lead_Time), 
                   dmIn = as.numeric(resData$Intermittency), 
                   dmCv = as.numeric(resData$CoV))

fit<-rpart(stock_mag~dmCv+dmIn+LT,data=regression_tree, method="anova",          control=rpart.control(minsplit=20))
x11()

fancyRpartPlot(fit, main="test")   # Will plot the tree#

Thanks!!

Aida
  • 79
  • 1
  • 8
  • Welcome to SO! Please get used to provide reproducible examples like described here: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – lukeA May 21 '15 at 09:53

1 Answers1

1

This should work just as with every other plot using pdf():

library(rattle)
library(rpart)
set.seed(42)
ds     <- weather
target <- "RainTomorrow"
risk   <- "RISK_MM"
ignore <- c("Date", "Location", risk)
vars   <- setdiff(names(ds), ignore)
nobs   <- nrow(ds)
form   <- formula(paste(target, "~ ."))
train  <- sample(nobs, 0.7*nobs)
test   <- setdiff(seq_len(nobs), train)
actual <- ds[test, target]
risks  <- ds[test, risk]
model <- rpart(form, data=ds[train, vars])

pdf(tf <- tempfile(fileext = ".pdf"))
fancyRpartPlot(model)
dev.off()
cat(tf) # filename
lukeA
  • 53,097
  • 5
  • 97
  • 100
  • I tried what you said but when I try to open it in windows ( I use R studio in a windows environment) it doesn't find the program to open it. Maybe I have to specify some parameter or something? – Aida May 21 '15 at 11:49
  • You mean there's no programm associated with a pdf extension? Then it would be a windows problem. – lukeA May 21 '15 at 16:00
  • Solved, the adobe was not properly installed in windows I fixed it and now it works. Thanks! – Aida May 21 '15 at 16:04