I want to redirect all console text to a file. Here is what I tried:
> sink("test.log", type=c("output", "message")) > a <- "a" > a > How come I do not see this in log Error: unexpected symbol in "How come"
Here is what I got in test.log:
[1] "a"
Here is what I want in test.log:
> a <- "a" > a [1] "a" > How come I do not see this in log Error: unexpected symbol in "How come"
What am I doing wrong? Thanks!
To redirect the output of a command to a file, type the command, specify the > or the >> operator, and then provide the path to a file you want to the output redirected to. For example, the ls command lists the files and folders in the current directory.
You can copy and paste your console to a text file. In your History pane (next to Environment), you can select the save icon.
You have to sink "output" and "message" separately (the sink
function only looks at the first element of type
)
Now if you want the input to be logged too, then put it in a script:
script.R
1:5 + 1:3 # prints and gives a warning stop("foo") # an error
And at the prompt:
con <- file("test.log") sink(con, append=TRUE) sink(con, append=TRUE, type="message") # This will echo all input and not truncate 150+ character lines... source("script.R", echo=TRUE, max.deparse.length=10000) # Restore output to console sink() sink(type="message") # And look at the log... cat(readLines("test.log"), sep="\n")
If you have access to a command line, you might prefer running your script from the command line with R CMD BATCH.
== begin contents of script.R ==
a <- "a" a How come I do not see this in log
== end contents of script.R ==
At the command prompt ("$" in many un*x variants, "C:>" in windows), run
$ R CMD BATCH script.R &
The trailing "&" is optional and runs the command in the background. The default name of the log file has "out" appended to the extension, i.e., script.Rout
== begin contents of script.Rout ==
R version 3.1.0 (2014-04-10) -- "Spring Dance" Copyright (C) 2014 The R Foundation for Statistical Computing Platform: i686-pc-linux-gnu (32-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. [Previously saved workspace restored] > a <- "a" > a [1] "a" > How come I do not see this in log Error: unexpected symbol in "How come" Execution halted
== end contents of script.Rout ==
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With