Portable environment ¶
This page will guide you on installing R and RStudio in a portable environment on a Windows 10 system. Following are the reasons why I prefer a portable installation over a regular installation:
- I am not tied up to a particular computer. Installation and files reside in a portable drive (a pen-drive or a portable hard disk drive). I can use R on any Windows system wherever I go.
- If this system crashes, I don't lose my setup or files.
- I like experimenting.
If you prefer a regular installation, visit RStudio and follow the steps.
Installing PortablaApps platform (optional): Portable Apps platforms comes with it's own start menu launcher which is handly when you install multiple portable programs in future. Download and install PortableApps Platform from https://portableapps.com/download.
1. Select "New Install"
- Portable apps if you want to install it on your pen-drive or a portable hard disk drive
- Cloud, if you want to intall it in your Dropbox, Google Drive, PCloud, One Drive, etc
- Local - It will be installed in your local drive but only you can access the programs. Other Windows users on your system will not be able to access your portable applications. This is where I am installing.
- Local All Users - It will be installed in your local drive and all users on your computer can access them.
3. Select your preferred directory and continue.
Once the installation is complete, open Portable Apps platform and if everthing went well, you'll see something like this.
Installing R ¶
1. Download R Portable paf.exe file from https://sourceforge.net/projects/rportable/.
2. Open PortableApps Menu and go to Apps > Install a New App
3. Select the R Portable paf.exe file you downloaded earlier and continue installation with default settings.
4. Once installation is complete, you will be able to see R in your Portable Apps menu. Click on it and open R console.
5. Updating R packages: In R Console menu, go to Packages > Update Packages
6. Select the CRAN mirror location nearest to you.
7. If there are any packages that need to be updated, you'll see a small window with a list of apps selected. Click 'OK' and update them.
8. Close R console. There is no need to save the workspace image.
Installing RStudio ¶
RStudio is provides the GUI(Graphics User Interface) and is also the commonly used IDE(Integrated Development Environment) for R.
1. Go to https://sourceforge.net/projects/rportable/files/R-Studio/ and select the latest version of RStudio. Download the paf.exe file from the folder.
2. Open PortableApps Menu and go to Apps > Install a New App
3. Select the RStudio Portable paf.exe file you downloaded earlier and continue installation with default settings.
4. Once installation is complete, you will be able to see RStudioPortable in your Portable Apps menu. Click on it and open R Studio.
5. The first time you open R Studio, it will ask you to chose the version of R you want to use.
6. Click "Browse" and point to the "bin" directory of portable R you installed earlier. The path looks similar to C:\Users\YourUserName\PortableApps\R-Portable\App\R-Portable\bin.
7. Select 32-bit or 64-bit based on your Windows 10 version. If you are unsure, select 32-bit as it works on both.
You are now ready to use R Studio.
8.\ Create R Project. Select File > New Project. Select "New Directory".
Select "New Project"
Name your project (example: DataScienceWithR). Browse and select the directory where you want the project to reside. Click "Create Project".
You should now see your project files including "DataScienceWithR.Rproj" file in the second quadrant of R Studio.
Installing R Markdown ¶
As a data scientist, it's important to not only write and run code but also explain data manipulation and inferences in words. Markdown allows us to document our work. R-Markdown integrates R code with Markdown to provide an integrated solution. JupyterLab (a successor of iPython) is another such tool, which we will come across soon.
1.\ In R Studio, go to File > New File > R Markdown.
2.\ If there are any missing packages, R Studio will ask you if you'd like to install them. Click 'OK' and wait for it to install.
3.\ Give a name to the document (example: sample). Select 'HTML' or 'PDF' as your choice of output.
4.\ This will create a sample markdown document with some examples in it. You may change the 'title' in the document to "Sample Markdown". Save the file.
5. Press CTRL+SHIFT+K or click the "Knit" button in the fourth quadrant of R Studio or go to File > Knit Document to generate an HTML or PDF output of the markdown file.
Installing R in JupyterLab (Optional)¶
This step is completely optional and you can safely skip it. JupyterLab is a web-based interactive development environment for Jupyter notebooks, code, and data. It supports over 40 programming languages including R. I started using JupyterLab a short time ago while learning Python and am quite impressed by its features, interactivity, improvements and its community. This is my attempt to run R in JupyterLab environment.
Follow the steps to install R in JupyterLab. If you already have JupyterLab installed, you can directly go to step x.
1. Anaconda vs Miniconda: To use JupyterLab, you need to have Anaconda or Miniconda installed. Follow either 1a or 1b.
1a. Anaconda comes bundled with Python and a lot of packages commonly used in data science. It also comes with a GUI called Navigator and it's own IDE called Spyder. If you are a beginner, install Anaconda from https://www.anaconda.com/distribution/. Select Python 3+ version. Install will default settings.
1b. Miniconda is a small, bootstrap version of Anaconda that includes only conda, Python, the packages they depend on, and a small number of other useful packages. I prefer Miniconda instead of Anaconda as I don't need Anaconda's GUI and IDE and I am more comfortable with using CLI(Command Line Interface) for installing and maintaining packages. Also, I don't need all the packages that come with Anaconda. I can install the packages I want when I need them. Download Miniconda from https://docs.conda.io/en/latest/miniconda.html. You can install with default settings, or if you prefer a portable version of it, open the command prompt in administrator mode and go to the directory where you downloaded Minoconda exe file and type the following
Miniconda3-latest-Windows-x86_64.exe /InstallationType=JustMe /AddToPath=0 /RegisterPython=0 /NoRegistry=1
2. Creating a virtual environment: In Anaconda(or Miniconda. In future, I am going to use the term Anaconda for both Anaconda and Miniconda because they both have the same core. Whatever works in one, works in the other too.), we have an option to create virtual environments. Each environment can have packages and code specific to its project. This is useful because different projects require different packages and it's not advisable to install all packages at one place in the base(base is the default environment that comes with Anaconda). Also, if you don't want to mess up the base when you are experimenting. Always create a new environment, experiment and if something goes wrong then delete it and create a new one without having to reinstall Anaconda.
2a. Open 'Anaconda Prompt' from Windows Start Menu
2b. Create a virtual environment. Enter the following in the command prompt.
conda create --name jhu.
I created an environment called 'jhu'. The name is arbitrary. You can name it anything you want.
2c. Activate the virtual environment
conda activate jhu
Replace 'jhu' with your environment name. You should see the name of the environment in brackets on the left of the prompt.
3. Install JupyterLab
conda install -c conda-forge jupyterlab
This will show a list of dependency packages to be installed. Press 'y' and continue.
4. Install R in JupyterLab
conda install -c r r-essentials
This will install R along with essential packages to use R in JupyterLab. Now open JupyterLab by typing the following
If everywthing went well, you should see JupyterLab launcher with R installed alongside Python.
Let's run a small piece of R code that I copied from here and see if it works.
library(dplyr) library(ggplot2) ggplot(data=iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) + geom_point(size=3)
Last updated 2020-04-11 16:47:25.910599 IST