tidylog Integration

The tidylog package supports console messaging for most dplyr and tidyr functions. This package integrates nicely with logr to provide automatic logging for common data manipulation tasks.

Autolog Integration

For logr version 1.2.0 and later, tidylog integration has been incorporated into the autolog feature. To engage autolog, simply turn it on when you call log_open, or set the autolog global option, as follows:

globals("logr.autolog" = TRUE)

The autolog feature also allows other packages to automatically write to the logr log. Overall, autolog makes your code cleaner and the logging more automatic. It is recommended to use autolog if the situation allows it.

Autolog Example

Here is a small example showing the autolog feature:

library(logr)
library(dplyr)
library(magrittr)

# Create temp file location
tmp <- file.path(tempdir(), "test.log")

# Open log
lf <- log_open(tmp, autolog = TRUE, show_notes = FALSE)

# Print log header
sep("Example of autolog feature")

# Send message to log
put("High Mileage Cars Subset")

# Perform dplyr operations
hmc <- mtcars %>% 
  select(mpg, cyl, disp) %>% 
  filter(mpg > 20) %>% 
  arrange(mpg) %>% 
  put() # sends pipeline result to log

# Close log
log_close()

# View results
writeLines(readLines(lf))

Autolog Log

Here is the log from the above example:

========================================================================= 
Log Path: C:/Users/User/AppData/Local/Temp/RtmpYDx4m4/log/test.log 
Working Directory: C:/packages/logr 
User Name: User 
R Version: 4.0.3 (2020-10-10) 
Machine: DESKTOP-1F27OR8 x86-64 
Operating System: Windows 10 x64 build 18363 
Log Start Time: 2021-01-02 12:45:06 
========================================================================= 

========================================================================= 
Example of autolog feature
========================================================================= 

High Mileage Cars Subset 

select: dropped 8 variables (hp, drat, wt, qsec, vs, …)

filter: removed 18 rows (56%), 14 rows remaining

                mpg cyl  disp
Mazda RX4      21.0   6 160.0
Mazda RX4 Wag  21.0   6 160.0
Hornet 4 Drive 21.4   6 258.0
Volvo 142E     21.4   4 121.0
Toyota Corona  21.5   4 120.1
Datsun 710     22.8   4 108.0
Merc 230       22.8   4 140.8
Merc 240D      24.4   4 146.7
Porsche 914-2  26.0   4 120.3
Fiat X1-9      27.3   4  79.0
Honda Civic    30.4   4  75.7
Lotus Europa   30.4   4  95.1
Fiat 128       32.4   4  78.7
Toyota Corolla 33.9   4  71.1

========================================================================= 
Log End Time: 2021-01-02 12:45:10 
Log Elapsed Time: 0 00:00:03 
========================================================================= 

Manual Integration

If you do not want to use autolog, or are using a version of logr prior to v1.2.0, you can still integrate with tidylog using a manual integration method.

To integrate logr with tidylog manually, first install and load the tidylog package. To reduce the number of warning messages, add the warn.conflicts parameter to the library function, as follows:

library("tidylog", warn.conflicts = FALSE)

Then assign the tidylog display setting to log_print, like this:

options("tidylog.display" = list(log_print))

This setting will cause all tidylog messages to be written to the logr log. You will not need to call log_print or put for tidylog messages. Note that you still must open and close the logr log, as per normal operation.

To detach logr from tidylog, set the display option to NULL:

options("tidylog.display" = NULL)

Manual Integration Example

Here is a small example showing logr and tidylog integration:

library(logr)
library(dplyr)
library(magrittr)
library(tidylog, warn.conflicts = FALSE)

# Connect tidylog to logr
options("tidylog.display" = list(log_print),
        "logr.notes" = FALSE)
 
# Create temp file location
tmp <- file.path(tempdir(), "test.log")

# Open log
lf <- log_open(tmp)

# Print log header
sep("Example of tidylog integration")

# Send message to log
put("High Mileage Cars Subset")

# Perform dplyr operations
hmc <- mtcars %>% 
  select(mpg, cyl, disp) %>% 
  filter(mpg > 20) %>% 
  arrange(mpg) %>% 
  put() # sends pipeline result to log

# Close log
log_close()

# View results
writeLines(readLines(lf))

Log

Here is the log from the above example:

========================================================================= 
Log Path: C:/Users/User/AppData/Local/Temp/RtmpioAPbg/log/test.log 
Working Directory: C:/packages/Testing 
User Name: User 
R Version: 4.0.3 (2020-10-10) 
Machine: DESKTOP-1F27OR8 x86-64 
Operating System: Windows 10 x64 build 18363 
Log Start Time: 2020-12-21 13:29:03 
========================================================================= 

========================================================================= 
Example of tidylog integration 
========================================================================= 

High Mileage Cars Subset 

select: dropped 8 variables (hp, drat, wt, qsec, vs, …)

filter: removed 18 rows (56%), 14 rows remaining

                mpg cyl  disp
Mazda RX4      21.0   6 160.0
Mazda RX4 Wag  21.0   6 160.0
Hornet 4 Drive 21.4   6 258.0
Volvo 142E     21.4   4 121.0
Toyota Corona  21.5   4 120.1
Datsun 710     22.8   4 108.0
Merc 230       22.8   4 140.8
Merc 240D      24.4   4 146.7
Porsche 914-2  26.0   4 120.3
Fiat X1-9      27.3   4  79.0
Honda Civic    30.4   4  75.7
Lotus Europa   30.4   4  95.1
Fiat 128       32.4   4  78.7
Toyota Corolla 33.9   4  71.1

========================================================================= 
Log End Time: 2020-12-21 13:29:03 
Log Elapsed Time: 0 00:00:00 
=========================================================================