SlideShare una empresa de Scribd logo
1 de 23
Descargar para leer sin conexión
Reproducible R coding
CMEC R-Group
Martin Jung
12.02.2015
Goals of reproducible programming?
Make your code readible by you and others
Group your code and functionalize
Embrace collaboration, version control and automation
First step - readibility
1. Writing cleaner code
Writing cleaner R code | Names
Keep new filenames descriptive and meaningful
"helper-functions.R"
# or for sequences of processing work
"01_Download.R"
"02_Preprocessing.R"
#...
Use CamelCase or Snake_case for variables
"spatial_data"
"ModelFit"
"regression.results"
Avoid predetermined names like c or plot
Writing cleaner R code | Spacing
Use Spacing just as in the english language
# Good
model.fit <- lm(age ~ circumference, data = Orange)
# Bad
f1=lm(Orange$age~Orange$circumference)
Don’t be afraid of using new lines
model.results <- data.frame(Type = sample(letters, 10),
Data = NA,
SampleSize = 10 )
# Same goes for loops
# And don't forget good documentation
More on writing clean code
Google R Style Guide
Hadley Wickhams Style Guide
RopenSci Guide
And there even is a r-package to clean up your code:
formatR
Further ways to improve reproduciability
Ideally attach your code + data to publications
Open-access hoster (DataDryad, Figshare, Zenodo)
Restructuring of workflow with RMarkdown / LaTeX / HTML
Functionalize!
Many R users are tempted to write their code very specialized
and non-reusable
Number 1 rule for clear coding :
DRY - Don't repeat yourself!
Simple example: We want to fit a linear model to test if in an
orange orchard the circumference (mm) increases with age (age of
trees). If so we want to quantify and display the
Root-Mean-Square-Error (RMSE) of this fit for each individual
orange tree in the dataset (N = 5).
Normal way:
# Linear model
model.fit <- lm(age ~ circumference, data = Orange)
model.resid <- residuals( model.fit )
model.fitted <- fitted( model.fit )
rmse <- sqrt( mean( (model.resid - model.fitted)^2 ))
tapply(model.resid - model.fitted, Orange$Tree,
function(x) sqrt( mean( (x)^2 )))
3 1 5 2 4
0200400600800100012001400
Defining your functions
Essentially most r-packages are just a compilation of useful
functions that users have written.
# We want to get the RMSE of a linear model
rmse <- function(fit, groups = NULL, ...)
{
f.resid <- residuals(fit);f.fitted <- fitted(fit)
if(! is.null( groups )) {
tapply((f.resid-f.fitted), groups, function(x) sqrt(mea
} else {
sqrt(mean((f.resid-f.fitted)^2, ...))
}
}
model.fit <- lm(age ~ circumference, data = Orange)
# This function is more flexible, can be further customized
# applied in other situations
rmse(model.fit)
## [1] 1041.809
rmse(model.fit, Orange$Tree)
## 3 1 5 2 4
## 602.4244 688.8896 929.9055 1319.1573 1408.7033
(very) short intro into pipes
Pipes (|) are a common tool in the linux / programming world that
can be used to chain inputs and outputs of functions together. In R
there are two packages, namely dplyr and magrittr that enable
general piping between all functions
Goal:
Solve complex problems by combining simple pieces
(Hadley Wickham)
library(dplyr)
model.rmse <- Orange %>%
lm(age ~ circumference, data=.) %>%
rmse(., Orange$Tree) %>%
barplot
OR like this (Correlation within Iris dataset)
iris %>% group_by(Species) %>%
summarize(count = n(), pear_r = cor(Sepal.Length, Petal.L
arrange(desc(pear_r))
## Source: local data frame [3 x 3]
##
## Species count pear_r
## 1 virginica 50 0.8642247
## 2 versicolor 50 0.7540490
## 3 setosa 50 0.2671758
Outsource your functions
# Put your function into an extra files
# At the beginning of your main processing script
# you simply load them via source
source("outsourced.rmse.R")
Easy package writing
Open RStudio
Install the devtools and roxygen2 package
Create a new package project and use the existing function as
basis
Create the documentation for it
Update the package metadata and build your package
library(roxygen2)
library(devtools)
# Build your package with two simple commands
# Has to be within your package project
document() # Update the namespace
install() # Install.package
However package development has multiple facets and options.
More detailed info on Package development with RStudio.
Higher acceptance for method papers and analysis code. Make
it citable with a DOI
Software management and collaboration with Github
Git is one of the most commonly used revision control systems
Originally developed for the Linux kernel by Linus Torvalds
Github is web-based software repository service offering
distributed revision control
Californian Startup, now the largest code hoster in the
world
Offers public repositories for free, private for money and a
nice snippet exchange service called gists
How to Git with rstudio (do it later)
1. Setup an account with a git repository hoster like Github
2. Install RStudio and git for your platform (http://www.
rstudio.com/ide/docs/version_control/overview)
3. Link to the git executable within the RStudio options
4. Create a new repository on Github and a new project in
RStudio -> Version Control git
5. Clone your empty project (pull), add new files/changes to it
(commit) and (push)
Idea for CMEC R Users:
Create a Github organization (like a repository basecamp)
Further developments
There are now packages to push gists and normal git updates
directly from within R. In order to use them you need a github api
key (instructions on the websites below) rgithub
To detailed to show here, but have a look at the gistr package:
gistr

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Introduction to R for Data Science :: Session 8 [Intro to Text Mining in R, M...
Introduction to R for Data Science :: Session 8 [Intro to Text Mining in R, M...Introduction to R for Data Science :: Session 8 [Intro to Text Mining in R, M...
Introduction to R for Data Science :: Session 8 [Intro to Text Mining in R, M...
 
Datamining with R
Datamining with RDatamining with R
Datamining with R
 
Python programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operationsPython programming: Anonymous functions, String operations
Python programming: Anonymous functions, String operations
 
Grouping & Summarizing Data in R
Grouping & Summarizing Data in RGrouping & Summarizing Data in R
Grouping & Summarizing Data in R
 
Manipulating string data with a pattern in R
Manipulating string data with  a pattern in RManipulating string data with  a pattern in R
Manipulating string data with a pattern in R
 
Functional Programming in R
Functional Programming in RFunctional Programming in R
Functional Programming in R
 
R code descriptive statistics of phenotypic data by Avjinder Kaler
R code descriptive statistics of phenotypic data by Avjinder KalerR code descriptive statistics of phenotypic data by Avjinder Kaler
R code descriptive statistics of phenotypic data by Avjinder Kaler
 
Tackling repetitive tasks with serial or parallel programming in R
Tackling repetitive tasks with serial or parallel programming in RTackling repetitive tasks with serial or parallel programming in R
Tackling repetitive tasks with serial or parallel programming in R
 
R basics
R basicsR basics
R basics
 
R tutorial for a windows environment
R tutorial for a windows environmentR tutorial for a windows environment
R tutorial for a windows environment
 
Introduction to Data Mining with R and Data Import/Export in R
Introduction to Data Mining with R and Data Import/Export in RIntroduction to Data Mining with R and Data Import/Export in R
Introduction to Data Mining with R and Data Import/Export in R
 
R Programming Language
R Programming LanguageR Programming Language
R Programming Language
 
Python- Regular expression
Python- Regular expressionPython- Regular expression
Python- Regular expression
 
Data Management in Python
Data Management in PythonData Management in Python
Data Management in Python
 
R Programming: Getting Help In R
R Programming: Getting Help In RR Programming: Getting Help In R
R Programming: Getting Help In R
 
Unit 2
Unit 2Unit 2
Unit 2
 
Functional Programming in R
Functional Programming in RFunctional Programming in R
Functional Programming in R
 
Introduction to R for Data Science :: Session 1
Introduction to R for Data Science :: Session 1Introduction to R for Data Science :: Session 1
Introduction to R for Data Science :: Session 1
 
R language
R languageR language
R language
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 

Similar a Reproducibility with R

Modeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.pptModeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.ppt
anshikagoel52
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
KabilaArun
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
attalurilalitha
 
Questions On The Code And Core Module
Questions On The Code And Core ModuleQuestions On The Code And Core Module
Questions On The Code And Core Module
Katie Gulley
 

Similar a Reproducibility with R (20)

Get started with R lang
Get started with R langGet started with R lang
Get started with R lang
 
Oct.22nd.Presentation.Final
Oct.22nd.Presentation.FinalOct.22nd.Presentation.Final
Oct.22nd.Presentation.Final
 
Devtools cheatsheet
Devtools cheatsheetDevtools cheatsheet
Devtools cheatsheet
 
Devtools cheatsheet
Devtools cheatsheetDevtools cheatsheet
Devtools cheatsheet
 
Lecture1_R.ppt
Lecture1_R.pptLecture1_R.ppt
Lecture1_R.ppt
 
Lecture1_R.ppt
Lecture1_R.pptLecture1_R.ppt
Lecture1_R.ppt
 
Lecture1 r
Lecture1 rLecture1 r
Lecture1 r
 
Modeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.pptModeling in R Programming Language for Beginers.ppt
Modeling in R Programming Language for Beginers.ppt
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
 
R-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdfR-Language-Lab-Manual-lab-1.pdf
R-Language-Lab-Manual-lab-1.pdf
 
Getting started with R
Getting started with RGetting started with R
Getting started with R
 
Introduction to r
Introduction to rIntroduction to r
Introduction to r
 
Data Science - Part II - Working with R & R studio
Data Science - Part II -  Working with R & R studioData Science - Part II -  Working with R & R studio
Data Science - Part II - Working with R & R studio
 
R package development, create package documentation isabella gollini
R package development, create package documentation   isabella golliniR package development, create package documentation   isabella gollini
R package development, create package documentation isabella gollini
 
Reproducible Computational Research in R
Reproducible Computational Research in RReproducible Computational Research in R
Reproducible Computational Research in R
 
Language-agnostic data analysis workflows and reproducible research
Language-agnostic data analysis workflows and reproducible researchLanguage-agnostic data analysis workflows and reproducible research
Language-agnostic data analysis workflows and reproducible research
 
Questions On The Code And Core Module
Questions On The Code And Core ModuleQuestions On The Code And Core Module
Questions On The Code And Core Module
 
Lecture1
Lecture1Lecture1
Lecture1
 
Inroduction to r
Inroduction to rInroduction to r
Inroduction to r
 

Último

Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
only4webmaster01
 
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men 🔝malwa🔝 Escorts Ser...
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men  🔝malwa🔝   Escorts Ser...➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men  🔝malwa🔝   Escorts Ser...
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men 🔝malwa🔝 Escorts Ser...
amitlee9823
 
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...
karishmasinghjnh
 
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
amitlee9823
 
➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men 🔝Sambalpur🔝 Esc...
➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men  🔝Sambalpur🔝   Esc...➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men  🔝Sambalpur🔝   Esc...
➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men 🔝Sambalpur🔝 Esc...
amitlee9823
 
Just Call Vip call girls roorkee Escorts ☎️9352988975 Two shot with one girl ...
Just Call Vip call girls roorkee Escorts ☎️9352988975 Two shot with one girl ...Just Call Vip call girls roorkee Escorts ☎️9352988975 Two shot with one girl ...
Just Call Vip call girls roorkee Escorts ☎️9352988975 Two shot with one girl ...
gajnagarg
 
➥🔝 7737669865 🔝▻ Dindigul Call-girls in Women Seeking Men 🔝Dindigul🔝 Escor...
➥🔝 7737669865 🔝▻ Dindigul Call-girls in Women Seeking Men  🔝Dindigul🔝   Escor...➥🔝 7737669865 🔝▻ Dindigul Call-girls in Women Seeking Men  🔝Dindigul🔝   Escor...
➥🔝 7737669865 🔝▻ Dindigul Call-girls in Women Seeking Men 🔝Dindigul🔝 Escor...
amitlee9823
 
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night StandCall Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men 🔝mahisagar🔝 Esc...
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men  🔝mahisagar🔝   Esc...➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men  🔝mahisagar🔝   Esc...
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men 🔝mahisagar🔝 Esc...
amitlee9823
 
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night StandCall Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 

Último (20)

Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
 
Aspirational Block Program Block Syaldey District - Almora
Aspirational Block Program Block Syaldey District - AlmoraAspirational Block Program Block Syaldey District - Almora
Aspirational Block Program Block Syaldey District - Almora
 
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
 
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Anomaly detection and data imputation within time series
Anomaly detection and data imputation within time seriesAnomaly detection and data imputation within time series
Anomaly detection and data imputation within time series
 
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men 🔝malwa🔝 Escorts Ser...
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men  🔝malwa🔝   Escorts Ser...➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men  🔝malwa🔝   Escorts Ser...
➥🔝 7737669865 🔝▻ malwa Call-girls in Women Seeking Men 🔝malwa🔝 Escorts Ser...
 
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
 
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...
👉 Amritsar Call Girl 👉📞 6367187148 👉📞 Just📲 Call Ruhi Call Girl Phone No Amri...
 
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24  Building Real-Time Pipelines With FLaNKDATA SUMMIT 24  Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
 
Discover Why Less is More in B2B Research
Discover Why Less is More in B2B ResearchDiscover Why Less is More in B2B Research
Discover Why Less is More in B2B Research
 
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 
➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men 🔝Sambalpur🔝 Esc...
➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men  🔝Sambalpur🔝   Esc...➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men  🔝Sambalpur🔝   Esc...
➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men 🔝Sambalpur🔝 Esc...
 
Just Call Vip call girls roorkee Escorts ☎️9352988975 Two shot with one girl ...
Just Call Vip call girls roorkee Escorts ☎️9352988975 Two shot with one girl ...Just Call Vip call girls roorkee Escorts ☎️9352988975 Two shot with one girl ...
Just Call Vip call girls roorkee Escorts ☎️9352988975 Two shot with one girl ...
 
➥🔝 7737669865 🔝▻ Dindigul Call-girls in Women Seeking Men 🔝Dindigul🔝 Escor...
➥🔝 7737669865 🔝▻ Dindigul Call-girls in Women Seeking Men  🔝Dindigul🔝   Escor...➥🔝 7737669865 🔝▻ Dindigul Call-girls in Women Seeking Men  🔝Dindigul🔝   Escor...
➥🔝 7737669865 🔝▻ Dindigul Call-girls in Women Seeking Men 🔝Dindigul🔝 Escor...
 
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night StandCall Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Doddaballapur Road ☎ 7737669865 🥵 Book Your One night Stand
 
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men 🔝mahisagar🔝 Esc...
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men  🔝mahisagar🔝   Esc...➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men  🔝mahisagar🔝   Esc...
➥🔝 7737669865 🔝▻ mahisagar Call-girls in Women Seeking Men 🔝mahisagar🔝 Esc...
 
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
 
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night StandCall Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Attibele ☎ 7737669865 🥵 Book Your One night Stand
 
Detecting Credit Card Fraud: A Machine Learning Approach
Detecting Credit Card Fraud: A Machine Learning ApproachDetecting Credit Card Fraud: A Machine Learning Approach
Detecting Credit Card Fraud: A Machine Learning Approach
 
Predicting Loan Approval: A Data Science Project
Predicting Loan Approval: A Data Science ProjectPredicting Loan Approval: A Data Science Project
Predicting Loan Approval: A Data Science Project
 

Reproducibility with R

  • 1. Reproducible R coding CMEC R-Group Martin Jung 12.02.2015
  • 2. Goals of reproducible programming? Make your code readible by you and others Group your code and functionalize Embrace collaboration, version control and automation
  • 3. First step - readibility 1. Writing cleaner code
  • 4. Writing cleaner R code | Names Keep new filenames descriptive and meaningful "helper-functions.R" # or for sequences of processing work "01_Download.R" "02_Preprocessing.R" #... Use CamelCase or Snake_case for variables "spatial_data" "ModelFit" "regression.results" Avoid predetermined names like c or plot
  • 5. Writing cleaner R code | Spacing Use Spacing just as in the english language # Good model.fit <- lm(age ~ circumference, data = Orange) # Bad f1=lm(Orange$age~Orange$circumference) Don’t be afraid of using new lines model.results <- data.frame(Type = sample(letters, 10), Data = NA, SampleSize = 10 ) # Same goes for loops # And don't forget good documentation
  • 6. More on writing clean code Google R Style Guide Hadley Wickhams Style Guide RopenSci Guide And there even is a r-package to clean up your code: formatR
  • 7. Further ways to improve reproduciability Ideally attach your code + data to publications Open-access hoster (DataDryad, Figshare, Zenodo) Restructuring of workflow with RMarkdown / LaTeX / HTML
  • 8. Functionalize! Many R users are tempted to write their code very specialized and non-reusable Number 1 rule for clear coding : DRY - Don't repeat yourself! Simple example: We want to fit a linear model to test if in an orange orchard the circumference (mm) increases with age (age of trees). If so we want to quantify and display the Root-Mean-Square-Error (RMSE) of this fit for each individual orange tree in the dataset (N = 5).
  • 9. Normal way: # Linear model model.fit <- lm(age ~ circumference, data = Orange) model.resid <- residuals( model.fit ) model.fitted <- fitted( model.fit ) rmse <- sqrt( mean( (model.resid - model.fitted)^2 )) tapply(model.resid - model.fitted, Orange$Tree, function(x) sqrt( mean( (x)^2 )))
  • 10. 3 1 5 2 4 0200400600800100012001400
  • 11. Defining your functions Essentially most r-packages are just a compilation of useful functions that users have written. # We want to get the RMSE of a linear model rmse <- function(fit, groups = NULL, ...) { f.resid <- residuals(fit);f.fitted <- fitted(fit) if(! is.null( groups )) { tapply((f.resid-f.fitted), groups, function(x) sqrt(mea } else { sqrt(mean((f.resid-f.fitted)^2, ...)) } }
  • 12. model.fit <- lm(age ~ circumference, data = Orange) # This function is more flexible, can be further customized # applied in other situations rmse(model.fit) ## [1] 1041.809 rmse(model.fit, Orange$Tree) ## 3 1 5 2 4 ## 602.4244 688.8896 929.9055 1319.1573 1408.7033
  • 13. (very) short intro into pipes Pipes (|) are a common tool in the linux / programming world that can be used to chain inputs and outputs of functions together. In R there are two packages, namely dplyr and magrittr that enable general piping between all functions Goal: Solve complex problems by combining simple pieces (Hadley Wickham)
  • 14. library(dplyr) model.rmse <- Orange %>% lm(age ~ circumference, data=.) %>% rmse(., Orange$Tree) %>% barplot OR like this (Correlation within Iris dataset) iris %>% group_by(Species) %>% summarize(count = n(), pear_r = cor(Sepal.Length, Petal.L arrange(desc(pear_r)) ## Source: local data frame [3 x 3] ## ## Species count pear_r ## 1 virginica 50 0.8642247 ## 2 versicolor 50 0.7540490 ## 3 setosa 50 0.2671758
  • 15. Outsource your functions # Put your function into an extra files # At the beginning of your main processing script # you simply load them via source source("outsourced.rmse.R")
  • 16. Easy package writing Open RStudio Install the devtools and roxygen2 package Create a new package project and use the existing function as basis Create the documentation for it Update the package metadata and build your package library(roxygen2) library(devtools) # Build your package with two simple commands # Has to be within your package project document() # Update the namespace install() # Install.package
  • 17. However package development has multiple facets and options. More detailed info on Package development with RStudio. Higher acceptance for method papers and analysis code. Make it citable with a DOI
  • 18. Software management and collaboration with Github Git is one of the most commonly used revision control systems Originally developed for the Linux kernel by Linus Torvalds
  • 19.
  • 20. Github is web-based software repository service offering distributed revision control Californian Startup, now the largest code hoster in the world Offers public repositories for free, private for money and a nice snippet exchange service called gists
  • 21. How to Git with rstudio (do it later) 1. Setup an account with a git repository hoster like Github 2. Install RStudio and git for your platform (http://www. rstudio.com/ide/docs/version_control/overview) 3. Link to the git executable within the RStudio options 4. Create a new repository on Github and a new project in RStudio -> Version Control git 5. Clone your empty project (pull), add new files/changes to it (commit) and (push)
  • 22. Idea for CMEC R Users: Create a Github organization (like a repository basecamp)
  • 23. Further developments There are now packages to push gists and normal git updates directly from within R. In order to use them you need a github api key (instructions on the websites below) rgithub To detailed to show here, but have a look at the gistr package: gistr