R Commands Cheat Sheet



  1. Basic R Commands Cheat Sheet
  2. R Common Commands Cheat Sheet
  3. R Commands Cheat Sheet
  4. R Program Cheat Sheet
  5. Cheat Sheet For R
  6. R Studio Commands Cheat Sheet
  7. R Cheat Sheets Pdf
  1. R Markdown Cheatsheet R Markdown is an authoring format that makes it easy to write reusable reports with R. You combine your R code with narration written in markdown (an easy-to-write plain text format) and then export the results as an html, pdf, or Word file. You can even use R Markdown to build interactive documents and slideshows.
  2. Start profiling R code: stopBuild: Stop the current build: stopProfiler: Stop profiling R code: suspendSession: Suspend R Session: switchToChunkBash: Switch chunk to Bash: switchToChunkPython: Switch chunk to Python: switchToChunkR: Switch chunk to R: switchToChunkRCPP: Switch chunk to Rcpp: switchToChunkSQL: Switch chunk to SQL: switchToChunkStan: Switch chunk to Stan.
This article lists down 10 popular machine learning algorithms and related R commands (& package information) that could be used to create respective models. The objective is to represent a quick reference page for beginners/intermediate level R programmers who working on machine learning related problems. Please feel free to comment/suggest if I missed to mention one or more important points. Also, sorry for the typos.
Following are the different ML algorithms included in this article:
  1. Linear regression
  2. Logistic Regression
  3. K-Means Clustering
  4. K-Nearest Neighbors (KNN) Classification
  5. Naive Bayes Classification
  6. Decison Trees
  7. Support Vector Machine (SVM)
  8. Artifical Neural Network (ANN)
  9. Apriori
  10. AdaBoost


Basic Linux commands. Description ls Lists all files and directories in the present working directory. Essential Statistics with R: Cheat Sheet Important libraries to load If you don’t have a particular package installed already: install.packages(Tmisc). Work with strings with stringr:: CHEAT SHEET Detect Matches strdetect(string, pattern) Detect the presence of a pattern match in a string. Strdetect(fruit, 'a') strwhich(string, pattern) Find the indexes of strings that contain a pattern match. Strwhich(fruit, 'a') strcount(string, pattern) Count the number of matches in a string.

Cheat Sheet – ML Algorithms & R Commands
  • Linear regression: “lm” method from base package could be used for linear regression models. Following is the sample command:
  • Logistic Regression: Logistic regression is a classification based model. “glm” method from base R package could be used for logistic regression. Following is the sample command:
  • K-Means Clustering: “kmeans” method from base R package could be used to run k-means clustering. Following is a sample command given X is a data matrix and m is the number of clusters:
  • K-Nearest Neighbors (KNN) Classification: “knn” method from “class” package could be used for K-NN modeling. One need to install and load “class” package. Following is the sample command given X_train represents a training dataset, X_test represents test data set, k represents number of nearest neighbors to be included for the modeling
  • Naive Bayes Classification: “naiveBayes” method from “e1071” package could be used for Naive Bayes classification. One need to install and load “e1071” package prior to analysis. Following is the sample command:
  • Decision Trees: “rpart” method from “rpart” can be used for Decision Trees. One need to install and load “rpart” package. Following is the sample command:
  • Support Vector Machine (SVM): “svm” method from “e1071” package could be used for SVM. Note that the same package also provide method, naiveBayes, for Naive Bayes classification. One need to install and load “e1071” package. Following is the sample command given X is the matrix of features, labels be the vector of 0-1 class labels, and C being regularization parameter
  • Artifical Neural Network (ANN): “neuralnet” method from “neuralnet” package could be used for ANN modeling. Following is sample command:

    Prediction could be made using following formula:

  • Apriori: “apriori” method from “arules” package could be used for Apriori analysis. One need to install and load “arules” package. Following is the sample command:
  • AdaBoost: “ada” method from “rpart” package could be used as boosting function. Following is sample command:

For most of the above formulas including linear regression model, one could use following function to predict:


  • First Principles Understanding based on Physics - April 13, 2021
  • Precision & Recall Explained using Covid-19 Example - April 11, 2021
  • Moving Average Method for Time-series forecasting - April 4, 2021

Documentation#

TopicLink
Generalhttps://docs.rstudio.com/
Installing Rhttps://docs.rstudio.com/resources/install-r/
Installing Pythonhttps://docs.rstudio.com/resources/install-python/
RStudio Server admin guidehttps://docs.rstudio.com/ide/server-pro/
RStudio Connect admin guidehttps://docs.rstudio.com/connect/admin/
RStudio Package Manager admin guidehttps://docs.rstudio.com/rspm/admin/
Pro Drivershttps://docs.rstudio.com/pro-drivers/

Pre-requisites:

Installing R#

Install R from pre-compiled binaries#

Recommended best practise is to install R from pre-compiled binaries

For example, on Ubuntu 18.04 for R version 3.6.1.

This script installs into /opt/R/...

Verify installation:

RStudio Server Pro#

Admin guide: https://docs.rstudio.com/ide/server-pro/

Starting, stopping and restarting#

Running the verify installation command:

To restart the RSP service and check its status, run these commands (with sudo if necessary):

Status#

Error logs#

The locations of the various server log files:

The server log is at:

RStudio Connect#

Admin guide: https://docs.rstudio.com/connect/admin/

Config file#

Systemd vs upstart#

Distributions using systemd include Red Hat/CentOS 7, Red Hat/CentOS 8, SUSE 12, Ubuntu 16.04, and Ubuntu 18.04.

  • systemctl start rstudio-connect

Distributions using Upstart include Red Hat/CentOS 6 and Ubuntu 14.04.

  • start rstudio-connect

Starting and stopping#

Checking status#

Log files#

Get the diagnostics script#

Basic R Commands Cheat Sheet

RStudio Package Manager#

Admin guide: https://docs.rstudio.com/rspm/admin/

The rspm command#

Try running the rspm command on the server instance where RSPM is installed:

To save keystrokes when typing RSPM commands, create a command alias for therspm command:

Run the rspm command with no arguments for a list of available commands:

Working with CRAN#

R Common Commands Cheat Sheet

Manage repositories and sources#

Manage packages#

Working with repositories#

Utilities#

System health check#

You have various methods to check the health of your RSPM instance:

R Commands Cheat Sheet

  1. View the RSPM application logs:

  2. View the RSPM access logs:

  3. Check the status of your RSPM license:

  4. Run RSPM diagnostics:

  5. View diagnostics output:

License manager#

View license status#

Activate license:

List of available commands:

Supported commands:

Expired license#

Things to try:

  • Check time and time zone timedatectl
  • Resync if needed, e.g. sudo hwclock -w
  • Restart

Deactivating a license#

Sheet

Deactivation tool: http://apps.rstudio.com/deactivate-license/

Pro Drivers#

Documentation overview: https://docs.rstudio.com/pro-drivers/

Driver locations:

Troubleshooting#

Troubleshooting R#

Almost all RStudio products depend on R being installed successfully to run.

  • Look for errors related to R in the server logs

Can R be launched manually and can the user's code be run in R without any errors?

How was R installed?

  • From source or pre-compiled binaries? If so, were all correct flags and capabilities set?

Are all necessary dependencies installed?

Are the permissions set correctly to allow r-x by all?

Are there multiple versions of R on the system?

Troubleshooting configuration#

Look for any errors in the startup. You can view the live log during startup using:

Read through the admin guides and make sure you have the correct formatting

Make sure you're on the right version of the product compared to the admin guide!

Troubleshooting authentication#

It is difficult for RStudio Support to troubleshoot as it depends on external configuration values unknown to us.

Commands

Does it work outside of the RStudio product?

  • Often possible to copy over working setup from other systems or services.
  • For RStudio Server Pro, thepamtester utility lets you test outside RStudio systems.

Check server log files including access.log files.

Check connections to any external authentication providers

  • Kerberos, Active Directory, etc.

General Linux tips#

Operating system version#

What OS are you using?

or

R Program Cheat Sheet

Export environment variable to PATH#

Cheat Sheet For R

Cheat sheet for r

R Studio Commands Cheat Sheet

Persist changes on startup in .bashrc#

R Cheat Sheets Pdf

You can run scripts and define variables on startup by adding lines to your .bashrc file: