---
title: 'Lab 1: Getting Started'
format:
  html:
    embed-resources: true
editor: source
---

###### Question 1
Copy and paste the code below into the console pane, and press the <kbd>Enter</kbd> key to run the code. 
If your installation of R is working correctly, you should see the R version printed out, and a message indicating that your version of R is up do date (like the message seen below).


```{r check_r_version}
#| document: show
r_version_info <- R.Version()
if (all(as.numeric(r_version_info$major) >= 4, 
        as.numeric(r_version_info$minor) > 4
        )
    ) {
  msg <- "✔ Your version of R is up to date!"
} else {
  msg <- "✖ Your version of R is out of date; You should update to version 4.4 now."
}
cat(r_version_info$version.string, msg, sep="\n")
```
###### Question 2
Copy and paste the R code below into the console and press the <kbd>Enter</kbd> key to run the code. 
If you have configured RStudio correctly by following the instructions above, you should see a message that says "✔ RStudio is correctly configured!". 


```{r unnamed-chunk-2}
#| document: show
rstudio_config <- jsonlite::fromJSON(paste0(rstudio.prefs::rstudio_config_path(),
                                            "/rstudio-prefs.json")
                                     )
options_set <- c(
  rstudio_config$save_workspace == "never",
  rstudio_config$load_workspace == FALSE,
  rstudio_config$restore_last_project == FALSE,
  rstudio_config$restore_source_documents == FALSE,
  rstudio_config$check_for_updates == FALSE,
  rstudio_config$color_preview == FALSE,
  rstudio_config$rmd_viewer_type == "pane",
  rstudio_config$rmd_chunk_output_inline == FALSE
  )

if (all(options_set)) {
  cli::cli_alert_success("RStudio is correctly configured!")
} else {
  cli::cli_alert_danger("RStudio settings have not been correctly configured.")
}
```
###### Question 3
Copy and paste the R code below into the console and press the <kbd>Enter</kbd> key to run the code. 
If an appropriate version of the `tidyverse` package is properly installed, you should see two messages: "✔ tidyverse is installed and relatively up-to-date" and "✔ usethis is installed."


```{r check_tidyverse}
#| document: show
has_tidyverse <- suppressPackageStartupMessages(require(tidyverse))
has_usethis <- suppressPackageStartupMessages(require(usethis))

if (has_tidyverse) {
  tidyverse_version <- installed.packages() |>
    as_tibble() |>
    filter(Package == "tidyverse") |>
    pull(Version) 
  
  if (tidyverse_version > 2.0) {
    cli::cli_alert_success("tidyverse is installed and relatively up-to-date.")
  } else {
    cli::cli_alert_danger("tidyverse is installed but it is not up-to-date. Please update your packages.")
  }
} else {
  ui_oops("tidyverse could not be loaded.")
}
if (has_usethis) {
  cli::cli_alert_success("usethis is installed")
} else {
  ui_oops("usethis could not be loaded")
}
```
###### Question 4
Copy and paste the code below into your R console, and press the <kbd>Enter</kbd> key to run the code.
If your project is set up correctly, you should see a message saying "Your project is in a good place" (like the one shown in the image below).


```{r check_project}
#| document: show
project <- tryCatch(
  usethis:::proj_path(),
  error = function(e) {
    if (class(e)[1] == "usethis_error") {
      FALSE
    } else {
      stop(e) # rethrow package not found error!
    }
  }
)

if (!isFALSE(project)) {
  cli::cli_alert_success("Project found at: {.path {project}}. Your project is in a good place.")
} else {
  cli::cli_alert_danger(
    "No project environment detected. Make sure you:\n
     1. Create and open an R project for SDS 100.
     2. Move your Quarto file into your project folder before rendering it
    "
  )
}
```
