9

Let's say I am running some code in R. As an example:

x <- 1

if (x == 0.1){
    print('X = 0.1')
    } else if (x > 0.1){
    print('X is bigger than 0.1 ')
    } else {
    print('X is smaller than 0.1')
    }

If I look at the history file in R studio it will show me that I ran this conditional statement but it will not show me the outcome (i.e. X is bigger than 0.1).

Is there a way to automatically log the output in R or R studio?

Phil
  • 7,287
  • 3
  • 36
  • 66
Ido Hatam
  • 113
  • 1
  • 1
  • 6
  • Have a function return something, and save it to a variable? What do you want to do with the output? – Heroka Dec 01 '15 at 18:29
  • What ever it is people do with outputs, publish. However sometime you run something and forget to record it or it doesn't seem important at the time so you don't and than you end up having to find your data and run the command again. I was just wondering if there is a way to setup automatic archiving of the output for disorganized people such as myself. – Ido Hatam Dec 01 '15 at 18:42

3 Answers3

12

Direct output to a log file and screen:

sink("myfilename", append=FALSE, split=TRUE)  # for screen and log

Return output to the screen only:

sink()

From Quick-R

Jaap
  • 81,064
  • 34
  • 182
  • 193
ddunn801
  • 1,900
  • 1
  • 15
  • 20
  • So basically I will have to do that before I run a command? there is no way to default the recording of the output as well in the history file? – Ido Hatam Dec 01 '15 at 18:32
  • 2
    Run it once, then everything after that gets saved to the logfile until you type sink() again. – ddunn801 Dec 01 '15 at 18:51
  • Thanks, great. This does the trick, for some reason I can't see the output in the console but it does appear in the txt file I direct it to. – Ido Hatam Dec 01 '15 at 19:43
  • 1
    OK came out stupid, need to change the split parameter had it as F :) – Ido Hatam Dec 01 '15 at 19:55
4

You need to combine sink() and cat().

First, in your script or at the console, you can use sink() to start your log file and send text to the terminal at the same time:

sink("myfile.log", append=TRUE, split=TRUE)

This will also append data to an existing log file.

Then replace print with cat in your functions, so that output is captured by the log file. So, in your example:

sizex <- function(x){
if (x ==0.1){
    cat('X = 0.1')
    } else if (x > 0.1){
    cat('X is bigger than 0.1 ')
    } else {
    cat('X is smaller than 0.1')
    }
}

and then we'd use the function:

x <- 1
sizex(x)

If for some reason you want to stop logging, just type sink(). The nice thing about cat() is that it will revert to pushing text to the the terminal without any effort on your behalf if you are using sink().

N.B. If you want to know the difference between cat and print, see What is the difference between cat and print?.

Community
  • 1
  • 1
Andy Clifton
  • 4,926
  • 3
  • 35
  • 47
0

One can search for "log output packages R" or something similar . Here is one example I pulled from the search.

library(log4r)
#> 
#> Attaching package: 'log4r'
#> The following object is masked from 'package:base':
#> 
#>     debug

my_logfile = "my_logfile.txt"

my_console_appender = console_appender(layout = default_log_layout())
my_file_appender = file_appender(my_logfile, append = TRUE, 
                            layout = default_log_layout())

my_logger <- log4r::logger(threshold = "INFO", 
                appenders= list(my_console_appender,my_file_appender))

log4r_info <- function() {
  log4r::info(my_logger, "Info_message.")
}

log4r_error <- function() {
  log4r::error(my_logger, "Error_message")
}

log4r_debug <- function() {
  log4r::debug(my_logger, "Debug_message")
}

log4r_debug() # will not trigger log entry because threshold was set to INFO

log4r_info() 
#> INFO  [2020-07-01 12:48:02] Info_message.

log4r_error() 
#> ERROR [2020-07-01 12:48:02] Error_message

readLines(my_logfile)
#> [1] "INFO  [2020-07-01 12:48:02] Info_message."
#> [2] "ERROR [2020-07-01 12:48:02] Error_message"
Andrew Borst
  • 338
  • 3
  • 12