diff --git a/.gitignore b/.gitignore index fae8299..04704f1 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,9 @@ vignettes/*.pdf # R Environment Variables .Renviron + +/.quarto/ + +/.luarc.json + +*_book/ \ No newline at end of file diff --git a/README.md b/README.md index a82ef35..1ea64b5 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. diff --git a/_quarto.yml b/_quarto.yml index 9a65cc9..b2d24b8 100644 --- a/_quarto.yml +++ b/_quarto.yml @@ -3,21 +3,26 @@ 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 @@ -25,3 +30,10 @@ format: html: theme: cosmo code-link: true + +# Register Adsense plugin to appear on all pages +adsense: + publisher-id: ca-pub-4979996562647159 + +filters: +- adsense \ No newline at end of file diff --git a/base-r-graphics.qmd b/base-r-graphics.qmd new file mode 100644 index 0000000..d947eeb --- /dev/null +++ b/base-r-graphics.qmd @@ -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 +``` \ No newline at end of file diff --git a/hello-world.qmd b/hello-world.qmd deleted file mode 100644 index b8b1ecb..0000000 --- a/hello-world.qmd +++ /dev/null @@ -1,3 +0,0 @@ -# The Hello World Example {#sec-hello-world} - -Hello Quarto World! diff --git a/index.qmd b/index.qmd index fbb5586..9181951 100644 --- a/index.qmd +++ b/index.qmd @@ -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. \ No newline at end of file diff --git a/r-environment-variables.qmd b/r-environment-variables.qmd new file mode 100644 index 0000000..0704b50 --- /dev/null +++ b/r-environment-variables.qmd @@ -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? \ No newline at end of file diff --git a/r-reference.code-workspace b/r-reference.code-workspace new file mode 100644 index 0000000..876a149 --- /dev/null +++ b/r-reference.code-workspace @@ -0,0 +1,8 @@ +{ + "folders": [ + { + "path": "." + } + ], + "settings": {} +} \ No newline at end of file