Skip to content

Commit

Permalink
Add a bit of content... Just some friendly notes.
Browse files Browse the repository at this point in the history
  • Loading branch information
coatless committed Jan 11, 2024
1 parent b616aab commit 90695eb
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 51 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,9 @@ vignettes/*.pdf

# R Environment Variables
.Renviron

/.quarto/

/.luarc.json

*_book/
26 changes: 3 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# quarto-book-template
Template repository for creating a book powered by Quarto and Rendered by GitHub Actions onto GitHub Pages
# r-reference

The repository holds a reference book of _R_ related programming topics and ideas. This primarily serves as a way for the author to quickly catalog and store code snippets in the vein of a personal knowledge management (PKM).

## Overview

Expand All @@ -8,27 +9,6 @@ The repository holds:
- [`.github/workflows/quarto-render.yml`](.github/workflows/quarto-render.yml): Install, setup, and render a Quarto book using R and Python
- [`_quarto.yml`](_quarto.yml): Setup the properties of the book in a minimal fashion (for more options see [Quarto: Book Structure](https://quarto.org/docs/books/book-structure.html))
- [`index.qmd`](index.qmd): Welcome page

Additional files:

- [`requirements.txt`](requirements.txt): List of Python Packages to install
- [`DESCRIPTION`](DESCRIPTION): List of R Packages using the standard DESCRIPTION file to install with `pak`.

## Publishing with GitHub Actions

Included in the repository is a custom GitHub Action that will automatically render and deploy the book onto GitHub Pages.
Before the first run of the GitHub Action, please make sure to use locally in terminal the following:

```sh
quarto publish gh-pages
```

This command [initializes the `gh-pages` branch and turns on GitHub Pages for the repository](https://quarto.org/docs/publishing/github-pages.html#source-branch).

If you do not run this command before the first GitHub Action is triggered, you will likely encounter the following error message in the build log:

```sh
ERROR: No _publish.yml file available (_publish.yml specifying a destination required for non-interactive publish)
```

To avoid this issue, please make sure to run the GitHub Action locally so that GitHub can render and publish your Quarto document after every push to the repository.
22 changes: 17 additions & 5 deletions _quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,37 @@ project:
output-dir: _book

book:
title: "Title of Next Bestseller"
title: "R Reference"
author: "JJB"
description: |
TBA
#cover-image: images/cover.png
site-url: https://tutorials.thecoatlessprofessor.com/quarto-book-template
repo-url: https://github.com/coatless-tutorials/quarto-book-template
site-url: https://textbooks.thecoatlessprofessor.com/r-reference
repo-url: https://github.com/coatless-textbooks/r-reference
repo-branch: main
repo-actions: [edit]
sharing: [twitter, facebook, linkedin]
chapters:
- index.qmd
- part: my-first-book.qmd
- part: "Graphics"
chapters:
- hello-world.qmd
- base-r-graphics.qmd
- part: "Configuration"
chapters:
- r-environment-variables.qmd
- part: "Parallelization"
- part: "Compiled Code"
appendices:
- appendix-notes.qmd

format:
html:
theme: cosmo
code-link: true

# Register Adsense plugin to appear on all pages
adsense:
publisher-id: ca-pub-4979996562647159

filters:
- adsense
68 changes: 68 additions & 0 deletions base-r-graphics.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Base R Graphics


## Customizing Plot Appearance

### Adjusting Axis Labels and Titles

```{r}
# Example 1
x <- 1:10
y <- x^2
plot(x, y, main = "Quadratic Function", xlab = "X", ylab = "Y")
# Example 2
df <- data.frame(Time = 1:10, Value = cumsum(rnorm(10)))
plot(df$Time, df$Value, main = "Cumulative Sum", xlab = "Time", ylab = "Cumulative Value")
```

### Changing Line Types, Colors, and Symbols

```{r}
# Example 1
x <- 1:5
y1 <- x
y2 <- 2 * x
y3 <- 3 * x
plot(x, y1, type="b", col="red", pch=16, lty=1, ylim=c(0, 15), main="Multiple Lines Example")
points(x, y2, col="blue", pch=17, lty=2)
lines(x, y3, col="green", lty=3)
# Example 2
set.seed(123)
x <- rnorm(50)
y <- x + rnorm(50, mean=2)
plot(x, y, col="purple", pch=19, main="Scatter Plot with Customized Symbols", xlab="X", ylab="Y")
```

```{r}
# Create data
x <- 1:10
y <- 1:10
# Line Types
par(mar=c(4, 4, 2, 2))
plot(x, y, type="n", main="All Line Types", xlab="X", ylab="Y", ylim=c(0, 6))
for (i in 0:5) {
lines(x, rep(i*.5, length(x)), lty=i, col="black", lwd=2)
}
legend("topright", legend=0:5, title="Line Types", lty=0:5, col="black", lwd=2, ncol=3)
```

```{r}
# Colors
par(mar=c(4, 4, 2, 2))
plot(x, y, type="n", main="All Colors", xlab="X", ylab="Y", ylim=c(0, 9))
for (i in 1:8) {
lines(x, rep(i*.5, length(x)), col=i, lwd=2)
}
legend("topright", legend=1:8, title="Colors", col=1:8, lwd=2, ncol=2)
```

```{r}
# symbols
# todo
```
3 changes: 0 additions & 3 deletions hello-world.qmd

This file was deleted.

21 changes: 1 addition & 20 deletions index.qmd
Original file line number Diff line number Diff line change
@@ -1,23 +1,4 @@


## Welcome {.unnumbered}

Welcome splash page! Provide an overview of the book.

Sample evaluation of R and Python in a side-by-side manner:

::: {layout-ncol=2}
#### R

```{r}
my_list = list(1, 2, 3)
typeof(my_list)
```

#### Python

```{python}
my_list = [1, 2, 3]
type(my_list)
```
:::
Hello there! You've stumbled across a reference guide that contains different useful R code and/or links. There isn't a lot of depth associated with each page if you are expecting a through walkthrough of the material.
66 changes: 66 additions & 0 deletions r-environment-variables.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# R Environment Variables

Setting up and using environment variables in R is useful for handling configuration
details and/or suppressing private information.

## Retrieve/access environment variables:

Values stored in the system environment can be retrieved using `Sys.getenv("VAR_NAME")`.

```{r}
# Example: Accessing environment variable named API_KEY
api_key <- Sys.getenv("API_KEY")
api_key
```

In the case where the value might not be found, make sure to specify an **unset** value that acts as a default.

```{r}
# Example: Accessing environment variable named API_KEY
api_key <- Sys.getenv("API_KEY", unset = NA)
api_key
```

Note, the `Sys.getenv()` function only returns a `character` value.

## Set environment variables in your R environment

Environment variables can be set in R by using `Sys.setenv()`.

```{r}
Sys.setenv(API_KEY = "your_api_key")
```

## Use an `.Renviron` file for configuration:

Frequently using environment variables? Instead of defining them for each script, aim to store
environment variables in a `.Renviron` file in the project directory.

Variables are specified in the `.Renviron` file with the format `VAR_NAME=value`.

:::callout-note
Avoid using spaces around the `=` sign.
:::

```ini
# Example .Renviron file
API_KEY=your_api_key
```

## Access `.Renviron` from R using `browseURL()` or `usethis` package

The `.Renviron` file may be accessed using either `browseURL("~/.Renviron")` or with `usethis::edit_r_environ()` for editing.

```{r}
#| eval: false
# Example: Load .Renviron file with browseURL()
utils::browseURL("~/.Renviron")
```

```{r}
#| eval: false
# Example: Load .Renviron file with the usethis package
usethis::edit_r_environ()
```

### TODO: mention `dotenv` for managing environment variables?
8 changes: 8 additions & 0 deletions r-reference.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"folders": [
{
"path": "."
}
],
"settings": {}
}

0 comments on commit 90695eb

Please sign in to comment.