background preloader

Cheatsheets - RStudio

Cheatsheets - RStudio
Related:  Learning R

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 <- ?

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

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

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 ??? > ??? ?

Minimal reproducible examples Minimal reproducible examples I occa­sion­ally get emails from peo­ple think­ing they have found a bug in one of my R pack­ages, and I usu­ally have to reply ask­ing them to pro­vide a min­i­mal repro­ducible exam­ple (MRE). This post is to pro­vide instruc­tions on how to cre­ate a MRE. Bug reports on github, not email First, if you think there is a bug, please don’t send me emails. Cre­at­ing an MRE An MRE is intended to repro­duce an error using the small­est amount of code. It should con­sist of a sin­gle R script file that can be run with­out error. Pack­ages to be loaded. To check that your code is repro­ducible, try run­ning it in a fresh R session. Adding your code to a bug report Copy and paste your MRE into gist​.github​.com, set the lan­guage to R, and choose “Cre­ate secret Gist”. Then go to the github repos­i­tory for the pack­age, cre­ate a new issue, explain the prob­lem in a few words and paste the URL to your MRE. What not to do Report a bug that has already been fixed.

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 reg­u­larly, and I would like my research stu­dents and post-​​docs to learn them too. Here are some great online tuto­ri­als that might help. ggplot tuto­r­ial from Win­ston ChangWrit­ing an R pack­age from Karl BromanRmark­down from RStudioShiny from RStudiogit/​github guide from Karl Bromanmin­i­mal make tuto­r­ial from Karl Broman Related Posts: r-gotchas How do you you determine the namespace of a function? Vectorization in R: Why? Here are my notes from a recent talk I gave on vectorization at a Davis R Users’ Group meeting. Thanks to Vince Buffalo, John Myles White, and Hadley Wickham for their input as I was preparing this. Feedback welcome! Beginning R users are often told to “vectorize” their code. Now, remember, premature optimization is the root of all evil (Knuth). First, let’s acknowledge that vectorization can seem like voodoo. Why on earth should these take a different amount of time to calculate? 1. R is a high-level, interpreted computer language. i <- 5.0 you don’t have to tell your computer: That “5.0” is a floating-point numberThat “i” should store numeric-type dataTo find a place in memory for to put “5”To register “i” as a pointer to that place in memory You also don’t have to convert i <- 5.0 to binary code. When you then type i <- "foo" you don’t have to tell the computer that i no longer stores an integer but a series of characters that form a string, to store “f”, “o”, and “o”, consecutively, etc.

How to make a great R reproducible example How to ask for R help by Stephen Weller, Senior Support Engineer at Revolution Analytics, and Joseph Rickert For someone trying to learn any new technology getting help with a problem on a public forum can be stressful. Knowing where to go, deciding how to pose a question and figuring out how to deal with a response can be challenging. Preliminary WorkTwo of the most novice friendly places to go for help in the R world are the R-Help mailing list on CRAN and the R section of stack overflow. Stack overflow provides some excellent suggestions on posting questions. Sharing your research helps everyone. Posting Your QuestionWhen comes time to post your question you may find Steves guidelines helpful. You should always include information on which version of R and what flavor of operating system you are running (see 'R.version' and 'Revo.version' in R)Be as specific as you can in describing your problem. #1 - a good post call: inDL(x, as.logical(local), as.logical(now), ...) thanks #2 - a good post Thank you, Hi,

Rules for Naming Objects in R How are objects allowed to be named in R? As it turns out, this is a very different question from how should objects be named. This isn't about style conventions, camelCase, dots.verus_underscores, or anything like that; this is about what is strictly possible. I do a lot of outreach to HPC people who are starting to get an interest in R, often because their users are starting to ask for help with R. This is a horrible lie. From ? There are no restrictions on name: it can be a non-syntactic name (see make.names). What does that mean? R simply won't allow it. You can prove that it works by checking your global environment with ls(). We can also do fun things like: Remember when I said you could even use unicode (assuming your terminal supports it)? All perfectly valid. As mentioned above, there are three sets of quotes in R; single and double ticks, i.e. ' and ", generally serve the same purpose of creating character data (which the rest of the world calls strings). Or

Memory usage · Advanced R. A solid understanding of R’s memory management will help you predict how much memory you’ll need for a given task and help you to make the most of the memory you have. It can even help you write faster code because accidental copies are a major cause of slow code. The goal of this chapter is to help you understand the basics of memory management in R, moving from individual objects to functions to larger blocks of code. Outline Prerequisites In this chapter, we’ll use tools from the pryr and lineprof packages to understand memory usage, and a sample dataset from ggplot2. install.packages("ggplot2") install.packages("pryr") install.packages("devtools") devtools::install_github("hadley/lineprof") Sources The details of R’s memory management are not documented in a single place. Object size To understand memory usage in R, we will start with pryr::object_size(). library(pryr) object_size(1:10) ## 6.74 kB This isn’t just an artefact of integer vectors. Object metadata (4 bytes). Exercises Loops

Related: