How to save text to a file in R?

I have an R function that can generate LaTeX code (output is LaTex code) using cat(), and now I want to save this LaTeX code, but I don’t know which function can save these LaTeX code ...

+3
source share
3 answers

I like to use the function sink():

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 path to the file in which the file will be saved by changing the receiver argument, for example: sink(file='c:/Users/Eva/Desktop/ols.txt')orsink(file='~/ols.txt')

+5
source

, R LaTeX ( , , ), - cat() file =. ? Cat.

+3

If this happens, if you have output in a character vector (i.e. you use something like cat(<something>)that to write it to the console), you can use a function writeLines, for example:

writeLines(<something>,"filename.txt")

However, the best way to make a LaTeX file in R is to use Sweave or create a brew template.

+2
source

All Articles