Lesson · 40 min · Free
Intro to R: Console & Notebooks
Intro to R: Console & Notebooks R is the dominant open-source language for statistical genomics, and it is the engine behind widely used Bioconductor packages for RNA-seq differential expression, variant annotation,
Intro to R: Console & Notebooks
R is the dominant open-source language for statistical genomics, and it is the engine behind widely used Bioconductor packages for RNA-seq differential expression, variant annotation, and single-cell analysis. Before you can build pipelines, you need to be comfortable with the two primary ways of interacting with R: the console , an interactive read–evaluate–print loop (REPL) where each line is executed immediately, and the notebook , a literate-programming document (typically R Markdown, .Rmd ) that combines narrative text, executable code chunks, and rendered output in a single reproducible file. In RStudio, you can send a line of code from a script to the console with Ctrl + Enter (or Cmd + Enter ), which is the fastest way to explore a dataset interactively. The console is best understood as a scratchpad. Anything you assign there lives in the global environment for the current session, and objects vanish when the session ends unless you save them. This is ideal for quick sanity checks — inspecting a vector, testing a function argument, peeking at the first rows of a count matrix with head() — but it has a serious drawback for scientific work: a console-only analysis leaves no persistent record of the exact commands you ran. In bioinformatics, where a figure in a paper must be traceable back to a specific version of the data and code, that gap is unacceptable. Notebooks solve this by making the code itself the record. Each code chunk is delimited and can carry options that control its behavior: echo determines whether the code is shown in the rendered output, eval whether it is run, message and warning whether diagnostic output is displayed, and cache whether results are stored between renders. Because a notebook can be re-run from a clean environment top to bottom ( knitting ), it enforces a crucial discipline: if your notebook knits successfully from scratch, your analysis is reproducible. A useful habit is to treat the console as a testing ground and then promote every command that "worked" into a commented, ordered chunk in your notebook. The following example shows what a typical interactive console session looks like. Notice the > prompt for your input and the [1] prefix that R prints before output; the commands compute summary statistics for a small vector of gene expression values: > expr <- c(4.2, 7.8, 3.1, 9.5, 6.6) > mean(expr) [1] 6.24 > median(expr) [1] 6.6 > sd(expr) [1] 2.608267 > round(sd(expr), 2) [1] 2.61 > paste0("Mean log2 expression: ", mean(expr)) [1] "Mean log2 expression: 6.24" The same analysis, promoted into a notebook, looks like the chunk below. The first line is the chunk header: it declares the engine ( r ), gives the chunk a memorable label ( expr-stats ), and sets options. When the document is knitted, the code runs, the numeric results are printed where the chunk sits, and the commands are preserved in the source file for anyone reviewing your work: ```{r expr-stats, echo=TRUE, message=FALSE} # Summary statistics for log2 expression of five genes expr <- c(4.2, 7.8, 3.1, 9.5, 6.6) cat("Mean: ", mean(expr), "\n") cat("SD: ", round(sd(expr), 2), "\n") ``` The R console is an interactive REPL: great for exploration, but it leaves no permanent record of your commands. Objects created in the console live in the global environment and disappear when the session ends. R Markdown notebooks interleave prose and executable code chunks, producing a single reproducible document. Chunk options such as echo , eval , and cache control how each chunk runs and appears in the output. A notebook that knits successfully from a clean environment is a reproducible analysis; a console session is not. Standard workflow: experiment in the console, then move validated commands into labeled, commented chunks. Practice exercise: Open RStudio and, using only the console, create a vector named gc_content holding plausible GC percentages for five genes (e.g., 38.2, 51.7, 44.0, 60.1, 47.3), then compute its mean, standard deviation, and range using the built-in functions mean() , sd() , and range() . Once the commands work interactively, create a new R Markdown notebook, promote the same commands into a chunk labeled gc-stats with echo=TRUE , add one sentence of prose interpreting the mean GC content, and knit the document to HTML — confirming that you can reproduce the identical numbers from the console run.
Watch the full lesson — free
This topic is part of Bioinformatics & Computational Genomics, a complete AI-narrated video course. Press play once and watch the entire lecture like a movie.
Start the course free →