knitr::opts_chunk$set(echo = TRUE) library(tidyverse) library(gapminder)
General advice
Useful tips
Pay attention to error messages
'Googling' error messages will very often prove useful
How do I XX in R
Set up an Rstudio 'project' for each project.
These are just folders that Rstudio adds some extra info to.
Make separate folders for project-specific data
, code
and results
You can switch between projects using the project tab in the upper-right corner of Rstudio.
If you're redoing the same thing many times, use a function
help(my_function)
or ?my_function
to pull up help documentation for a function
help(median)
Use read_csv
, read_tsv
to read common text file formats into tibbles
You can tell which to use by the type of file:
read_csv
: for .csv files (comma-separated)
read_tsv
: for .tsv files (tab-separated)
For .txt files read_table
is a good bet
You can use the same functions as above to load matrices as tibbles
For matrices, it's helpful to have one column be the 'rownames'.
You can convert a tibble df
to a matrix mat
like this:
mat <- df %>% column_to_rownames(var = 'row_col') %>% as.matrix()
Always inspect data after loading it to make sure that the data type of each field is correct and the column names look right
For tibbles:
glimpse(gapminder) head(gapminder)
corner
function from the useful
package is nice, it just shows the upper-left portion of the matrixuseful::corner(my_mat)
When you have an Rproject loaded, if you've organized your project folder as above, you can easily load data from that project's data folder without having to worry about file 'paths': read_csv(data/my_data.csv)
To load data from elsewhere you need to specify the full 'path', e.g. read_csv(~/my_stuff/general_data/my_data.csv)
You can read more about file paths, e.g. here
As an easy fall-back, you can always use the Import Dataset
tool in Rstudio.
Write analysis like an experiment notebook
Emphasis on clarity and communication
Write text to describe what you're doing and why, embed code and results
Encourages reproducibility, clarity, and robustness
ALSO use in-line comments with your code using #
Rmarkdown naturally facilitates literate coding (more)
Intersperse code chunks with explanatory text, and embed results
Easily output as pdf, html, and other formats
Look for examples you like (e.g. gallery) and use as templates
See the RMarkdown cheatsheet for more tips
tidy
data provides a consistent framework that works with tidyverse
tools
Making plots: ggplot
-- Look for examples online and copy the code!
tidyverse cheat sheet
Many more cheat sheets here
Use version control git
[POINTER TO TUTORIAL]
Uses the DT
package
VERY easy to create nice interactive tables in your html reports (use with Rmarkdown only)
library(DT) datatable(gapminder)
ggplotly
takes as input a ggplot object and converts it into a cool interactive plot (that can be embedded in your html reports or used in Rstudio).
Note that the text
aesthetic includes the country name in the hovertip text (so you can see the country associated with each dot when you hover over it with your mouse)
library(plotly) g <- ggplot(gapminder %>% filter(year == 1952), aes(x = gdpPercap, y = lifeExp, text = country)) + geom_point() + scale_x_log10() ggplotly(g)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.