-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest.Rmd
217 lines (163 loc) · 10.9 KB
/
test.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
---
title: "Test"
author: "Ryo®, Eng Lian Hu"
date: "October 25, 2015"
output:
html_document:
fig_caption: yes
highlight: haddock
theme: cerulean
toc: yes
word_document:
fig_caption: yes
pdf_document:
fig_caption: yes
toc: yes
---
Testing output of the coding.
```{r load-packages, include=FALSE}
## Setup Options, Loading Required Libraries and Preparing Environment
## Setup `knitr` options and loading the required libraries.
## Setting to omit all warnings
options(warn=-1)
## Loading the packages
if(!suppressPackageStartupMessages(require('devtools'))){
install.packages('devtools')}
if(!suppressPackageStartupMessages(require('BBmisc'))){
install.packages('BBmisc')}
if(!suppressPackageStartupMessages(require('BiocParallel'))){
devtools::install_github('Bioconductor/BiocParallel')}
suppressPackageStartupMessages(library('BBmisc'))
pkgs <- c('devtools','zoo','chron','stringr','stringi','reshape','reshape2','tbl_df','sparkline','data.table','DT','plyr','dplyr','magrittr','foreach','manipulate','ggplot2','ggthemes','proto','extrafont','directlabels','PerformanceAnalytics','plotly','doParallel','rvest','highlightHTML','knitr','rmarkdown','scales','lubridate','tidyr','whisker','gtable','grid','gridExtra','pander','arules','arulesViz','googleVis','rlist')
#'@ c('memoise','RStudioAMI','parallel','BiocParallel','RSelenium','doMC','editR') #load if needed
suppressAll(lib(pkgs)); rm(pkgs)
```
```{r setting}
## Creating a parallel computing Cluster and support functions.
## Preparing the parallel cluster using the cores
doParallel::registerDoParallel(cores = 16)
# Set the googleVis options first to change the behaviour of plot.gvis, so that only the chart component of the HTML file is written into the output file.
op <- options(gvis.plot.tag='chart')
```
```{r read-data-summary-table, results='asis'}
## Read the datasets
## Refer to **Testing efficiency of coding.Rmd** at chunk `get-data-summary-table-2.1`
source(paste0(getwd(),'/function/readfirmDatasets.R'))
source(paste0(getwd(),'/function/arrfirmDatasets.R'))
years <- 2011:2014
lProfile <- c(AH=0.10,OU=0.12)
mbase <- readfirmDatasets(years=years) %>% arrfirmDatasets(., lProfile=lProfile)
r <- range(mbase$datasets$Stakes)
```
```{r, echo=FALSE}
bar_string <- "type: 'bar', barColor: 'orange', negBarColor: 'purple', highlightColor: 'black'"
cb_bar = JS(paste0("function (oSettings, json) { $('.spark:not(:has(canvas))').sparkline('html', { ", bar_string, " }); }"), collapse = "")
line_string <- "type: 'line', lineColor: 'black', fillColor: '#ccc', highlightLineColor: 'orange', highlightSpotColor: 'orange'"
cb_line <- JS(paste0("function (oSettings, json) { $('.spark:not(:has(canvas))').sparkline('html', { ", line_string, ", chartRangeMin: ", r[1], ", chartRangeMax: ", r[2], " }); }"), collapse = "")
box_string <- "type: 'box', lineColor: 'black', whiskerColor: 'black', outlierFillColor: 'black', outlierLineColor: 'black', medianColor: 'black', boxFillColor: 'orange', boxLineColor: 'black'"
cb_box1 = JS(paste0("function (oSettings, json) { $('.spark:not(:has(canvas))').sparkline('html', { ", box_string, " }); }"), collapse = "")
cb_box2 = JS(paste0("function (oSettings, json) { $('.spark:not(:has(canvas))').sparkline('html', { ", box_string, ", chartRangeMin: ", r[1], ", chartRangeMax: ", r[2], " }); }"), collapse = "")
```
Please refer to [Natural Language Analysis](http://rpubs.com/englianhu/natural-language-analysis) to see the firm A staking sample dataset.
```{r data-month-summary-plots, results='asis'}
## Before we start analyse the staking model, we are firstly see the monthly Stakes and Profit & Lose of the Agency A
## the stakes amount display as $1 = $10,000
dat <- mbase$datasets %>% mutate(Month=paste(month(Date,label=TRUE),year(Date))) %>% tbl_df
#'@ dat <- mbase$datasets %>% mutate(Month=month(ymd(Date), label=TRUE)) %>% tbl_df
m <- ddply(dat, .(Sess, Month), summarise, Stakes=sum(Stakes)/10000, PL=sum(PL)/10000) %>% tbl_df
#'@ melted <- melt(m, id.vars=c('Sess','Month')) %>% tbl_df
## http://help.plot.ly/make-a-3D-surface-plot
## Will doing research on 3D graph some other days.
gvis.options <- list(hAxis="{title:'Month'}", vAxis="{title:'Stakes'}",width='automatic', height='automatic')
line.gvis <- gvisLineChart(xvar='Month', yvar=c('Stakes','PL'), data=m, options=gvis.options)
plot(line.gvis)
#'@ print(line.gvis, tag='chart')
```
*graph 3.1.1*
```{r}
#'@ CityPopularity$Mean=mean(CityPopularity$Popularity)
#'@ plots <- gvisComboChart(CityPopularity, xvar='City', yvar=c('Mean', 'Popularity'), options=list(seriesType='bars', width=450, height=300, title='City Popularity', series='{0: {type:"line"}}'))
#'@ plot(plots)
```
```{r}
## aaaaaaaaaaaaaaa
#'@ plots <- gvisComboChart(m, xvar='Month', yvar=c('Stakes','PL'), options=list(seriesType='bars', width=450, height=300, title='Monthly Summary', series='{0: {type:"line"}}'))
#'@ plot(plots)
#'@ print(plots, tag='chart')
```
```{r, results='asis'}
## Set options back to original options
options(op)
```
## DT data tables
Here is the full data table generated with DT. There are various feature options for data table html widgets in the browser using DT. See this introduction for details.
### DT
```{r, echo=FALSE}
dat <- mbase$datasets %>% mutate(Month=month(ymd(Date), label=TRUE)) %>% tbl_df
m <- ddply(dat, .(Sess, Month, Day), summarise, Stakes=sum(Stakes)/10000, PL=sum(PL)/10000) %>% tbl_df
m %>% select(Sess,Month,Day,Stakes) %>% datatable(.,caption="Table 3.1.1 : Annual Summary of Staking Data.",extensions=c('ColReorder','ColVis','TableTools'),options=list(dom='TC<"clear">rlfrtip',colVis=list(exclude=c(0),activate='mouseover'),tableTools=list(sSwfPath=copySWF(pdf=TRUE)),scrollX=TRUE,scrollCollapse=TRUE))
```
```{r, echo=FALSE}
## 12 columns which are counting from Jan until Dec
colDefs1 <- list(list(targets = c(1:12), render = JS("function(data, type, full){ return '<span class=spark>' + data + '</span>' }")))
## 4 columns which are counting the Soccer Session from 2011 untli 2014
colDefs2 <- list(list(targets = c(1:4), render = JS("function(data, type, full){ return '<span class=spark>' + data + '</span>' }")))
```
```{r,echo=FALSE}
dat.t <- m %>% group_by(Sess, Month) %>% summarise(Stakes = paste(Stakes, collapse = ",")) %>% tbl_df
dat.ta <- suppressAll(dcast(dat.t, Sess ~ Month)) %>% tbl_df
dat.tb <- suppressAll(dcast(dat.t, Month ~ Sess)) %>% tbl_df
```
### d1
```{r, echo=FALSE}
#'@ d1 <- dat.ta %>% datatable(.,caption="Table 3.1.1 : Annual Summary of Staking Data.",extensions=c('ColReorder','ColVis','TableTools'),options=list(dom='TC<"clear">rlfrtip',colVis=list(exclude=c(0),activate='mouseover'),tableTools=list(sSwfPath=copySWF(pdf=TRUE)),scrollX=TRUE,scrollCollapse=TRUE,columnDefs = colDefs2, fnDrawCallback = cb_bar))
#'@ d1 <- datatable(tbl_df(dat.ta), rownames = FALSE, options = list(columnDefs = colDefs1, fnDrawCallback = cb_line))
d1 <- dat.ta %>% data.table %>% datatable(., rownames=FALSE, options=list(columnDefs=colDefs1, fnDrawCallback=cb_line))
d1$dependencies <- append(d1$dependencies, htmlwidgets:::getDependency("sparkline"))
d1
```
### d2
```{r}
#'@ d2 <- dat.tb %>% datatable(.,caption="Table 3.1.1 : Annual Summary of Staking Data.",extensions=c('ColReorder','ColVis','TableTools'),options=list(dom='TC<"clear">rlfrtip',colVis=list(exclude=c(0),activate='mouseover'),tableTools=list(sSwfPath=copySWF(pdf=TRUE)),scrollX=TRUE,scrollCollapse=TRUE, options = list(columnDefs = colDefs2, fnDrawCallback = cb_line)))
#'@ d2 <- datatable(tbl_df(dat.tb), rownames = FALSE, options = list(columnDefs = colDefs2, fnDrawCallback = cb_bar))
d2 <- dat.tb %>% data.table %>% datatable(., rownames=FALSE, options=list(columnDefs=colDefs2, fnDrawCallback=cb_bar))
d2$dependencies <- append(d2$dependencies, htmlwidgets:::getDependency("sparkline"))
d2
```
Here I try to plot an inline graph `r sparkline(mbase$datasets$HKPrice, type='bar')` testing `r sparkline(mbase$datasets$netProbB)`... line `r sparkline(mbase$datasets$netProbB, type='line')`
```{r}
d3 <- datatable(data.table(dat.tb), rownames = FALSE, options = list(columnDefs = colDefs2,
fnDrawCallback = cb_box1))
d3$dependencies <- append(d3$dependencies, htmlwidgets:::getDependency("sparkline"))
d3
```
```{r}
d4 <- datatable(data.table(dat.tb), rownames = FALSE, options = list(columnDefs = colDefs2,
fnDrawCallback = cb_box2))
d4$dependencies <- append(d4$dependencies, htmlwidgets:::getDependency("sparkline"))
d4
```
```{r, echo=FALSE}
dat.t2 <- dat %>% group_by(Sess, Month, Day) %>% summarise(Mean = round(mean(Stakes), 1)/10000, SD = round(sd(Stakes), 2)/10000, Min = min(Stakes)/10000, Max = max(Stakes)/10000, Samples = paste(Stakes/10000, collapse = ",")) %>% mutate(Series = Samples) %>% tbl_df
cd <- list(list(targets=7, render=JS("function(data, type, full){ return '<span class=sparkSamples>' + data + '</span>' }")), list(targets=8, render=JS("function(data, type, full){ return '<span class=sparkSeries>' + data + '</span>' }")))
cb = JS(paste0("function (oSettings, json) {\n $('.sparkSeries:not(:has(canvas))').sparkline('html', { ", line_string, " });\n $('.sparkSamples:not(:has(canvas))').sparkline('html', { ", box_string, " });\n}"), collapse = "")
```
```{r}
d5 <- dat.t2 %>% datatable(., rownames = FALSE, options = list(columnDefs=cd, fnDrawCallback=cb))
d5$dependencies <- append(d5$dependencies, htmlwidgets:::getDependency("sparkline"))
d5
```
### res
```{r, echo=FALSE}
## Annual summary
res <- ldply(.data=split(mbase$datasets, mbase$datasets$Sess), summarise, S.total=sum(Stakes), S.median=median(Stakes), S.mean=mean(Stakes), S.sd=sd(Stakes), Count=length(PL), minHKPrcB=min(HKPrice), maxHKPrcB=max(HKPrice), minProbB=min(netProbB), maxProbB=max(netProbB), Return=sum(PL,Stakes), PL.Total=sum(PL), PL.percent=sum(PL)/sum(Stakes), S.box=as.character(paste(Stakes,collapse=','))) %>% tbl_df
##, S.graph=as.character(paste(x$Stakes,collapse=',', PL.box=paste(x$PL,collapse=','), PL.graph=paste(x$PL,collapse=','))))) %>% tbl_df
names(res)[1] <- 'Sess'
cd <- list(list(targets=12, render=JS("function(data, type, full){ return '<span class=sparkSamples>' + data + '</span>' }")), list(targets=13, render=JS("function(data, type, full){ return '<span class=sparkSeries>' + data + '</span>' }")))
cb = JS(paste0("function (oSettings, json) {\n $('.sparkSeries:not(:has(canvas))').sparkline('html', { ", line_string, " });\n $('.sparkSamples:not(:has(canvas))').sparkline('html', { ", box_string, " });\n}"), collapse = "")
```
```{r, echo=FALSE}
#'@ res %>% datatable(.,caption="Table 3.1.1 : Annual Summary of Staking Data.",extensions=c('ColReorder','ColVis','TableTools'),options=list(dom='TC<"clear">rlfrtip',colVis=list(exclude=c(0),activate='mouseover'),tableTools=list(sSwfPath=copySWF(pdf=TRUE)),scrollX=TRUE,scrollCollapse=TRUE,columnDefs=cd, fnDrawCallback=cb))
res %>% datatable(., rownames=FALSE, options=list(dom='t', scrollX=TRUE, scrollCollapse=TRUE, columnDefs=cd, fnDrawCallback=cb))
res$dependencies <- append(res$dependencies, htmlwidgets:::getDependency("sparkline"))
```