LogoLogo
  • About this Documentation
  • Getting started
    • Initial Resources
    • OSPSuite Architecture
  • Setup
    • Git Workflow
    • C# Coding Standards
    • Setting up the developer environment for C#
  • OSPSuite-R Setup
    • Setup OSPSuite-R on Windows
    • Setup OSPSuite-R on CentOS7
    • Setup OSPSuite-R on Ubuntu
  • R Development Resources
    • R Development Collaboration Guide
    • R Coding Standards
    • Best Practices R
    • R Code structure
  • .NET Code Specifics
    • Serialization
    • Unit and Integration Testing
    • Commands
    • Debugging
    • Data Binding
  • PK-Sim Database
    • PK-Sim Database Description
  • Sim Model
    • Finding Memory Leaks
  • Algorithms
    • Algorithm for Individual Creation
Powered by GitBook
On this page
  • theme
  • dev_mode
  • Reload the package
  • Add or update script files
  • Coding standards
  • Useful literature
  • Examples of "good" packages
  • Useful shortcuts
  • Profiling with R-Studio
  • Graphics
  • Snapshot testing
  • Setting up Linux environment for R development
  1. R Development Resources

Best Practices R

PreviousR Coding StandardsNextR Code structure

Last updated 9 months ago

theme

If you like vscode theme, use https://github.com/anthonynorth/rscodeio

dev_mode

devtools::dev_mode function switches your version of R into "development mode". This is useful to avoid clobbering the existing versions of CRAN packages that you need for other tasks. Calling dev_mode() again will turn development mode off, and return you to your default library setup.

# This will install the package in the folder C:/Rpackages
devtools::dev_mode(path="C:/Rpackages")

Reload the package

devtools::load_all()

or Ctrl + Shift + L

Add or update script files

.R files defined in tests\dev\ will be removed from the package and can be used to simulate interaction with the package. See

Coding standards

Coding standards are described

Useful literature

Examples of "good" packages

Examples of packages that can serve as inspiration:

Useful shortcuts

  • Show all shortcuts: Alt+Shift+K

  • Reload package: Cmd + Shift + L

  • Navigate to: Ctrl + .

  • Generate Doc: Ctrl + Shift + D

  • Run unit tests: Ctrl + Shift + T

  • Navigate to implementation: Mouse over + F2 or CTRL + Mouse Click

  • Un-/Comment line/selection: Ctrl + Shift + C

  • Multi-select: CTRL+SHIFT+ALT+M

Profiling with R-Studio

profvis({
  data(diamonds, package = "ggplot2")

  plot(price ~ carat, data = diamonds)
  m <- lm(price ~ carat, data = diamonds)
  abline(m, col = "red")
})

Graphics

Snapshot testing

  • Short summary:

    • The first time a test with snapshot is executed, it creates a snapshot file that will be considered the truth. Therefore it is important to check this file for its validity.

    • If the behavior of the tested function changes, the test will fail, as the new output will differ from the snapshot.

    • Run snapshot_review() to compare the new output with the snapshot.

    • If the new behavior is correct, accept the snapshot by calling snapshot_accept().

  • If build fails because of failing snapshot tests, never accept new snapshots without manual review.

Setting up Linux environment for R development

As an example, a Hyper-V Virtual Machine under Windows 10 is used. Currently tested with Ubuntu 19.10

  1. Install Ubuntu

  • Download Ubuntu from https://ubuntu.com/download/desktop

  • Tutorial: https://www.youtube.com/watch?v=oyNjjzg-UXo = >This is a very good intro to get ubuntu installed from scratch

  1. Install git

  • sudo apt install git

  1. Install nuget

  • sudo apt install nuget

  1. Install R

  • sudo apt install r-base

  1. Install R Studio

  1. Install devtools

  • sudo apt update

  • sudo apt-get install libcurl4-openssl-dev

  • sudo apt-get install libssl-dev

  • sudo apt install libxml2-dev

  • Install devtool from R Studio from CRAN

  1. Install mono (as described here https://www.mono-project.com/download/stable/)

  • sudo apt install gnupg ca-certificates

  • sudo apt autoremove

  • sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF

  • Install the 5.18 version, as the latest stable is bugged atm: echo "deb https://download.mono-project.com/repo/ubuntu stable-bionic/snapshots/5.18 main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.list


LATEST DOES NOT WORK AT THE MOMENT

  • Alternative: install latest stable: echo "deb https://download.mono-project.com/repo/ubuntu stable-bionic main" | sudo tee /etc/apt/sources.list.d/mono-official-stable.list


  • sudo apt update

  • sudo apt install mono-complete

  1. Install some missing dependencies required to build rClr

  • sudo apt-get install libglib2.0-dev

  1. Install .NET SDK

  • See https://aka.ms/dotnet-download

Profiling of code can be done within R-Studio with the package profvis, a description of the process is given . In short, pass the code to be profiled as argument to the function profvis:

{ospsuite} uses snapshots to test the behavior of plot functions. Read for information on how to.

Download R studio from

Optionally: Install

scripts.R
here
Advanced R by Hadley Wickham
tidyverse
PKPDsim
here
Export to SVG
Introduction to snapshot testing in R
here
MonoDevelop