-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUseR2015.Rmd
269 lines (226 loc) · 9.07 KB
/
UseR2015.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
---
title: "UseR2015"
author: "Mario Santoro"
date: "06 luglio 2015"
output: ioslides_presentation
---
## Rcpp http://www.rcpp.org
Codice Performante in C++ per R
foo.cpp
```
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector timesTwo(NumericVector x) {
return x * 2;
}
```
## sourceCpp()
```{r,eval=TRUE}
library(Rcpp)
sourceCpp("foo.cpp")
timesTwo(2)
```
## Altre release specifiche di Rcpp
[RcppArmadillo](http://dirk.eddelbuettel.com/code/rcpp.armadillo.html)
Uso di Rcpp con la libreria di linear algebra in C++ [Armadillo](http://arma.sf.net/)
```
#include <RcppArmadillo.h>
```
[RcppGSL](http://dirk.eddelbuettel.com/code/rcpp.gsl.html) interfaccia a [GNU GSL](http://www.gnu.org/software/gsl/)
```
#include <RcppGSL.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_blas.h>
```
[RcppSMC](http://dirk.eddelbuettel.com/code/rcpp.smc.html) implementa Rcpp per Sequential Monte Carlo and Particle Filters [(SMC)](http://www.jstatsoft.org/v30/i06)
## Rcpp [Parallel](https://github.com/RcppCore/RcppParallel)
```
#include <RcppParallel.h>
```
```
#include <Rcpp.h>
using namespace Rcpp;
#include <algorithm>
// [[Rcpp::export]]
double vectorSum(NumericVector x) {
return std::accumulate(x.begin(), x.end(), 0.0);
}
```
## Rcpp [Parallel](https://github.com/RcppCore/RcppParallel)
```
// [[Rcpp::depends(RcppParallel)]]
#include <RcppParallel.h>
#include <Rcpp.h>
using namespace RcppParallel;
struct Sum : public Worker
{
// source vector
const RVector<double> input;
// accumulated value
double value;
// constructors
Sum(const Rcpp::NumericVector input) : input(input), value(0) {}
Sum(const Sum& sum, Split) : input(sum.input), value(0) {}
// accumulate just the element of the range I've been asked to
void operator()(std::size_t begin, std::size_t end) {
value += std::accumulate(input.begin() + begin, input.begin() + end, 0.0);
}
// join my value with that of another Sum
void join(const Sum& rhs) {
value += rhs.value;
}
};
```
## Rcpp [Parallel](https://github.com/RcppCore/RcppParallel)
```
using namespace RcppParallel;
// [[Rcpp::export]]
double parallelVectorSum(Rcpp::NumericVector x) {
// declare the SumBody instance
Sum sum(x);
// call parallel_reduce to start the work
parallelReduce(0, x.length(), sum);
// return the computed sum
return sum.value;
}
```
## Rcpp [Parallel](https://github.com/RcppCore/RcppParallel)
```{r,eval=TRUE}
library(RcppParallel)
sourceCpp("foo_ser.cpp")
sourceCpp("foo_par.cpp")
library(rbenchmark)
v <- as.numeric(c(1:10000000))
pp <- benchmark(vectorSum(v),parallelVectorSum(v),order="relative")
pp
```
## RInside [rinside.html](http://dirk.eddelbuettel.com/code/rinside.html)
R embedded in classi C++
```
#include <RInside.h>
```
Pacchetto giovane, alle versione 0.2, ma promettente ...
```
#include <RInside.h> // for the embedded R via RInside
int main(int argc, char *argv[]) {
RInside R(argc, argv); // create an embedded R instance
R["txt"] = "Hello, world!\n"; // assign a char* (string) to 'txt'
R.parseEvalQ("cat(txt)"); // eval the init string, ignoring any returns
exit(0);
}
```
## [HTMLWIDGETS](http://www.htmlwidgets.org)
Widgets interattivi costruiti con R e javascript
Già ci sono alcuni paccheti di R che implementano librerie js
esempi con questo dataset
```{r,echo=FALSE}
library(dplyr,quietly = T,warn.conflicts = F)
library(gplots,quietly = T,warn.conflicts = F)
library(zoo,quietly = T,warn.conflicts = F)
library(xts,quietly = T,warn.conflicts = F)
library(reshape2,quietly = T,warn.conflicts = F)
# Slurp up ./data/*.csv into one data frame
downloads <- do.call(rbind, lapply(list.files("./ggbrush/data", full.names = TRUE), read.csv, stringsAsFactors = FALSE))
# Round time to nearest hour
downloads <- downloads %>% mutate(hour = sub(":.*", ":00", time)) %>% tbl_df()
# Counts per date/hour by country
counts_by_country <- downloads %>%
count(date, hour, country) %>%
arrange(date, hour, country)
#counts_by_country
# Counts per date/hour, all countries combined
counts <- counts_by_country %>%
group_by(date, hour) %>%
summarise(n = sum(n))
#counts
# Countries with at least 300 downloads
major_countries <- (downloads %>% count(country) %>% filter(n > 300))$country
# Make a matrix of countries vs. hours
m_hours_countries <- counts_by_country %>%
filter(country %in% major_countries) %>%
group_by(hour, country) %>%
summarise(n = sum(n)) %>%
arrange(hour, country) %>%
acast(hour ~ country, value.var = 'n', fill = 0)
```
Scaricamenti orari di pacchetti R per nazione
```{r,}
m_hours_countries[1:4,1:10]
```
## [dygrhaps](http://dygraphs.com)
Libreria per serie temporali
```{r,echo=FALSE}
library(dygraphs)
# Turn into time series
times <- paste0(counts$date, " ", counts$hour, ":00 UTC") %>% as.POSIXct()
ts_times <- xts(counts$n, order.by = times, frequency = 24)
#Plot
dg <- dygraph(ts_times) %>%
dyAxis("y", "Downloads") %>%
dyAxis("x", "Date")
dg %<>% dyRangeSelector() %>% dyAnnotation(as.POSIXct("2015-05-17 19:00"), "A") %>%
dyShading(as.POSIXct("2015-05-17 05:00"),
as.POSIXct("2015-05-18 09:00")) %>%
dyRoller()
dg
```
## [d3hetamap](https://github.com/rstudio/d3heatmap)
Heatmap usando D3
```{r,fig.height=3}
library(d3heatmap)
m_counts <- t(acast(counts, date ~ hour, value.var = 'n'))
d3heatmap(scale(m_hours_countries), cellnote = m_hours_countries,
colors = "Blues", dendrogram = "col")
```
## [leaflet](http://leafletjs.com)
Mappe interattive
```{r,eval=F}
library(leaflet)
leaflet(quakes) %>%
addTiles("https://api.tiles.mapbox.com/v4/mapbox.wheatpaste/{z}/{x}/{y}.png?access_token=pk.eyJ1Ijoic2FudG9yb21hIiwiYSI6ImUxN2ZkZWRkNTU2NjVlZjRiM2E2ZmZhNGQyODM0OGFhIn0.mM32XaR8Ro0dBQIlA6G2iA") %>%
addCircles(color = "#CC0000", weight = 2, radius = ~10^mag / 5,
popup = ~as.character(stations))
```
## [leaflet](http://leafletjs.com)
Mappe interattive
```{r,echo=F}
library(leaflet)
leaflet(quakes) %>%
addTiles("https://api.tiles.mapbox.com/v4/mapbox.wheatpaste/{z}/{x}/{y}.png?access_token=pk.eyJ1Ijoic2FudG9yb21hIiwiYSI6ImUxN2ZkZWRkNTU2NjVlZjRiM2E2ZmZhNGQyODM0OGFhIn0.mM32XaR8Ro0dBQIlA6G2iA") %>%
addCircles(color = "#CC0000", weight = 2, radius = ~10^mag / 5,
popup = ~as.character(stations))
```
## [threejs globe](http://threejs.org)
Libreria 3D in javascript
```{r,}
library(threejs)
globejs(lat = quakes$lat, long = quakes$long,
value = 10^quakes$mag / 10^4,atmosphere = TRUE)
```
## [epiwidgets](https://github.com/sdwfrost/epiwidgets)
Tree Viewer in js D3
```{r,eval=F}
# devtools::install_github("sdwfrost/epiwidgets")
library(epiwidgets)
nwk <- "(((EELA:0.150276,CONGERA:0.213019):0.230956,(EELB:0.263487,CONGERB:0.202633):0.246917):0.094785,((CAVEFISH:0.451027,(GOLDFISH:0.340495,ZEBRAFISH:0.390163):0.220565):0.067778,((((((NSAM:0.008113,NARG:0.014065):0.052991,SPUN:0.061003,(SMIC:0.027806,SDIA:0.015298,SXAN:0.046873):0.046977):0.009822,(NAUR:0.081298,(SSPI:0.023876,STIE:0.013652):0.058179):0.091775):0.073346,(MVIO:0.012271,MBER:0.039798):0.178835):0.147992,((BFNKILLIFISH:0.317455,(ONIL:0.029217,XCAU:0.084388):0.201166):0.055908,THORNYHEAD:0.252481):0.061905):0.157214,LAMPFISH:0.717196,((SCABBARDA:0.189684,SCABBARDB:0.362015):0.282263,((VIPERFISH:0.318217,BLACKDRAGON:0.109912):0.123642,LOOSEJAW:0.397100):0.287152):0.140663):0.206729):0.222485,(COELACANTH:0.558103,((CLAWEDFROG:0.441842,SALAMANDER:0.299607):0.135307,((CHAMELEON:0.771665,((PIGEON:0.150909,CHICKEN:0.172733):0.082163,ZEBRAFINCH:0.099172):0.272338):0.014055,((BOVINE:0.167569,DOLPHIN:0.157450):0.104783,ELEPHANT:0.166557):0.367205):0.050892):0.114731):0.295021)"
treewidget(nwk)
```
## [epiwidgets](https://github.com/sdwfrost/epiwidgets)
Tree Viewer in js D3
```{r,echo=F}
# devtools::install_github("sdwfrost/epiwidgets")
library(epiwidgets,quietly = T,warn.conflicts = F)
nwk <- "(((EELA:0.150276,CONGERA:0.213019):0.230956,(EELB:0.263487,CONGERB:0.202633):0.246917):0.094785,((CAVEFISH:0.451027,(GOLDFISH:0.340495,ZEBRAFISH:0.390163):0.220565):0.067778,((((((NSAM:0.008113,NARG:0.014065):0.052991,SPUN:0.061003,(SMIC:0.027806,SDIA:0.015298,SXAN:0.046873):0.046977):0.009822,(NAUR:0.081298,(SSPI:0.023876,STIE:0.013652):0.058179):0.091775):0.073346,(MVIO:0.012271,MBER:0.039798):0.178835):0.147992,((BFNKILLIFISH:0.317455,(ONIL:0.029217,XCAU:0.084388):0.201166):0.055908,THORNYHEAD:0.252481):0.061905):0.157214,LAMPFISH:0.717196,((SCABBARDA:0.189684,SCABBARDB:0.362015):0.282263,((VIPERFISH:0.318217,BLACKDRAGON:0.109912):0.123642,LOOSEJAW:0.397100):0.287152):0.140663):0.206729):0.222485,(COELACANTH:0.558103,((CLAWEDFROG:0.441842,SALAMANDER:0.299607):0.135307,((CHAMELEON:0.771665,((PIGEON:0.150909,CHICKEN:0.172733):0.082163,ZEBRAFINCH:0.099172):0.272338):0.014055,((BOVINE:0.167569,DOLPHIN:0.157450):0.104783,ELEPHANT:0.166557):0.367205):0.050892):0.114731):0.295021)"
treewidget(nwk)
```
## Thematic Maps [tmap](https://github.com/mtennekes/tmap)
```{r}
library(tmap,quietly = T,warn.conflicts = F)
data(World)
data(metro)
qtm(World, fill="pop_est_dens", theme="World", fill.style="kmeans",
fill.title="Population per km^2") +
qtm(metro, bubble.size = "pop2010", bubble.col ="purple",
bubble.title.size="Metropolitan Areas", theme = "World", bubble.scale=.5)
```