How do I log an R session to a file?
Do you know about sink()
in base R ?
There are also some logging packages on CRAN: logging, log4r and possibly more.
Lastly, Emacs user have ESS and its transcript mode. You can save your session as a log, and in general, the 'work from file and execute from the file' approach builds a (partial, commands-only) log as you work.
How to make a log-file of an R-session which combines commands, results and warnings/messages/errors from the R-console
If you are running R from a Unix/Linux/Mac/etc. terminal, you can do:
R | tee mydir/mylog.txt
On windows, you can run the script in
R CMD BATCH yourscript.R
and your result will appear in the same folder as yourscript.out
How to save all console output to file in R?
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")
How to write a reader-friendly sessionInfo() to text file
Capture the screen output into a character vector and use writeLines
.
writeLines(capture.output(sessionInfo()), "sessionInfo.txt")
Save the output of an r script including its commands
You are probably looking for the TeachingDemos
package. Documentation can be found here.
Example:
library(TeachingDemos)
txtStart("test.txt")
# Your code
txtStop()
This should write both your command input and output to a file called test.txt
.
Access locally served files within an R session
Isn't it a great feeling when you can come back and answer a question you asked!
From the httpuv::startServer()
documentation:
startServer binds the specified port and listens for connections on an thread running in the background. This background thread handles the I/O, and when it receives a HTTP request, it will schedule a call to the user-defined R functions in app to handle the request. This scheduling is done with later(). When the R call stack is empty – in other words, when an interactive R session is sitting idle at the command prompt – R will automatically run the scheduled calls. However, if the call stack is not empty – if R is evaluating other R code – then the callbacks will not execute until either the call stack is empty, or the run_now() function is called. This function tells R to execute any callbacks that have been scheduled by later(). The service() function is essentially a wrapper for run_now().
In other words, if we want to respond to requests as soon as they are received, we have to explicitly do so using httpuv::service()
. Something like the following does the trick!
s <- callr::r_session$new()
on.exit(s$close())
s$call(function() {
httpuv::startServer("0.0.0.0", port = 9359, app = list(
call = function(req) {
list(
status = 200L,
headers = list("Content-Type" = "text/html"),
body = "Some content...")
)
}
))
while (TRUE) httpuv::service()
})
# Give the server a chance to start
Sys.sleep(3)
page <- curl_fetch_memory(url = "http://127.0.0.1:9359")
Related Topics
R Create Function to Add Water Year Column
Assign Column Names to List of Dataframes
How to Set Axis Ranges in Ggplot2 When Using a Log Scale
Fitting a Lognormal Distribution to Truncated Data in R
Sort Boxplot by Mean (And Not Median) in R
Data.Table Join and J-Expression Unexpected Behavior
How to Log an R Session to a File
How to Represent Polynomials with Numeric Vectors in R
Why Is Date Is Being Returned as Type 'Double'
How to Save a Data Frame in a Txt or Excel File Separated by Columns
Check Whether All Elements of a List Are in Equal in R
Assign Names to Vector Entries Without Assigning the Vector a Variable Name
Plot Emojis/Emoticons in R with Ggplot
Setting an Individual Color Palette for the Group Variable in Geom_Smooth