Sie sind auf Seite 1von 12

Modules in R

Sebastian Warnholz INWT Statistics GmbH

July 7th , 2017 UseR! Brussels

S. Warnholz INWT Statistics GmbH Modules in R 1/ 10


What we do!

INWT Statistics GmbH


INWT Statistics specializes in intelligent data analysis and delivers solutions in the elds of online
marketing, CRM, data management and business intelligence/reporting.

Online Marketing/ CRM/ BI/Reporting/

Customer Journey Analysis Customer Lifetime Value Data Management


Conversion Optimization Customer Segmentation Data Consolidation
Fraud Detection Churn Management Dashboards

Training/

Selected Customers:

S. Warnholz INWT Statistics GmbH Modules in R 2/ 10


Motivation

Managing dependencies between several R scripts

S. Warnholz INWT Statistics GmbH Modules in R 3/ 10


Motivation

Managing dependencies between several R scripts

Options for organising functions in larger code bases

S. Warnholz INWT Statistics GmbH Modules in R 3/ 10


What are modules ?

Script/ Module/ Package/

+ Interactive data analysis + Local namespace + Documentation


+ Everything ad-hoc + Import + export + Dependencies
- Managing dependencies + Bundle related functions + Namespace (imports and
and names - No documentation exports)
- Organise helper functions Delivery inside packages + Share / deliver code
- One global scope

S. Warnholz INWT Statistics GmbH Modules in R 4/ 10


Code example I

One-le setup
stats <- module({
import("stats", "median")
myMedian <- function(x) median(x, TRUE)
})
stats$myMedian(rnorm(10))

S. Warnholz INWT Statistics GmbH Modules in R 5/ 10


Code example I

One-le setup Multiple-le setup


stats <- module({ helperStats.R
import("stats", "median")
import("stats", "median")
myMedian <- function(x) median(x, TRUE)
myMedian <- function(x) ...
})
stats$myMedian(rnorm(10)) analysis.R
stats <-
modules::use("helperStats.R")
stats$myMedian(rnorm(10))

S. Warnholz INWT Statistics GmbH Modules in R 5/ 10


Code example II

Dependencies in modules: Local search path:

stats <- module({


import("stats", "median")
export("myMedian")
myMedian <- function(x) median(x, TRUE)
})

import: state dependencies explicitly


export: state what clients can use
search path: inside packages, the root is the packages
namespace

S. Warnholz INWT Statistics GmbH Modules in R 6/ 10


Code example III

Nested module denition: Local search path:

mathStuff <- module({


add <- function(x, y) x + y
subtract <- function(x, y) x - y
usefulAbstractions <- module({
add1 <- function(x) add(x, 1)
})
})

mathStuff$add(1, 2)
mathStuff$usefulAbstractions$add1(2)

S. Warnholz INWT Statistics GmbH Modules in R 7/ 10


What are modules?

An abstraction between functions and packages . . .

Abstractions/ Characteristics of modules/

1. Data and functions List of functions (immutable)


2. Modules and object orientation First class citizen
3. Packages Dene imports and exports
4. Repositories Module composition (easy re-export)

S. Warnholz INWT Statistics GmbH Modules in R 8/ 10


When to use modules

Use cases/ Alternatives/


In larger code bases Packages for Object Orientation (methods,
to bundle related functions proto, R.oo, R6)
as units of abstraction
not as a substitute for packages The package import as substitute for
library
Analysis distributed across several scripts
funA from scriptA has its own and local
set of dependencies Try it . . . /
using import has no side effects in the
global environment install.packages("modules")
vignette("modulesInR")
Substitute for library github.com/wahani/modules

S. Warnholz INWT Statistics GmbH Modules in R 9/ 10


Thank You for Your Attention!

INWT Statistics GmbH


E-Mail: info@inwt-statistics.de
Internet: www.inwt-statistics.de
Tel.: +49 30 609857990

S. Warnholz INWT Statistics GmbH Modules in R 10/ 10

Das könnte Ihnen auch gefallen