\(~~~~~~~~~\)RStudio and Quarto\(~~~~~~~~~\)

Somsak Chanaim

International College of Digital Innovation, Chiang Mai University

October 30, 2024

Why we use RStudio?

RStudio is an integrated development environment (IDE) for R that provides several features and tools to enhance the R programming experience. Here are some reasons why many users choose to use RStudio:

User-Friendly Interface and Integrated Tools

  • Intuitive Layout: RStudio has a well-organized interface.

  • Syntax Highlighting: It offers syntax highlighting, which helps in reading and writing code more easily.

  • Script Editor: You can write and edit R scripts, the features like code completion and error checking.

  • Console: The built-in console allows you to run R commands interactively.

  • Viewer Pane: You can visualize plots and interactive outputs within the IDE.

RStudio Interface

Set Up Your Styles

menu Tools \(\rightarrow\) Global Options...

FiraCode Font

Fira Code

Fira Code is a popular monospaced font designed for programming. Here are some key features and information about it:

Features of Fira Code:

  1. Ligatures: Fira Code includes programming ligatures, which are special combinations of characters that are rendered as single symbols (e.g., !=, ->, ==). This makes code more readable and visually appealing.

  2. Monospaced: As a monospaced font, each character takes up the same amount of horizontal space, which is ideal for aligning code and making it easier to read.

  3. Open Source: Fira Code is open-source and can be freely used and modified. It is available on platforms like GitHub.

  4. Cross-Platform: It can be used on various operating systems, including Windows, macOS, and Linux.

  5. Clear and Readable: The font is designed to be legible at different sizes, making it suitable for long coding sessions.

Quarto is an open-source, next-generation publishing system designed for creating dynamic documents,

  • reports

  • presentations

  • websites

  • dashboard

  • Shiny

  • books

  • ETC.

Allowing for a more flexible and powerful way to create documents that integrate code, text, tables, videos,and visualizations depending on the output.

Check your computer.

  1. You are install R program.

  2. You are install RStudio.

  3. You are install Quarto.

Warning

This interactive slide cannot run Quarto files (.qmd).

To start using Quarto with RStudio

Follow these steps:

  1. Open RStudio: Launch RStudio from your applications or programs menu.

  2. Create a New Quarto Document:

    • In RStudio, go to the top menu and select: File \(\rightarrow\) New File \(\rightarrow\) Quarto Document

    • This will create a new .qmd file where you can write your Quarto content.

Note

In this course, we will focus on creating HTML documents/reports with Quarto.

For the final project, you will learn how to create a dashboard with Quarto.

  1. Write Your Quarto Content: You can start writing your document using Markdown syntax, and include R code chunks where needed. Here’s a simple example:
---
title: "My First Quarto Document"
author: "Your name"
institute: "International College of Digital Innovation, CMU"
date: Oct 13, 2024
date-format: long
format: html
---

# Introduction

This is my first Quarto document.

```{r}
summary(cars)
```

Tip

Copy codes and put into your computer \(\rightarrow\) save file \(\rightarrow\) render.

Quarto Process

Quarto works the same way as R Markdown

After save file, we can render to recieve the output/report

Quarto anatomy

You can render a qmd file in RStudio into HTML, docx and pdf etc. The output based on your set up

Normally for each Quarto file is composed of 3 main components

  1. a YAML header

  2. formatted text

  3. one or more code chunks.

YAML header

YAML header is processed in many stages of the rendering process and can influence the final document in many different ways.

A typical YAML header looks like this, and contains basic metadata about the document and rendering instructions:

---
title: "RStudio and Quarto"
author: "Somsak Chanaim"
institute: "International College of Digital Innovation, CMU"
date: "30 Oct 2024"
date-format: long
format: html
theme: superhero
toc: true
---

YAML settings depend on the type of output. You can read more details on the Quarto website.

Formatted text

One of the great things about Quarto is that you don’t need to rely on your word processor to bring your R code, analysis and writing together. Quarto is able to render (almost) all of the text formatting that you are likely to need such as italics, bold, strike-through, super and subscript as well as bulleted and numbered lists, headers and footers, images, links to other documents or web pages and also equations.

Text format

Goal Quarto output
bold text **text** text
italic text *text* text
strikethrough ~~text~~ text
superscript text^2^ text2
subscript text~2~ text2

Headings

## section
### subsubsection
#### subsubsubsection
##### subsubsubsubsection

Try it in your Quarto

---
title: "RStudio and Quarto"
author: "Somsak Chanaim"
institute: "International College of Digital Innovation, CMU"
date: "30 Oct 2024"
date-format: long
format: html
theme: superhero
toc: true
---
## section
contents...

### subsubsection
contents...

#### subsubsubsection
contents...

##### subsubsubsubsection
contents...

Lists

If you want to create a bullet point list of text you can format an unordered list with sub items. Notice that the sub-items need to be indented.

Codes

 - item 1

 - item 2

    + sub-item 2

    + sub-item 3

 - item 3

 - item 4

Results

  • item 1

  • item 2

    • sub-item 2

    • sub-item 3

  • item 3

  • item 4

Tables

Codes

| Goal          | R markdown | output  |
|---------------|------------|---------|
| bold text     | \**text**   | **text**  |
| italic text   | \*text*     | *text*  |
| strikethrough | \~\~text~~   | ~~text~~  |
| superscript   | text\^2^    | text^2^ |
| subscript     | text\~2~    | text~2~ |

Results

Goal R markdown output
bold text **text** text
italic text *text* text
strikethrough ~~text~~ text
superscript text^2^ text2
subscript text~2~ text2

Add an image code

![put some text here](path-to-image-here)

Example

![Rstudio](Rstudio.png)

Equations

If your output contains mathematical symbols, you need to know \(\LaTeX\) commands.

Codes

 
$$ \cos^2(\theta) +\sin^2(\theta) =1 $$
 

Result

\[ \cos^2(\theta) +\sin^2(\theta) =1 \]

You can use the online service from many website, example hostmath

Code chunk

https://bookdown.org/yihui/rmarkdown-cookbook/ This part is about set up your R code, you can

  • changing our language engine

  • modifying chunk options, which can be global, local, or engine-specific;

  • set up outputs

how to use code chunk

In R markdown, you can put R code in this

```{r}
2+2
```

Results

[1] 4

Chunk Options

We will show some useful chuck options ans you must do it in your R markdown.

Hide source code:

```{r}
#| echo: false
1+1
```

Does not run this chuck

```{r}
#| eval: false
1+1
```

Setting the height and width of the graph

Without setting

Codes

```{r}
set.seed(1)
x <- rnorm(300)
hist(x)
```

Results

Setting by

fig-height: \<number\>

fig-width: \<number\>

Codes

```{r}
#| fig-height: 9
#| fig-width: 9
set.seed(1)
x <- rnorm(300)
hist(x)
```

Results

Add caption from plot

In the chuck code use command

fig-cap = "any caption word"

Codes

```{r}
#| fig-cap: 'The histogram'
set.seed(1)
x <- rnorm(300)
hist(x)
```

Results

The histogram

Chunk code setup with tool in RStudio

GUI

RStudio keyboard shortcuts

RStudio has keyboard shortcuts. A full list can be found under the menu Tools \(\rightarrow\) Keyboard Shortcuts Help. Some of the most useful shortcuts related to Markdown are summarized in Table

Task Windows & Linux macOS
Insert R chunk Ctrl+Alt+l Command+Option+l
Preview HTML Ctrl+Shift+K Command+Shift+K
Knitr document (knitr) Ctrl+Shift+K Command+Shift+K
Compile PDF Ctrl+Shift+K Command+Shift+K
Run all chunks above Ctrl+Alt+P Command+Option+P
Run current chunk Ctrl+Alt+C Command+Option+C
Run current chunk Ctrl+Shift+Enter Command+Shift+Enter
Run next chunk Ctrl+Alt+N Command+Option+N
Run all chunks Ctrl+Alt+R Command+Option+R
Go to next chunk/title Ctrl+PgDown Command+PgDown
Go to previous chunk/title Ctrl+PgUp Command+PgUp

Used result from chuck code or R

Example: If we have chuck code

a <- 1
b <- 3

We can use variable values to display or compute within the content.

Content

The value of a + b is \(^\backprime\)r a + b\(^\backprime\).

The value of a / b is \(^\backprime\)r a / b\(^\backprime\).

Result after render.

The value of a + b is 4.

The value of a / b is 0.3333333.

Some useful package: ymlthis

install.packages("ymlthis")

You will get the addins “Write new R markdown of YAML file

addins ymlthis

Create Awesome Table: DT

The goal of DT is to help you build interactive table.

install.packages("DT")

Example

Visual Markdown Editing menu in RStudio

Rstudio after version 1.4+or after the year 2021, we can use the Visual Markdown Editing menu to product quarto output.

The Visual Markdown Editing menu

Reference

[1] https://docs.posit.co/ide/user/

[2] https://www.quarto.org

[3] https://github.com/tonsky/FiraCode

[4] https://www.hostmath.com