Home > Back-end >  R print object to file [duplicate]
R print object to file [duplicate]

Time:10-06

In R, is there any way to print an object, such as summary of a linear model, to a txt file? print() can print out the object nicely but I can't find a way to redirect the output. cat() on the other hand don't seem to be able to handle lists, I got this error back:

Error in cat(summary(lin_model), file = paste0(output_dir, "/linear_model_output.txt")) : 
  argument 1 (type 'list') cannot be handled by 'cat'

CodePudding user response:

Yes, you can use sink() prior to and after your print. The first call contains the path to your output file.

In your case:

sink(file.path(output_dir, "linear_model_output.txt"))
print(summary(lin_model))
sink()
  •  Tags:  
  • r
  • Related