# Before We Start

### What is R?

R is a versatile, open source programming/scripting language available on all platforms that is useful not only for statistics, but also data science. It is inspired by the programming language S.

R is open-source software licensed under the GNU General Public License (GPL). It is superior (if not just comparable) to commercial alternatives, and is widely used both in academia and industry.

### What is RStudio?

RStudio is a freely available open-source Integrated Development Environment (IDE). It can be downloaded from the [RStudio webite](https://www.rstudio.com/products/rstudio/download). It is a great alternative to working on R in the terminal for many reasons:

* automatic syntax highlighting/formatting in the editor&#x20;
* direct code execution from editor to console
* real-time access to environment, plotting, and history&#x20;
* good tool for workspace management

The RStudio interface has four main panels:

* Console: where you can type commands and see output
* Editor: where you can type out commands and save to file. You can also run in console with Ctrl Enter
* Workspace/History: workspace shows all active objects and history keeps track of all commands run in console
* Files/Plots/Packages/Help

### Best practices

1. Code and workflow are more reproducible if we can document everything that we do.
2. Our end goal is not just to “do stuff”, but to do it in a way that anyone can easily and exactly replicate our workflow and results.
3. All code should be written in the editor and saved to file, rather than working in the console. The R console should be used to inspect objects, test a function or get help.
4. Use `#` signs to comment. Comment liberally in your R scripts. This will help future you and other collaborators know what each line of code (or code block) was meant to do. Anything to the right of a # is ignored by R. A shortcut for this is Ctrl + Shift + C if you want to comment an entire chunk of text.
5. Organizing your working directory: You should separate the original data (raw data) from intermediate datasets that you may create for the need of a particular analysis. For instance, you may want to create a data/ directory within your working directory that stores the data, and have a results/ directory for intermediate datasets and a figures/ directory for the plots you will generate. The current working directory has a data folder already. Create additional directories called "results" and "figures" respectively. You can do this by navigating to the lower right panel and using the New Folder button in the file tab

### Interacting with R

There are two main ways of interacting with R: using the console or by using script files (plain text files that contain your code).

The console window (in RStudio, the bottom left panel) is the place where R is waiting for you to tell it what to do, and where it will show the results of a command. You can type commands directly into the console, but they will be forgotten when you close the session. It is better to enter the commands in the script editor, and save the script. This way, you have a complete record of what you did, you can easily show others how you did it and you can do it again later on if needed. You can copy-paste the code into the R console, but the Rstudio script editor allows you to ‘send’ the current line or the currently selected text to the R console using the `Ctrl-Enter` shortcut.

### Command prompt

If R is ready to accept commands, the R console shows a > prompt. If it receives a command (by typing, copy-pasting or sent from the script editor using `Ctrl-Enter`), R will try to execute it, and when ready, show the results and come back with a new > prompt to wait for new commands.

If R is still waiting for you to enter more data because it isn’t complete yet, the console will show a + prompt. It means that you haven’t finished entering a complete command. This is because you have not ‘closed’ a parenthesis or quotation. If you’re in Rstudio and this happens, click inside the console window and press `Ctrl+c`; this should help you out of trouble.

### More powerful R computing for genomic analyses

Genomic data can be too big to run on your laptop. BCC/BMC cluster has lots of memory and CPU which enables processing big data in a parallel fashion. However, you need to be trained by our course [Introduction to Unix and KI Computational Resources](/mini-courses/introduction-to-unix-and-ki-computational-resources.md) in order to run R scripts on our cluster in appropriate ways.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://igb.mit.edu/mini-courses/r/intro-to-r/before-we-start.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
