# Goodfit_models.Rutility_functions.R# Badfit models.Rfoo.rstuff.r
# Load data ---------------------------# Plot data ---------------------------
Strive for names that are concise and meaningful.
# Goodday_oneday_1# Badfirst_day_of_the_monthDayOnedayonedjm1
Avoid using already existing names.
# BadT <- FALSEc <- 10mean <- function(x) sum(x)
Any style guide is fundamentally opinionated...
# Good Hadleyaverage <- mean((feet / 12) + inches, na.rm = TRUE)sqrt(x^2 + y^2)x <- 1:10base::get# Good Alexisaverage <- mean( ( feet / 12 ) + inches , na.rm = TRUE)sqrt( x^2 + y^2 )x <- 1:10base::get# Badaverage<-mean(feet/12 + inches,na.rm=TRUE)sqrt(x ^ 2 + y ^ 2)x <- 1 : 10base :: get
# Goodif (y < 0 && debug) { message("y is negative")}# Badif (y < 0 && debug) message("Y is negative")
Strive to limit your code to 80 characters per line.
# Gooddo_something_very_complicated( something = "that", requires = many, arguments = "some of which may be long")# Baddo_something_very_complicated("that", requires, many, arguments, "some of which may be long")
# Goodx <- 5# Badx = 5
You can use Ctrl + Shift + A
or Command + Shift + A
Or lintr
and styler
packages
install.packages(c("lintr", "styler"))library("lintr")library("styler")
lintr can be used o a source file, eg bad_style.R
being the following source:
# Spacing average<-mean(feet/12 + inches,na.rm=TRUE)sqrt(x ^ 2 + y ^ 2)x <- 1 : 10base :: get# Indentingif (y < 0 && debug) message("Y is negative")# Assignmentx = 5
Then run
lint("bad_style.R")
You can use the style_text()
function as follows
ugly_code <- "a<-function( x){1+1} "style_text(ugly_code)
# Bada<-function( x){1+1}# Gooda <- function(x) { 1 + 1}
As well as the following variants of style_text()
:
style_file()
styles .R and/or .Rmd files.style_dir()
styles all .R files in a directory.style_pkg()
styles the source files of an R package.Get automatic advice about your package
Install and run goodpractice
package for Advice (that) includes functions and syntax to avoid, package structure, code complexity, code formatting, etc.
Remove typos!
Use the devtools::spell_check()
function.
To complete:
Create a R profile file .Rprofile
with the path to your local
library if you use local libraries into your code, e.g :
.libPaths(new="~/R_libs")
See http://gettinggeneticsdone.blogspot.com/2013/06/customize-rprofile.html for more informations.
Create a directory with the name of your package, e.g. MyPackage
,
with inside:
R
for the R source codeDESCRIPTION
for meta-informations on your package.dir.create( path = "MyPackage" )dir.create( path = paste( "MyPackage" , "R" , sep = .Platform$file.sep ) )file.create( path = paste( "MyPackage" , "DESCRIPTION" , sep = .Platform$file.sep ) )
cat( c( "Package: MyPackage" , "Version: 0.1" , "Date: 2018-02-08" , "Type: Package" , "Title: My nice package" , "Author: A. Arnaud" , "Maintainer: Alexis Arnaud <alexis.arnaud@inria.fr>" , "Description: Basic package to display 2 ggplot2 plots." , "License: GPLv3" , "URL: https://github.com/jarbel/Rgoodpractice.git" ) , file = paste( "MyPackage" , "DESCRIPTION" , sep = .Platform$file.sep ) , sep = "\n" , append = TRUE )
multiplot : http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/
cat( c( "plot_boxplot <- function(data) { if ( !is.data.frame(x = data) ) data <- as.data.frame(x = data) data_melt <- melt(data = data) plot_pairs <- ggpairs(data = data) plot_boxplot <- ggplot( data = data_melt , aes( x = variable , y = value , fill=variable) ) + geom_boxplot( ) multiplot( plot_pairs , plot_boxplot , cols = 2) }" ) , file = "plot_functions.R" , sep = "\n" , append = TRUE )
Use the Roxygen templates to start the documentation:
And fill the Roxygen fields in order to best describe your function:
Now, you just need to move your R files to the R directory of your package, and then run Roxygen before building and installing your package.
library(package = "roxygen2")for ( file in c( "plot_functions.R", "multiplot.R" ) ) {file.copy( from = file , to = paste( "MyPackage" , "R" , file , sep = .Platform$file.sep ) ) }roxygenize( package.dir = "MyPackage" , clean = TRUE )library(package = "tools")Rcmd(args = "build MyPackage")## or with a shell : R CMD build MyPackageinstall.packages(pkgs = "MyPackage_0.1.tar.gz")library(package = "MyPackage")
n_col <- 3n_row <- 1e3m_test <- t( x = matrix( data = rnorm( n = n_col * n_row , mean = 0 , sd = 1 ) , nrow = n_col , ncol = n_row , dimnames = list( "Variables" = paste("V ", seq_len(length.out = n_col), sep = "") , "Observations" = NULL ) ) * c(0.1, 1, 3) + c(-1, 1, 0))plot_boxplot(data = m_test)
To complete:
Presentation available at
https://jarbel.github.io/Rgoodpractice/rpresentation.html
http://www.julyanarbel.com/ & http://mistis.inrialpes.fr/people/arnaud/
Twitter: @julyanarbel
Slides created via the R package xaringan.
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |