-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogr-tidylog.Rmd
237 lines (177 loc) · 6.35 KB
/
logr-tidylog.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
---
title: "tidylog Integration"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{tidylog Integration}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
The **[tidylog](https://CRAN.R-project.org/package=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:
```{r eval=FALSE, echo=TRUE}
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
Base Packages: stats graphics grDevices utils datasets methods base
Other Packages: logr_1.2.7 dplyr_1.0.7
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:
```{r eval=FALSE, echo=TRUE}
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
=========================================================================
```
Next: [Package Integration](logr-integration.html)