An R-project stores everything to do with each lab in one place on your computer.
This is incredibly useful - it means that if you switch from R-Cloud, to the lab computers, to your laptop, all you have to do is to move the folder and everything will just work. It also means you have to worry less about directory names.

New Project. New Directory, then New Project.
Create Project, 
R will change slightly.
- If you look at the top of the screen in the title bar, it should say
Lab1-Project R Studio.
- The Files tab should have gone to your project folder. Essentially,
R-Studio is now “looking” inside your Lab 1 folder, making it easier to
find your data and output your results.


.RProj.Visit the Posit Cloud Login Page:

Create a New Project:
Click the button on the top right and select “R-studio project.”
A new R-studio Cloud instance will open. Rename it to match your lab.

Just as you don’t need to go to the app store every time you use your weather app, you only need to download and install packages once. There are two methods
install.packages() command with
quotes around the package name
e.g. install.packages("bardr").Try installing the bardr package onto your computer
Installing a package doesn’t make it available to you. For that you
need to load it (like clicking on an app). This can be done with the
library() command.
In the console type this to install the full works of Shakespeare in the bardr package (https://www.rdocumentation.org/packages/bardr/versions/0.0.9)
library(bardr)
If you have managed to install a package successfully, often nothing happens - this is great! It means it loaded the package without errors.
Otherwise, I suggest running this command TWICE! This is because loading packages will print “friendly messages” or “welcome text” the first time you load them.
For example, this is what shows up when you install the tidyverse package. The welcome text is indicating the sub-packages that tidyverse downloaded and also that some commands now have a different meaning.
Tidyverse install messages
To find out if what you are seeing is a friendly message or an error, run the command again. If you run it a second time and there is no error then nothing should happen.
You can also use any command from any package by naming it and using ::
For example, this command forces the computer to use the dplyr package version of filter.
dplyr::filter(mydata)
R Markdown is a powerful tool that allows you to combine text, code, and outputs in a single document. This tutorial will guide you through creating an R Markdown document, using templates, understanding its components, and working with key features like visual mode, code chunks, and knitting.
There are also many more hints and tips here https://posit.co/blog/r-markdown-tips-tricks-1-rstudio-ide/.
Read more here: https://rmarkdown.rstudio.com
Typing commands into the console is like making a phone call to the computer—you communicate but don’t keep a record of your interactions. To save your commands for future use, we use scripts and documents. There are several types:
A basic script (.R file): A blank
notepad where you save code commands. When you “run” the script, R
simply copies and pastes the commands into the console.
An R Notebook or R Markdown document
(.Rmd file): An interactive document where you can combine
text, code, and output in one place. With a single click, you can
convert .Rmd files into reports, websites, blogs,
presentations, or interactive applications.
R Markdown is especially useful for labs because it allows you to integrate code, output, and written analysis seamlessly. Instead of taking screenshots, you can embed results directly in your document.

Your markdown file contains three main areas:
The YAML code at the top describes what the final output should look like.
For example this might be a website, a pdf, a presentation or one of these 21 other formats…https://rmarkdown.rstudio.com/lesson-9.html
It also includes other ‘meta-data’ for example the title/author name, whether you want a table of contents and any themes. For example, in the screenshot above, I included:
See “Editing YAML” below for more information.
The report text.
Code chunks
To create a new R Markdown document

File → New File →
R Markdown...HTML.OKRStudio will generate a default R Markdown file with an example of formatted text and an embedded code chunk.
There are many different options you can add to a basic RmD file, by changing the YAML code at the top. For example you can add a table of contents, make the auto-hide (code folding) or change the theme.
Click here to see examples of how to do this: https://psu-spatial.github.io/Stat462-2025/CH2_EACHLAB.html#34_Editing_YAML
Before you continue, on your computer (outside R-Studio), go to your project folder
MAKE SURE THE RMD FILE SAVED IN YOUR PROJECT FOLDER. Also, check you didn’t accidentally create a load of sub-folders inside your lab. If so, delete/move things around until it looks like this.

There are also now several package which contain professional markdown templates. Common examples include
rmdformats package: See example themes here: https://github.com/juba/rmdformatsprettydoc package: See example themes here: https://prettydoc.statr.me/themes.htmlTo use one of these packages:
Alternatively, you can replace your YAML code with the suggested code from that theme. |
![]() |

There are many different options you can add to a basic RmD file, by changing the YAML code at the top. For example you can add a table of contents, make the auto-hide (code folding) or change the theme.
The easiest way to see what is available for your particular theme is to go to the package website and take a look at the documentation (scroll down). For example
rmdformats package has various options for each
theme https://github.com/juba/rmdformatsprettydoc package has more description
here https://prettydoc.statr.me/themes.htmlYou can also just try and edit the YAML code, press knit and see if it works. Click here to see examples of how to do this: https://psu-spatial.github.io/Stat462-2025/CH2_EACHLAB.html#34_Editing_YAML
NOTE, PrettyDocs does NOT allow a floating table of contents.
Sometimes rmdformats makes an extra subfolder and puts your report in there.
So before you continue, on your computer (outside R-Studio), go to your project folder
MAKE SURE THE RMD FILE SAVED IN YOUR PROJECT FOLDER. Also, check you didn’t accidentally create a load of sub-folders inside your lab. If so, delete/move things around until it looks like this.

Finally, we can now use ChatGPT to help make custom YAML code for whatever template you want or to help you fix broken themes/YAML code. It will almost certainly take interations - and look at github or google cool markdown formats for examples.
For example,
How to make a template that includes a logo/header, and a discussion about what styles.css is https://chatgpt.com/share/67e44d44-8254-800f-a2bf-b447464a4f4a
A silly template for a PI who likes penguins using a new google font https://chatgpt.com/share/67e44f17-a760-800f-850e-479c63776883
R-Markdown is especially powerful because it is easy to convert your reports into many different templates. We do this via changing the YAML code at the top. Luckily, you do not have to remember all the YAML commands yourself.
The basic RmD format is pretty boring. So if you go this route, it’s good to edit the YAML code to add in a few additional features. Here is some example YAML code that I often use as a baseline. CAREFULLY replace yours with this, paying attention to spacing.
---
title: "change to add your title"
author: "hlg5155 or your email ID"
date: "`r Sys.Date()`"
output:
html_document:
number_sections: true
toc: true
toc_float: true
toc_depth: 3
theme: journal
---
There are many more details about different options on these websites:
There are about 10 built-in themes in R to quickly change the format of your documents. You view them here, alongside ways to further customize your document (for example to add a table of contents)
If you used a custom theme from rmdformats, check out their websites to see if there are additional options you wish to add in.
As I described in the previous section, you can also use other themes
from packages like rmdformats, but rather than just
changing the word “theme”, the entire YAML changes.
So the easiest way to do this is to create a blank new file for the template you want, then copy/paste the entire YAML code across to your report.
RStudio’s standard output is “source mode” where you see your text in a raw format.
RStudio now provides a Visual Editor to simplify text formatting. When switched on, you see something similar to the final output format and get access to all the normal word processing buttons (e.g. bold/headings etc.). To switch between the two modes:
Click the Visual tab in the .Rmd
editor.
Use the toolbar to format text, add links, or insert images.
Switch back to Source mode to view the underlying Markdown format.

Visual mode is incredibly useful, because you don’t need to remember any of the keyboard commands for formatting text. But… it can be a pain when editing/deleting code-chunks. To do that, I often switch back to source mode.

These are easy to add using visual mode. Just go to the text-editing menu at the top of the report.
Please use these! They allow your table of contents to be created correctly which allows easy navigation in your reports. For example, Heading 1 = Chapter Title, Heading 2 = sub-title etc.
In visual mode, click on the line you want then look at the text formatting menu. Click the arrow by “normal” and change to the heading you want.
THESE GO INTO THE TEXT SECTION! NOT INTO A CODE CHUNK.
Although I have heard rumors you can copy/paste equations into R, the easiest way I have found is to:
First, get the equation you want in chatgpt or similar and ask it to output as LateX format
Type $$ to tell R you are adding an equation, then paste the equation in a line of its own.
e.g. If you type this into the TEXT (not a code chunk)
$$
E_n = -\frac{m e^4}{2 \hbar^2} \frac{1}{n^2}
$$
In visual mode, you should see get
\[ E_n = -\frac{m e^4}{2 \hbar^2} \frac{1}{n^2} \]
If you want an equation on the same line, use single $ signs. E.g typing this into the TEXT
Hello! Here is an inline equation: $e^4$, that you can include mid-sentence
Will give you
Hello! Here is an inline equation: \(e^4\), that you can include mid-sentence.
In visual mode, you can add code chunks by clicking on the little green button at the top right and choosing R.

In source mode, you can add a code chunk by typing this

Where the little symbols before the {R} are called BACK-TICKS. I often just copy/replace.
There are several ways you can run the code in a code chunk. The easiest is to use the controls on each chunk.
To run that chunk → Click the green “Play” (▶) button in the top-right corner of the chunk.
To Run all chunks above → Click the small downwards arrow with line under it just to the left of the Play button.

Or via the Run menu on the top right.
Code chunk options control how your code behaves when you knit the document. You can suppress messages, hide warnings, or customize the appearance of your output. Here are some common options you might use:
message = TRUE/FALSE — Show or hide R messages when
knitting.warning = TRUE/FALSE — Show or hide warnings. Errors
will still be displayed.include = TRUE/FALSE — Show or hide both the code and
its output.echo = TRUE/FALSE — Show or hide the code but still
display its output.eval = TRUE/FALSE — Show the code, but don’t actually
run it. Useful if you can’t make it work, but want to still knit and
submit.{r inside the curly braces indicates it’s R code.{r, you can add options to modify the behavior of
the code when knitting. For example, use message=FALSE and
warning=FALSE to suppress library loading messages and
warnings.
In View mode, click the small cogwheel next to the run triangle at the top of the code chunk to set these options without remembering commands.

To apply settings to all code chunks, place this code chunk at the top of your document (below the YAML):
```{}
knitr::opts_chunk$set(echo=TRUE,
warning=FALSE,
message=FALSE)
```
Many templates include default settings, but you can easily customize them to fit your needs.
The file on your screen isn’t the finished article. To see how it will look as a final version, we need to “knit” it.
Knitting an R Markdown document means converting it into a final output format (such as HTML, PDF, or Word). When you knit a document, RStudio executes the code chunks, formats the Markdown text, and generates a nicely formatted report.
Go to the top of the .Rmd file, find the knit button.
Press it (you might have to first save your script if you haven’t
already, then press it again). This will only work if you have all the
commands saved in your lab-script e.g. if you read in the data but
didn’t write the command into your report, it will crash.

The terminal text will update you as your report turns into a website. If you get an error, then the exact line number with the problem and a description of the error will be printed on the console. For example, here I tried to print out a variable that didn’t exist.
