preloader

How does R work?

blog-image

Before delving into data and Data structures in R, let’s know, configure and take into account RStudio environment generalities


Working directory configuration

To work calmly on our projects we must always establish an order, this will give us skills to work more effectively.

One simplest and least named steps, it could help us in our projects in RStudio, it’s to configure our directory. Working directory (wd) is a folder (on our computer) that RStudio uses to read or write files. To know address folder (By default), we have to type next function in console getwd().

Attention attention!!!

A tip to facilitate your learning: The vast majority of R code syntax is related to function they are going to fulfill. For example, getwd() is get working directory.
getwd()
## [1] "C:/Users/david/Desktop/Rbiologos/content/english/blog/A004"

So, you can see that my directory path is set to work on a folder in my desktop. But if I need to work in a specific project folder, I can change this address with command setwd(), specifying folder address in question, as in next example

setwd(C:/Users/david/Desktop/Proyecto)

Likewise, if we want to know all files that are in our directory, we can execute list.files() function. This function will help you if you don’t remember what files you have in your R project … Those mental lapses in life of every biologist!


But wait … we advise you to configure your directory permanently to be able to streamline and organize your projects/data in best way (I have it on desktop folder because it’s where I have everything at hand). In order to do this, in application we are going to go to Tools tab and then we are going to Global Options….

So we will see it this screen

Options


In this panel we will have several options that you can modify such as RStudio appearance, font size, work panels order, among other. In our case, in General option, we will go to Default working directory label, we will click on Browse and it will open a window in which we will choose the folder that we want to configure as default directory.


Packages

Configured already our directory, the next step is to know that R is composed for a base system, this means that it has functions already installed to be able to perform simple tasks, however, there are multiple functions and tasks in R world. To be able to extend this functionality, R can install packages that have a functions collection predetermined by their developer.

So, there are several ways to install these packages. The most common and simple is through install.packages() code, as shown in next code

install.packages("lme4")

Or, we can install several packages at same time with this code:

install.packages(c("dplyr","ggplot2","lme4"))

One RStudio advantages is that when you load a script (in this same post we will talk about these), the program will automatically read packages not installed and will ask if you want to install them, as we can see in this image

library


This installation is necessary only once, this is because package will be incorporated into base information on our computer.

All we have to do is “call” at package. We carry out this task when we have to use any of its functions, the code to execute this command will be library(), as seen in next code

library(lme4) 
# Note that we no longer need name to be in quotes

Yes, it’s very possible that you find yourself in a dilemma, and you don’t know how package works, because your teacher told you, to work with this thing you need this package, nothing more. And in this moment the biologist begins to suffer.


But don’t worry …! Nothing is lost and R developers have arranged that packages have necessary information to help users in this complex process. To access this help we type in console ? with the package name, for example ?lme4 (Note: that to access this information it’s necessary that package be loaded in R), as we see in this example :

And help tab will open and will show information as in next image:

Help


But… all information will not always be so clear, it’s a reality. For this, several people have created pages or blogs dedicated to solving any doubts in R environment. The most sought after and used are these:

rbloggers
R-bloggers


stackover
Stackoverflow


We invite you to explore them, they have very good information, with discussion forums, mostly in English


Functions

With our packages installed we can work! Each packages has a number of functions that have a specific task to perform.

Thus, a function is a operations series to which a name has been assigned, these functions require specific arguments (values) to perform their task, if we don’t enter them properly, function command will generate error, the blessed error…


It is essential to know arguments that we need to enter each function!

To know arguments we can consult packages’ help, as we saw earlier, for example: ?lm(). Another way is to type the function, lm(), and inside parentheses we type TAB, this will display arguments required by function in question and if we navigate between them, each one will give us a brief explanation about himself.

Argumentos
Required arguments view for lm() function



Scripts

To finish our topic, we are going to talk about Scripts, but… What is a script?

A script is an R document whose purpose is to save codes that we create important for a project, it is something like a cooking recipe. Once you have all your codes saved, it’s only necessary to open script to have them at hand and to be able to use or modify them.

To create a new one, you just need to go to File -> New File -> R Script or with shortcut Ctrl + Shift + N

Scripts are recognized because they have an .R file extension, for example project.R. These files are the same as any text document, but R can read them and execute code they contain.

script
Script editor view in RStudio


To run all codes within script you can click on Run box or more simply Ctrl + Enter for every code. But, there is too a special function that can save you a little time source(), with this function each code that our script contains will be executed. For example, in our case the code would be source(Plots.R).

One advice, to work with your codes, directly from script editor and NOT in console. But why would it be better? Answer is simple, nothing better than not losing sight of codes that have worked for you, or editing code quickly to correct an error, in console you would have to be writing complete code, and if it is long, well, it becomes tedious.

In RStudio you can save editor contents frequently, and Rstudio will load them automatically when you reopen them.


In summary

Today, First, we were able to learn or remember to identify and configure our work site; We hope that with this step, your work in R will become much easier, we believe that doing things more easily frees us from simple tasks, to be able to concentrate more on what is really worthwhile.

Likewise, we learned about packages use, how to install them and how to use them. Keep in mind that there are currently 17258 packages available for R and that number continues to grow, each with a special functions collection and each function with its specific arguments, so don’t stress about not knowing everything about them. We are learning and we are going step by step, soft, soft, soft!


And finally, we talked about scripts. Important files to be able to save our codes and have them always at hand.


Finally

We invite you to continue investigating more on subject, we also want to know about your experience in R and RStudio and what doubts we can help you solve. Thank you very much for your time, we hope you have learned something new, it is important for us to contribute something to humanity learning!

We wait for you in our next post, which is loaded with many more things. We will tell you everything!

Information R

Vicente Coll & Pedro J. Pérez.Universidad de Valencia; 
Andina M. (2018); Ahumada, J. A. (2003); 
R para Principiantes. University of Hawaii; Mendoza Vega (2014).