An Intro to formatR You can install formatR from CRAN, or XRAN if you want to test the latest development version: install.packages("formatR", repos = " Or check out the Github repository and install from source if you know what this means. This page is always based on the development version. library(formatR) sessionInfo() The formatR package was designed to reformat R code to improve readability; the main workhorse is the function tidy_source(). long lines of code and comments are reorganized into appropriately shorter onesspaces and indent are added where necessarycomments are preserved in most casesthe number of spaces to indent the code (i.e. tab width) can be specified (default is 4)an else statement in a separate line without the leading } will be moved one line back= as an assignment operator can be replaced with <-the left brace { can be moved to a new line Below is an example of what tidy_source() can do. Two applications of tidy_source(): formatR::tidy_app() Replace = with <- ?
Beautiful plotting in R: A ggplot2 cheatsheet | Technical Tidbits From Spatial Analysis & Data Science Even the most experienced R users need help creating elegant graphics. The ggplot2 library is a phenomenal tool for creating graphics in R but even after many years of near-daily use we still need to refer to our Cheat Sheet. Up until now, we’ve kept these key tidbits on a local PDF. But for our own benefit (and hopefully yours) we decided to post the most useful bits of code. * Last updated January 20, 2016 (with ggplot2 2.0 replace vjust with margin for title text) You may also be interested in these other ggplot2-related posts: Under the hood of ggplot2 graphics in RMapping in R using the ggplot2 packageA new data processing workflow for R: dplyr, magrittr, tidyr and ggplot2 We start with the the quick setup and a default plot followed by a range of adjustments below. We’re using data from the National Morbidity and Mortality Air Pollution Study (NMMAPS). You can also download the data we’re using in this post here. g<-ggplot(nmmaps, aes(date, temp))+geom_point(color="firebrick") g Voila!
R for MATLAB users Help Searching available documentation Using interactively Operators Arithmetic operators Relational operators Logical operators root and logarithm Round off Mathematical constants Missing values; IEEE-754 floating point status flags Complex numbers Trigonometry Generate random numbers Vectors Sequences Concatenation (vectors) Repeating Miss those elements out Maximum and minimum Vector multiplication Matrices Concatenation (matrices); rbind and cbind Array creation Reshape and flatten matrices Shared data (slicing) Indexing and accessing elements (Python: slicing) Assignment Transpose and inverse Sum Sorting Matrix manipulation Equivalents to "size" Matrix- and elementwise- multiplication Find; conditional indexing Multi-way arrays File input and output Plotting Basic x-y plots Axes and titles Log plots Filled plots and bar plots Functions Polar plots Histogram plots 3d data Contour and image plots Perspective plots of surfaces over the x-y plane Scatter (cloud) plots Save plot to a graphics file Data analysis Set membership operators
Cookbook for R » Cookbook for R Random Data Sets Quickly Random Data Sets Quickly This post will discuss a recent GitHub package I’m working on, wakefield to generate random data sets. The post is broken into the following sections: Demo 1.1 Random Variable Functions 1.2 Random Data Frames 1.3 Missing Values 1.4 Default Data SetFuture DirectionGetting Involved You can view just the R code HERE or PDF version HERE One of my more popular blog posts, Function To Generate A Random Data Set, was an early post about generating random data sets. For one it’s not in a package so it’s not handyIt generates too many unrelated columns Recently I had an idea inspired by Richie Cotton’s rebus and Kevin Ushey & Jim Hester’s rex regex based packages. Random Variable Functions First we’ll use the pacman package to grab and load the wakefield package from GitHub. Then we’ll look at a random variable generating function. ## [1] White White White Black White Hispanic Black ## [8] Asian Hispanic White ## Levels: White Hispanic Black Asian Bi-Racial Native Other Hawaiian
Quick-R: Home Page 21 R navigation tools Navigation gets you from where you are to where you want to be. Speaking of navigation, you can jump to selected sections of this post: Navigation; R-bloggers; Task views; Rdocumentation.org; sos package; ??; apropos; ls; methods; getAnywhere; :::; find; args; grep; %in%; str; getwd; file.choose; Spyglass summary; browser; See also. Overview Figure 1: A map of the R world. Each R session has a workspace specific to it. ls() Or if you prefer explicitness over laziness: objects() But your R session will know about other objects as well. search() The items can be packages, or files of R objects (created, for example, via the save function and put on the search list with attach). The packages on the search list will have been selected from the library of packages on your machine. library() Add a package to the search list with the require function. require(BurStMisc) The same effect is achieved with: library(BurStMisc) The packages in your library have to get there from somewhere. Navigation ??? > ??? ?
UZH - Methodenberatung - Datenanalyse Universität Zürich » Startseite Methodenportal » Datenanalyse Home | Kontakt | Sitemap Datenanalyse top Minimal reproducible examples Minimal reproducible examples I occasionally get emails from people thinking they have found a bug in one of my R packages, and I usually have to reply asking them to provide a minimal reproducible example (MRE). This post is to provide instructions on how to create a MRE. Bug reports on github, not email First, if you think there is a bug, please don’t send me emails. Creating an MRE An MRE is intended to reproduce an error using the smallest amount of code. It should consist of a single R script file that can be run without error. Packages to be loaded. To check that your code is reproducible, try running it in a fresh R session. Adding your code to a bug report Copy and paste your MRE into gist.github.com, set the language to R, and choose “Create secret Gist”. Then go to the github repository for the package, create a new issue, explain the problem in a few words and paste the URL to your MRE. What not to do Report a bug that has already been fixed.
Niche modeling in R Lecture notes from Bio793: Plant Ecology / Niche modeling / Modeling in R Spring 2010 Jason Fridley, Syracuse University These are lecture notes from the Spring 2010 version of Bio793 at Syacuse. Objectives of the course were for students to learn R, implement different modeling approaches common to ecologists (and particularly species distribution modeling), and be introduced to the utility and construction of hierarchical models. The course was focused on implementation rather than statistical theory. I welcome comments (fridley -at- syr.edu). Recommendations on texts: We did not use a formal text for the course, but the following books proved helpful: Crawley MJ (2007). Most of the earlier topics below use the treedata.csv dataset (see tree_metadata.txt). Course topics Generalized additive models in R - non-parameteric smoothers, loess - gam in the mgcv library - gam in the gam library
Robin Lovelace - Consistent naming conventions in R Naming conventions in R are famously anarchic, with no clear winner and multiple conventions in use simultaneously in the same package. This has been written about before, in a lucid article in the R Journal, a detailed exploration of names in R source code hosted on CRAN and general discussion on stackoverflow. Basically, there are 5 naming conventions to choose from: alllowercase: e.g. adjustcolor period.separated: e.g. plot.new underscore_separated: e.g. numeric_version lowerCamelCase: e.g. addTaskCallback UpperCamelCase: e.g. SignatureMethod There are clear advantages to choosing one naming convention and sticking to it, regardless which one it is: “Use common sense and BE CONSISTENT” The Google Style Guide is ironically written in a rather inconsistent way (mixing capitals with lowercase in a single sentence surely breaks their own rule!) But which one to choose? Naming convention chaos Does any of your code look like this? It was time to follow our own advice! Which naming convention?
Useful tutorials | Hyndsight There are some tools that I use regularly, and I would like my research students and post-docs to learn them too. Here are some great online tutorials that might help. ggplot tutorial from Winston ChangWriting an R package from Karl BromanRmarkdown from RStudioShiny from RStudiogit/github guide from Karl Bromanminimal make tutorial from Karl Broman Related Posts: r-gotchas How do you you determine the namespace of a function?