Skip to content

Repository files navigation

Fun things in R

Iain McLaughlan 10/2/2021

Quick tips

Package install

Make sure that a package is installed.

if(!library(<package>)){
install.packages("<package>")
library(<package>)
}

Snippets

Bored of typing too much? Use snippets. Go to Tools >> Global options >> Code >> Edit snippets. In here is a list of snippets which can be edited to match the use case that you use it most for. These can come up as auto complete suggestions but if not can be added by typing the key then Shift + Tab.

An example

snippetifif($1:condition){
{2:code}
}

Typing if then Shift + Tab the code in the snippet will be added. Hit Tab to move between editables.

Better printing

Don’t like printing having a [1] in front of it?

cat("<string>")

R project - quick setup

# Create an R projectdevtools::create(path="./<PROJECT NAME>")
# Initialise an R environmentrenv::init()
# Set up unit testingusethis::use_testthat()
# Build packagedevtools::build(path="./")
# Setting the path = "./" will create the .tar.gz within the project dir.# If not then specify a path to your package dir.# Installing from .tar.gz# In this case path_to_file = "./Project_0.0.1.tar.gz"path_to_file="./Project_0.0.1.tar.gz"
install.packages(path_to_file, repos=NULL, type="source")

External dependincies

Working within ./R/DON’T use library(...) or require(...)

# Within a packaged don't use
library(<LIBRARYNAME>)
# or
require(<LIBRARYNAME>)

Instead update the DESCRIPTION file with an Imports: section.

# Within DESCRIPTIONImports:
ggplot2 (>=3.3.3)

Adding imports in this way will install with dependencies when the package is installed. If using this as a pretend package that isn’t installed fully then adding library(...) for the given dependency in the running file should work.

Roxygen - documentation

In a working file use Ctrl + Alt + Shift + R to add a Roxygen documentation template.

# With roxygen comments on functions within ./R create a manualdevtools::document()

Testing

Testthat - unit testing

# Create testing file structureusethis::use_testthat()
# Create tests for the current working fileusethis::use_test() # Adds a test file in ./tests/testthat/test-<FILE NAME>.R# Run all testsdevtools::test()

Covr - test coverage

Test coverage.

Renv - library environments

# Creating an R environmentrenv::init()
# Taking a snapshot of the current dependencies and store in renv.lock.renv::snapshot()
# Load back to the last snapshotrenv::restore()

Git

Supper basic just covering the most commonly used commands.

Create a git repo

gitinit

Add files, commit and push to remote repo (like github or another file store).

gitadd.gitcommit-m"<SHORT DESCRIPTION">-m"<LONGER DESCRIPTION>(optional)"gitpush<REMOTENAME><BRANCHNAME># examplegitpushoriginmaster

Checkout new branch and add to remote.

gitbranch<NEWBRANCHNAME>

or

gitcheckout-b<NEWBRANCHNAME>gitpush--set-upstream<remote><branch># examplegitpush--set-upstreamoriginmaster# If branch is already tracked on remotegitpush

Pull data from remote

gitpull<remote><branch>#examplegitpulloriginmaster# If on branchgitpull

General info

gitstatus

About

A collection of some of the things that I find useful/cool in R and RStudio.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages