Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I save text to a file in R?

Tags:

text

r

save

I have a R function which can generate the LaTeX code (the output is the LaTex code) by using cat(), while now I want to save these LaTeX code, but I don't know which function can save these LaTeX code...

like image 362
Eva Avatar asked Sep 20 '25 10:09

Eva


1 Answers

I like to use the sink() function:

latex.code <- function(){
   cat("\\begin{align}\n")
   cat("[X'X]^{-1}X'y\n")
   cat("\\end{align}\n")
}
sink(file='ols.txt')
latex.code()
sink()

Edit: Obviously, you can choose the file path where the file will be saved by changing the sink argument such as: sink(file='c:/Users/Eva/Desktop/ols.txt'), or sink(file='~/ols.txt')

like image 100
Vincent Avatar answered Sep 23 '25 12:09

Vincent