Search code examples
rexport-to-excel

Simultaneously Output and show the result in R


I'm wondering if it would be possible to both show a cat(...) in the console (Rstudio) "AND ALSO" save the file in .txt format?

Here is my R code:

   SOS = 33
    df = 12

  cat("\n","-------------", "\n" ,"SOS  ","  df","\n", "-------------","\n",
       SOS,"    ",df,"\n", "-------------", file = "Output.txt" )

Solution

  • SOS = 33
    df = 12
    
    #prepare your output
    x = paste("\n","-------------", "\n" ,"SOS "," df","\n", "-------------","\n",
                                        SOS," ",df,"\n", "-------------", sep = "")
    
    #display in console
    cat(x)
    #-------------
    #SOS  df
    #-------------
    #33 12
    #-------------
    
    #Write to a txt file
    cat(x, file = "output.txt")