3 ways to install/load R packages.

https://kostaspateras.files.wordpress.com/2021/01/d1187-1w0ce6ulkqisxd5njoh7oba.jpeg

7 years ago I started wondering how to save time by automating routine procedures such as; setting up working/output/input directories, loading source files, setting up graph layouts and loading R packages when opening R/R studio. A couple of colleagues also had similar requests at the time. In this short tutorial I will focus on the latter. How to easily install/load R packages in R.

Like other programming languages, basic R contains a number of set functions that are installed when R is installed. Except for these base functions numerous others exist but they have be loaded together with their corresponding R package. An R package can be considered as a combination of functions and datasets created by R users. R is an open source statistical programming language and every R user can create functions, include them in an R package and upload them to CRAN.

To start using an R package, the package itself has to be installed in your computer. This can be done by install.packages(“Package_name”).

Basic 1st way to install/load R packages

This was my first try to install and load packages based on my undergraduate studies knowledge. This was could be very convenient when only a few packages have to be installed in an R session or when only one package has to be installed and loaded i.e. when a specific part of your code is initiated.

# Install packages 
install.packages("Rfast")
install.packages("MASS")
install.packages("mvtnorm")
install.packages("mice")
install.packages("lme4")
install.packages("xtable")
install.packages("lattice")
install.packages("gridBase")
install.packages("mvtnorm")
install.packages("RfagridExtrast")
install.packages("grid")
install.packages("latticeExtra")
install.packages("tmvtnorm")
install.packages("fitdistrplus")

# Load packages
load(Rfast)
load(MASS)
load(mvtnorm)
load(mice)
load(lme4)
load(xtable)
load(lattice)
load(gridBase)
load(mvtnorm)
load(RfagridExtrast)
load(grid)
load(latticeExtra)
load(tmvtnorm)
load(fitdistrplus)

Most flexible 2nd way to install/load R packages

This could be the most flexible solution to install/load R packages. All packages are stored in a vector called i.e. “packages.list”. Then, a routine checks which packages are not yet installed and create a filtered version of the “packages.list” vector.

# Save all packages names
packages.list <- c("Rfast","MASS","mvtnorm","mice","lme4","xtable","lattice","gridBase","mvtnorm","gridExtra","grid","latticeExtra","tmvtnorm","fitdistrplus")

# Install missing packages
packages.new <- packages.list[!(packages.list %in% installed.packages()[,"Package"])]
install.packages(packages.new);rm(packages.new)

# Load all selected packages
lapply(packages.list, require, character.only = TRUE); rm(packages.list)

# Update all downloaded packages 
update.packages()

Most prompt 3rd way to install/load R packages

A number of packages currently exist in R that can automated the above procedure, such as the pacman package. By using such packages, as all R packages the user should get familiarized with each command result. For example, the p_load command by has the option to automatically update packages to each latest versions.

# Install pacman package
install.packages("pacman"); load(pacman)

# Install and load packages p_load(Rfast,MASS,mvtnorm,mice,lme4,xtable,lattice,gridBase,mvtnorm,gridExtra,grid, latticeExtra,tmvtnorm,fitdistrplus)

# Unload package
p_unload(Rfast,MASS)

# Install packages from Github
p_install_gh("trinker/pacman")

If you have any suggestions on the guide above send me a message or comment below.

Picture link: https://kostaspateras.files.wordpress.com/2021/01/d1187-1w0ce6ulkqisxd5njoh7oba.jpeg

Posted in STArT | Tagged , , , , , | Leave a comment

How to upload R code on GitHub in 10 steps.

https://mk0whatcounts5gas29i.kinstacdn.com/wp-content/uploads/2016/02/sharing-is-caring.jpg

Background

Few months ago, I created a GitHub account during a data health hackathon. In this Greek hackathon we were asked to explore and suggest ways to utilize public health Greek data (it also happens that our team won 1st prize, our presentation in Greek can be found here). Then, we had to upload the code in GitHub to make it available to everyone. After uploading the initial code, I quit using the platform for a couple of years.

Recently I came back to GitHub by the need to create a personal website. Then, I tried to upload the R code for a project I was working on in R with Shiny. After I uploaded my code, I invited collaborators to work on it. Apparently GitHub gives you tremendous possibilities to share you code, collaborate with colleagues, gain feedback or provide feedback or fixes to existing repositories.

A repository is the basic place where a project is being uploaded in GitHub.

The following tutorial is based on the way I set GitHub in my main Windows 10 computer, though, I believe it could be interchangeable between other OSs.

A 10-step by step Guide

Requirements

The following two are at least required to move along this guide:

1. a GitHub account
2. an installed GitHub Desktop application in your computer

Steps

As this guide example I will be using the first Rshiny code I uploaded in GitHub for the Shiny implementation of the gamlss.demo package, a package the demonstrates smoothing and distributions of the gamlss family. Of course, you could sneak peek the repository on GitHub if you would like to see the current uploaded code before further following the tutorial.

The result application can be found uploaded in a Virtual machine of Google cloud server that I set earlier.

In 10 steps I am now presenting an easy (but probably not the only) way to start sharing R code in GitHub.

No 1. Visit https://github.com and create an account by setting the following. Validate your account and you are set.

No 2. The main github screen is the following. On the left your future repositories will appear. For now just click on the green right button “New”.

No 3. In this screen fill up the name of the new repository “gamlss_shiny”, choose if the repository will be public or private, then insert a readme file which will act as a description and finally add a license or not.

No 4. The next github screen will be an empty repository with title gamlss_shiny and maximum two files a readme.md and a licence file. The description of the readme.md file appears at the bottom of the repository. Files can be changed directly through this interface. If clicked they all give the chance to edit. New files can also be added via the Add file button. Folders can be created by the same Add file button by clicking Add file and then typing for example folder2/file2.txt you will create a text file named “file2” in the folder named “folder2”

No 5. Open the Github Desktop application on your computer. You will have to be connected with the same GitHub account credentials that you are using in GitHub website. Then, press the Clone repository and the following screen will appear. Find or search for the new repository, then click “Clone”. The first connection is already there!

No 6. You will see a new folder with the two files, as in step No 4.

No 7. In this step, drag and drop your R code files and folders in the new repository folder in the empty repository folder. All changes and additions will be noted in GitHub Desktop. At the lower right part of the application enter a name and optinally a description for the changes. For example in the first upload enter just Initial submit. In later alterations you can be more specific. Then, click on “commit to main”.

No 8. In this step you have to click the “Push origin” button and send the changes to GitHub. If all went well then in the Push Origin tab no arrows or notifications will appear.

No 9. You can check every repository’s history changes if you click on the History tab under Current repository.

No 10. All files are now added in GitHub repository. You can share the code by clicking the green Code button and sharing the url. In our example case the following url: https://github.com/kpatera/gamlss_shiny.git. To add code through the desktop application make sure you have first pulled all online code to avoid code conflicts.

The Online Rshiny application that corresponds to the above repository can be found here.

If you have any suggestions on the guide above send me a message or comment below.

Picture: https://www.whatcounts.com/sharing-is-caring/

Posted in STArT | Tagged , , , , , , | Leave a comment