library(learnr)
library(gradethis)
library(data.table)
library(tibble)
knitr::opts_chunk$set(echo = FALSE)
gradethis_setup(
  pass.praise = TRUE, fail.hint = FALSE, 
  fail.encourage = TRUE, maybe_code_feedback = FALSE)

Project organization - content quiz

quiz(
  question(
    "How can you display your current working directory?",
    answer("Via the function `getwd()`.", correct = TRUE),
    answer("Via the function `cwd()`."),
    answer(paste0(
      "By clicking on ",
      "`Session/Set Working Directory/Print Working Directory` in R-Studio.")),
    answer("By searching for the `.Rproj` file in your file explorer."),
    allow_retry = TRUE, random_answer_order = TRUE
  ),
  question(
    "Which of the following statements about working directories are correct?",
    answer("The working directory cannot be changed once fixed within an R-project"),
    answer(paste0("If you use relative paths, they usually start ", 
                  "from your current working directory"), 
           correct=TRUE),
    answer("The default working directory for R is the user directory", 
           correct = TRUE),
    answer(paste0("If you open an R-project, the working directory is set to ", 
                  "the location of the `.Rproj`-file.")),
    allow_retry = TRUE, random_answer_order = TRUE
  ),
  question("What is the difference between absolute and relative paths?",
    answer(paste0("Absolute paths start at the root, ", 
                  "relative paths at the working directory."), 
           correct=TRUE),
    answer(paste0("Absolute paths are ambiguous across projects, relative ",
                  "paths are not.")),
    answer(paste0("Absolute paths contain a file name, relative paths ",
                  "only point to the directory in which files might be stored.")),
    answer(paste0("Relative paths are always relative to the `.Rproj`-file ",
                  "of the current project, absolute paths always start at ",
                  "the working directory.")),
    answer(paste0("Relative paths only work if a rerence point is provided ",
                  "explicitly, absolute paths always work.")),
    allow_retry = TRUE, random_answer_order = TRUE
    ),
  question(
    "Should you generally use absolute or relative paths?",
    answer(paste0("You should avoid absolute paths since they are " ,
                  "usually computer-specific"), correct=TRUE),
    answer(paste0(
      "For the sake of transparency, absolute paths are preferable ",
      "since they contain the whole paths, starting from the computer root.")),
    answer(paste0("Relative paths are always a bad idea because ", 
                  "they are dependent on your current working directory.")),
    answer(paste0("It does not really matter, both have their pros and cons.")),
    allow_retry = TRUE, random_answer_order = TRUE
    ),
  question(
    paste0("When creating a new project directory, which of the ",
           "following sub-directories should you create per default?"),
    answer("`R`, which contains all R scripts", correct=TRUE),
    answer("`output`, in which you save all output produced by your scripts", 
           correct=TRUE),
    answer("`text`, in which you save all written text", correct=TRUE),
    answer("`misc`, in which you store everything that doesn't fit elsewhere", 
           correct=TRUE),
    answer("`bin`, which serves as the garbage bin for this project"),
    answer("`plots`, in which you save your visualizations"),
    answer("`input`, in which you save your raw data that serves as input"),
    answer(paste0(
      "`public`, in which you store files meant for all public users ", 
      "(such as `LICENSE.md` or `README.md`).")),
    allow_retry = TRUE, random_answer_order = TRUE
  ),
  question(
    "What is the package `here` used for?",
    answer(paste0(
      "It takes relative paths as an argument, and produces ",
      "computer-specific absolute paths, based on information about the ",
      "relative location of the current file in your R project."), 
      correct=TRUE),
    answer(paste0(
      "It automatically sets the working directory of your ",
      "project to the place of the nearest `.Rproj` file, ",
      "allowing you to run code on different computers.")),
    answer(paste0(
      "After setting your position via `here::i_am()` you can ",
      "translate arbitrary file paths into paths to your current ",
      "working directory.")),
    answer(paste0(
      "It provides a number of functions that allow you to change the ",
      "working directory, to create new R-projects, and to facilitate code ",
      "sharing.")),
    allow_retry = TRUE, random_answer_order = TRUE
  )
)

Project organization - coding exercises

For the following exercises, assume that your project has the following directory structure and you are working from within the script myscript.R:

Exercise6
│   Exercise6.Rproj    
│
└───data
│   │
│   └───tidy
│       │   mydata.csv
│   └───raw
└───R
    │   myscript.R

Using here to self-identify file location

What should the first two lines in the file myscript.R look like if you will use the package here in this script?


____
library(____)
____
library(here)
here::____
library(here)
here::i_am(____)
library(here)
here::i_am("R/____")
library(here)
here::i_am("R/myscript.R")
library(here)
grade_this({
    fail_if(!stringr::str_detect(.user_code, "'R/myscript.R'"), 
          message = paste0(
            "Remember to tell `R` about the relative position of `myscript.R`. ",
            "Use the `i_am()` function form the package `here`!"
            )
          )
    fail_if_code_feedback(
          message = paste0(
            "Remember to tell `R` about the relative position of `myscript.R`" ,
            "in the beginning. Use the `i_am()` function form the package ",
            "`here`! Only after this you should attach the `here` package as ",
            "such."
            )
          )
  pass()
})

Creating absolute paths

Create an absolute path using the here package to the file mydata.csv. Assume that here is already attached, but make sure you use the ::-notation to be explicit.


here::here("data/tidy/mydata.csv")
here::here(____)
here::here("____")
here::here("data/____/____")
here::here("data/____/mydata.csv")
grade_this({
fail_if(!stringr::str_detect(.user_code, "here\\("), 
          message = paste0(
            "Your code is missing the call of the function `here()` with a ",
            "character vector representing the relative path to `mydata.csv`!"
            )
          )

      fail_if(!stringr::str_detect(.user_code, "data/tidy/mydata.csv"), 
          message = paste0(
            "Your code does not contain the correct relative path to ",
            "`mydata.csv`, which is located in the subdirectory `data/table/`."
            )
          )
      pass_if(identical(.result, here::here('data/tidy/mydata.csv')))
  fail()
})


graebnerc/DataScienceExercises documentation built on April 11, 2025, 7:57 p.m.