2

I am using rattle::asRules() to display rpart decision rules to the screen, my goal is to save the rules to a tidy data frame.

Here is an example:

library(rpart)
library(rattle)

fit <- rpart(Kyphosis ~ Age + Number + Start, data = kyphosis)
rattle::asRules(fit, TRUE)

Desired Output: enter image description here

G5W
  • 36,531
  • 10
  • 47
  • 80
ronencozen
  • 1,991
  • 1
  • 15
  • 26
  • Printing of the table is implemented [using `cat`](https://github.com/cran/rattle/blob/master/R/asRules.rpart.R). Your best bet would be to fork repo and modify the function. At the least output raw values (currently the result is order of values). – Roman Luštrik Oct 07 '18 at 07:08

1 Answers1

1

In the end, I used the sink() function to diverts R output to a file.

sink("rules.txt")
rattle::asRules(fit, TRUE)
sink()
ronencozen
  • 1,991
  • 1
  • 15
  • 26
  • Having the same issue. This is the only thing I can find that works. But I made a couple alterations to avoid saving to an external file. `sink("clipboard") rattle::asRules(tree, compact = TRUE) treerules <- read.table(file = "clipboard", sep = "\t", header = F, stringsAsFactors = F) sink()` – hmhensen Nov 22 '22 at 22:54