-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRNotebooks.Rmd
525 lines (374 loc) · 11.4 KB
/
RNotebooks.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
---
title: "R Notebook"
author: "Derek Slone-Zhen"
date: "Wednesday, 12th April, 2017"
output:
github_document: default
html_document:
df_print: kable
html_notebook: default
ioslides_presentation: default
slidy_presentation: default
word_document:
reference_docx: Template.docx
always_allow_html: yes
---
# Setup
## Favourite Libraries
We'll load up some of my standard R packages for later use.
```{r}
library (pacman)
p_load (magrittr)
p_load (ggplot2)
p_load (data.table)
```
## Language Engines for knitr
```{r}
is.windows <- .Platform$OS.type == "windows"
has.postgres <- !is.windows
if (is.windows) {
knitr::opts_chunk$set(engine.path = list(
bash = 'C:/Users/Derek Slone-Zhen/.babun/cygwin/bin/bash.exe',
perl = "C:/Strawberry/perl/bin/perl.exe"
))
}
```
## And a windows cmd processor
```{r echo=FALSE}
source('win_cmd.R')
```
# Welcome to an RNotebooks
RNotebooks allow the use of multiple, interwoven languages.
We'll demonstrate the getting, ingestion, and analysis of a Fuel data set.
## Fetch 'n' Sniff
Fetch : I can do this in `R`, but the command prompt is my home. Less friction for me here.
```{bash}
wget -c 'https://data.nsw.gov.au/data/dataset/a97a46fc-2bdd-4b90-ac7f-0cb1e8d7ac3b/resource/5ad2ad7d-ccb9-4bc3-819b-131852925ede/download/Service-Station-and-Price-History-March-2017.xlsx'
```
I'll take a quick look at the file, sometimes it's really a CSV file with an Excel extension.
```{bash}
hexdump -C Service-Station-and-Price-History-March-2017.xlsx | head -n20
```
OK, looks like a real Excel file. The `PK` at the beginning is the give-away of a zipped file, which is what Excels
newer file formats are. (Zipped XML files + some othe assets.)
## `readxl`
No external dependancies with this library, and installes with C / C++ native libraries for reading both
old and new Excel file formats. Thanks [Hadley](http://hadley.nz/)!
```{r}
p_load(readxl)
DATA <- read_excel("Service-Station-and-Price-History-March-2017.xlsx")
p_load(data.table)
DATA <- data.table(DATA)
```
and take a peek:
```{r}
DATA[1:(if (interactive()) 1000 else 10),]
```
## Sniffing Deeply
Not the most friendly. Lets try some extra packages:
```{r eval=interactive()}
# Only in the RNotebook
p_load(DT)
datatable(DATA[Suburb %in% c('Chatswood', 'Lane Cove', 'Artarmon', 'Lane Cove West')], filter="top")
```
## Summarising Data
```{r}
summary(DATA)
```
That's a lot of charaters that we're not getting summaries on. Lets convert all characters to factors, and the postcodes too.
```{r}
for (j in which(sapply(DATA,is.character))) {
set(DATA, j=j, value=factor(DATA[[j]], ordered = FALSE))
}
# Ask me why...
DATA <- DATA[,Postcode := factor(as.character(Postcode), ordered = FALSE)]
```
and try again:
```{r}
summary(DATA, maxsum = 8)
```
Lets focus in on our top four fuels.
```{r}
DATA[,.N,by=FuelCode][order(-N)] %>%
head(n=4) ->
top4
DATA4 <- DATA[FuelCode %in% top4$FuelCode]
```
## Visualising Data
```{r, fig.height=6, fig.width=10}
p_load(ggplot2)
ggplot(data=DATA4) +
scale_y_continuous(limits=c(75,200)) +
geom_violin(aes(y=Price, x=Brand)) +
facet_grid(FuelCode ~ ., scales='free_y') +
theme(axis.text.x = element_text(angle = 20, hjust = 1))
```
```{r, fig.height=6, fig.width=12}
g <- ggplot(data=DATA4[FuelCode == "U91"]) +
geom_point(aes(y=Price, x=PriceUpdatedDate, colour=Brand), alpha=0.6, position='jitter') +
scale_y_continuous(limits = c(75,175))
g
```
But what are those _really_ cheap petrol prices...
Let's get a more interactive visualisation.
```{r, fig.height=10, fig.width=18}
p_load(plotly)
g <- ggplot(data=DATA4[FuelCode == "U91"]) +
geom_violin(aes(y=Price, x=Brand), colour="red", fill='red', alpha=0.25) +
geom_boxplot(aes(y=Price, x=Brand), fill='transparent') +
scale_y_continuous(limits = c(75,175)) +
theme(axis.text.x = element_text(angle = 20, hjust = 1))
```
```{r, fig.height=10, fig.width=18}
print(g)
```
```{r, fig.height=10, fig.width=18, eval=interactive()}
# Only in the RNotebook
ggplotly(g)
```
# Copying Data To SQL Server
## Save as CSV (or better!)
```{r}
write.csv(DATA, 'Service-Station-and-Price-History-March-2017.csv', row.names = FALSE)
```
A couple of quick file tests - do I have a nice CSV I can upload?
Short of writing significant chunks of code, `BCP` is the only way to upload data quickly into
SQL Server, and it's _very_ picky over its file formats;
* doesn't tollerate quotes very well
* can tollearate 'embeded' field separators (i.e. the quotes don't help)
* can't tollerate embedded row separators (i.e. a new line within a quoted string)
```{bash}
< Service-Station-and-Price-History-March-2017.csv \
tr -d -c ',\n' |
awk -e '1 {print length($0)}' |
sort |
uniq -c |
sort -r -n
```
```{r}
ncol(DATA)
```
```{bash}
awk -F, -e 'NF != 9 {print}' Service-Station-and-Price-History-March-2017.csv | head
```
Blah! Commas in the addresses (and quotes that BCP won't like either).
Re-export using [ASCII Delimiters](https://ronaldduncan.wordpress.com/2009/10/31/text-file-formats-ascii-delimited-text-not-csv-or-tab-delimited-text/)
0x1F (Unit Separator) and 0x1E (Record Separator), and supress the quotes.
```{r}
write.table(
DATA,
'Service-Station-and-Price-History-March-2017.1F1E',
row.names = FALSE,
quote = FALSE,
sep = "\x1F",
eol = "\x1E")
```
And re-test:
```{bash}
< Service-Station-and-Price-History-March-2017.1F1E \
tr -d -c $'\x1E\x1F' |
tr $'\x1E' '\n' |
awk -e '1 {print length($0)}' |
sort |
uniq -c
```
## Upload the 1E1F
```{r echo=FALSE}
# For non-windows, we won't to the sql parts
db <- NULL
```
And load up the odbc driver and connection to local Microsoft SQL Server Database.
```{r eval=is.windows}
p_load(DBI)
p_load(odbc)
# drv <- dbDriver("ODBC")
con_template <- 'driver={SQL Server Native Client 11.0};Server=%s;Database=%s;Trusted_Connection=yes'
# db <- dbConnect(drv, connection = sprintf(con_template, server=".", database= "test"))
drv <- odbc::odbc()
db <- DBI::dbConnect(drv,.connection_string = sprintf(con_template, server=".", database= "test"))
```
```{r eval=has.postgres}
p_load(DBI)
p_load(RPostgreSQL)
drv <- PostgreSQL()
db <- dbConnect(drv, dbname="test")
```
Check that the DB is good
```{r, eval=has.postgres, echo=has.postgres}
isPostgresqlIdCurrent(db)
```
Drop the table if it already exists
```{sql connection=db}
DROP TABLE IF EXISTS "Service-Station-and-Price-History-March-2017";
-- And return a result set to keep the RNotebook happy
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Service-Station-and-Price-History-March-2017';
```
Use R to sketch out the body of an SQL `CREATE TABLE`.
```{r}
sprintf("%-20s %s not null,",
colnames(DATA),
DATA %>%
lapply(class) %>%
sapply(head,1) %>%
sapply(switch,
character = 'varchar(255)',
POSIXct = 'datetime2(0)',
numeric = 'numeric(4,1)')
) %>%
paste0(collapse="\n") %>%
cat
```
```{sql connection=db, eval=is.windows}
CREATE TABLE "Service-Station-and-Price-History-March-2017"
(
ServiceStationName varchar(255) not null,
Address varchar(255) not null,
Suburb varchar(255) not null,
Postcode char(4) not null,
Brand varchar(255) not null,
FuelCode char(3) not null,
PriceUpdatedDate datetime2(0) not null,
Price numeric(4,1) not null
);
-- And return a result set to keep the RNotebook happy
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Service-Station-and-Price-History-March-2017';
```
```{sql connection=db, eval=has.postgres}
CREATE TABLE "Service-Station-and-Price-History-March-2017"
(
ServiceStationName varchar(255) not null,
Address varchar(255) not null,
Suburb varchar(255) not null,
Postcode char(4) not null,
Brand varchar(255) not null,
FuelCode char(3) not null,
PriceUpdatedDate timestamp not null,
Price numeric(4,1) not null
);
-- And return a result set to keep the RNotebook happy
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Service-Station-and-Price-History-March-2017';
```
### Upload for SQL Server
I can never remember the syntax for `bcp` fully, so lets get a copy here for reference.
```{bash, error=TRUE, eval=is.windows}
bcp
```
Now I can craft the `bcp` for upload.
```{bash, eval=is.windows}
bcp \
"dbo.[Service-Station-and-Price-History-March-2017]" \
in \
'Service-Station-and-Price-History-March-2017.1F1E' \
-T -S . -d test \
-c -t $'\x1F' -r $'\x1E' -C UTF-8 \
-F 2 \
-h TABLOCK -b 100000 \
-e errors
```
Print out (the start of) any errors
```{bash, eval=is.windows}
head errors
```
### Upload for Postgres
```{bash eval=has.postgres}
< 'Service-Station-and-Price-History-March-2017.1F1E' \
bbe -b ':/\x1E/' -e 'D 1;s/\\/\\\\/;s/\r/\\r/;s/\n/\\n/;s/\x1E/\n/' |
psql test -c "COPY \"Service-Station-and-Price-History-March-2017\"
FROM STDIN WITH DELIMITER AS E'\x1F'"
```
# Querying from Database
Now we can query from the database
```{r}
fuel <- 'U91'
```
```{sql connection=db, output.var='DBDATA'}
SELECT ServiceStationName, Suburb, Brand, PriceUpdatedDate, Price
FROM "Service-Station-and-Price-History-March-2017"
WHERE FuelCode = ?fuel
ORDER BY Price ASC
```
```{r}
DBDATA <- if (is.windows || has.postgres) data.table(DBDATA) else DATA
DBDATA[1:10]
```
# Save data and read it back in many Languages
```{r}
p_load(feather)
write_feather(DBDATA,"Service-Station-and-Price-History-March-2017.feather")
Sys.setenv(file_in="Service-Station-and-Price-History-March-2017")
```
```{python, error=is.windows}
import os
import pandas
import feather
file_in = os.environ["file_in"] + ".feather"
df = feather.read_dataframe(file_in)
print(df.head(10))
```
```{python}
import os
import pandas as pd
file_in = os.environ["file_in"] + ".csv"
df = pd.read_csv(file_in)
print(df.head(10))
```
```{perl}
use Parse::CSV;
use Data::Dumper;
my $objects = Parse::CSV->new(
file => $ENV{file_in} . '.csv',
names => 1,
);
my $max_rows = 3;
while ( my $row = $objects->fetch ) {
print Dumper($row);
if (--$max_rows <= 0) { last; }
}
```
```{ruby}
require 'csv'
require 'pp'
file_in = ENV["file_in"] + ".csv"
customers = CSV.read(file_in)
pp(customers[1..3])
```
# Sillyness digression - what else can we do here?
## LaTeX fragments!
$$
x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
$$
Which, of course, also means we can use set algebra notation:
$$
Query = \{ \forall p \in [\text{Service-Station-and-Price-History-March-2017}] | p_{FuelCode} = \text{U91} \}
$$
# Tidy up after ourselves
```{r TakeDown}
if (!interactive()) {
invisible({
dbDisconnect(db)
dbUnloadDriver(drv)
})
}
```
# Sneaky Stuff
## I've a local bash script
The RNotebook mechanisms use a different strategy for executing code blocks (at lease bash one):
namely that they write the text of the block to a temp file and then invoke the file along as:
`bash` _`file_name`_
Whereas the `knitr` engine invokes bash as `bash -c ` _`code_block`_.
```{r code=readLines('bash.bat'), eval=FALSE}
```
# Bulid Info & Version Control
## sessionInfo
```{r}
sessionInfo()
```
## Version Control
This code ensure that when we `knit` the document, all changes get committed to
`git` and the SHA1 checksum of that commit is embedded in the document for
reproducability.
```{bash}
git add -A .
git commit -m "Knitting..."
git rev-parse HEAD
```