Slyron
Slyron

Reputation: 804

How to create periodically send text to a "log file" while printing normal output to console?

I'm creating R code for a Monte Carlo simulation of a professional sport. Because the game dynamics are very complicated and to make the debugging process simpler, I'd like to have R send a line of text for every action that happens in the game to a "log file." The log file would be a comprehensive, play by play description of what's happening in the simulation, and would look something like this…

I can't just use the sink() function because while the simulation is running, I setup a progress bar (with the setTxtProgressBar function) and real time scores to be printed to the console. If I used sink(), I couldn't see any of the progress indicators or scores on the R console. Does this make sense? In other words I need to periodically send text to a log file in a cumulative fashion. Here is some example code to give you something to work with…

Thanks

for (i in 1:100)
{**SOMEHOW NEED TO PRINT LINE "START LOOP" TO LOG FILE**;
a <- rnorm(n = 100, mean = i, sd = 5);
print(mean(a)); #PRINT THIS MEAN TO THE CONSOLE
**SOMEHOW PRINT "LOOP 'i' COMPLETE" TO LOG FILE**}

Upvotes: 17

Views: 15664

Answers (6)

user20697704
user20697704

Reputation: 1

Same example, including values written to the log.txt file:

    log_con <- file("log.txt", open="a")

    for (i in 1:10){
      cat("loop begins; i =", i, '\n', file = log_con)
      a <- rnorm(n = 100, mean = i, sd = 5)
      print(mean(a))
      cat("single loop completed; mean(a) = ", mean(a), '\n', file = 
      log_con)
    } 
    close(log_con)

Upvotes: 0

izmirlig
izmirlig

Reputation: 9

write("this message", file=stderr())

Upvotes: 0

Gregor Thomas
Gregor Thomas

Reputation: 146040

See ?cat. You can open a file connection to your log file and specify that in your cat call. When you don't specify a file name or connection it will print to the console.

As you say, don't use sink() as it will make the log file the default connection. Rather, open a named connection with file().

> log_con <- file("test.log")
> cat("write to log", file = log_con) # creates file and writes to it
> cat("write to console") # prints to console
write to console

The above results in a log file with the line "write to log" and "write to console" printed on the console.

If you need to append to your log file, set append = TRUE and use the file name instead of the file() connection.

> cat("add to log", file = "test.log", append = TRUE)

Upvotes: 18

J&#233;r&#244;me B
J&#233;r&#244;me B

Reputation: 427

The library log4r seems to be more complete than an homemade one: https://github.com/johnmyleswhite/log4r

Upvotes: 1

Slyron
Slyron

Reputation: 804

Figured it out, thanks to shujaa and BigFinger. To summarize, here is how you would do it with my example code:

log_con <- file("/filepath/log.txt", open="a")

for (i in 1:100)
{
cat("loop begins", file = log_con, sep="\n")
a <- rnorm(n = 100, mean = i, sd = 5)
print(mean(a))
cat("single loop completed", file = log_con, sep="\n")
}

close(log_con)

Upvotes: 7

BigFinger
BigFinger

Reputation: 1043

To open the log file in "append" mode:

log_con <- file("test.log",open="a")

Upvotes: 9

Related Questions