Walshe_d
Walshe_d

Reputation: 171

How to write the text R outputs from a function to a file?

I have a function custom_1 and when I use print(custom_1) R prints the returned data.frame but also some text at the bottom such as

The final values used for the model were mtry = 10 and ntree = 250

What i want to do is print the data.frame and the text but every time I try to write to a file only the dataframe gets printed.

This is the code i've been using.

write.csv(print(custom_1), file="results2.csv")

Upvotes: 0

Views: 627

Answers (1)

data princess
data princess

Reputation: 1158

Use a sink. In your case:

sink("results2.csv") # open the connection
print(custom_1)
sink()               # close the connection

The sink function takes output that would ordinarily go to the console and writes it to the specified file.

Upvotes: 1

Related Questions