### Inline Documentation Example Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/README.md Demonstrates how to use the documentation reference for inline code assistance by showing example R commands to look up function signatures, types, or error details. ```r # Looking for localize_beach() signature? # See: api-reference-delta.md or api-reference-echo.md # Need to understand a type? # See: types.md # Debugging an error? # See: errors.md ``` -------------------------------- ### Example File Not Found Error Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/errors.md Illustrates the error message when the 'beach-lookup-table.csv' file is not found. Ensure the delta package is installed and the file is not manually moved or deleted. ```R Error: Cannot open file '/nonexistent/path/beach-lookup-table.csv'. No such file or directory. ``` -------------------------------- ### Install Echo Package Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/quick-start.md Installs the echo package from the r-pkgs repository using devtools. ```r devtools::install("package-within-files/echo") ``` -------------------------------- ### Install Delta Package Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/quick-start.md Installs the delta package from the r-pkgs repository using devtools. ```r devtools::install("package-within-files/delta") ``` -------------------------------- ### Echo DESCRIPTION File with Imports Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/module-structure.md Example of a DESCRIPTION file for an R package named 'echo', explicitly declaring an import dependency on 'dplyr'. ```r Package: echo Title: What the Package Does (One Line, Title Case) Version: 0.0.0.9000 Authors@R: person("Jennifer", "Bryan", ...) Description: What the package does (one paragraph). License: MIT + file LICENSE Encoding: UTF-8 Roxygen: list(markdown = TRUE) RoxygenNote: 7.2.3 Imports: dplyr ``` -------------------------------- ### Delta DESCRIPTION File Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/module-structure.md Example of a DESCRIPTION file for an R package named 'delta'. It includes package name, title, version, author information, description, license, encoding, and Roxygen settings. ```r Package: delta Title: What the Package Does (One Line, Title Case) Version: 0.0.0.9000 Authors@R: person("Jennifer", "Bryan", ...) Description: What the package does (one paragraph). License: MIT + file LICENSE Encoding: UTF-8 Roxygen: list(markdown = TRUE) RoxygenNote: 7.2.3 ``` -------------------------------- ### Load Tidyverse Package Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/module-structure.md This snippet shows how to load the tidyverse package. It is a soft dependency and may cause the package to fail if tidyverse is not installed. ```r library(tidyverse) ``` -------------------------------- ### Load devtools package in R Source: https://github.com/hadley/r-pkgs/blob/main/data/colophon.txt Loads the devtools package, which provides tools for package development. Ensure the package is installed before loading. ```r library(devtools) #> Loading required package: usethis ``` -------------------------------- ### Get R session information with devtools Source: https://github.com/hadley/r-pkgs/blob/main/data/colophon.txt Retrieves detailed information about the current R session, including R version, OS, and loaded packages. This is crucial for ensuring reproducibility. ```r devtools::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.2.2 (2022-10-31) #> os macOS Big Sur ... 10.16 #> system x86_64, darwin17.0 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz America/Vancouver #> date 2023-06-06 #> pandoc 2.19.2 @ /Applications/RStudio.app/.../bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> brio 1.1.3 2021-11-30 [1] CRAN (R 4.2.0) #> cachem 1.0.8 2023-05-01 [1] CRAN (R 4.2.0) #> callr 3.7.3 2022-11-02 [1] CRAN (R 4.2.0) #> cli 3.6.1 2023-03-23 [1] CRAN (R 4.2.0) #> crayon 1.5.2 2022-09-29 [1] CRAN (R 4.2.0) #> devtools * 2.4.5 2022-10-11 [1] CRAN (R 4.2.0) #> digest 0.6.31 2022-12-11 [1] CRAN (R 4.2.0) #> ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.2.0) #> evaluate 0.21 2023-05-05 [1] CRAN (R 4.2.0) #> fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.2.0) #> fs 1.6.2 2023-04-25 [1] CRAN (R 4.2.0) #> glue 1.6.2 2022-02-24 [1] CRAN (R 4.2.0) #> htmltools 0.5.5 2023-03-23 [1] CRAN (R 4.2.2) #> htmlwidgets 1.6.2 2023-03-17 [1] CRAN (R 4.2.0) #> httpuv 1.6.9 2023-02-14 [1] CRAN (R 4.2.0) #> knitr 1.43 2023-05-25 [1] CRAN (R 4.2.0) #> later 1.3.0 2021-08-18 [1] CRAN (R 4.2.0) #> lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.2.0) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.2.0) #> memoise 2.0.1 2021-11-26 [1] CRAN (R 4.2.0) #> mime 0.12 2021-09-28 [1] CRAN (R 4.2.0) #> miniUI 0.1.1.1 2018-05-18 [1] CRAN (R 4.2.0) #> pkgbuild 1.4.0 2022-11-27 [1] CRAN (R 4.2.0) #> pkgload 1.3.2 2022-11-16 [1] CRAN (R 4.2.0) #> prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.2.0) #> processx 3.8.1 2023-04-18 [1] CRAN (R 4.2.0) #> profvis 0.3.7 2020-11-02 [1] CRAN (R 4.2.0) #> promises 1.2.0.1 2021-02-11 [1] CRAN (R 4.2.0) #> ps 1.7.5 2023-04-18 [1] CRAN (R 4.2.0) #> purrr 1.0.1 2023-01-10 [1] CRAN (R 4.2.0) #> R.cache 0.16.0 2022-07-21 [1] CRAN (R 4.2.0) #> R.methodsS3 1.8.2 2022-06-13 [1] CRAN (R 4.2.0) #> R.oo 1.25.0 2022-06-12 [1] CRAN (R 4.2.0) #> R.utils 2.12.2 2022-11-11 [1] CRAN (R 4.2.0) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.2.0) #> Rcpp 1.0.10 2023-01-22 [1] CRAN (R 4.2.0) #> remotes 2.4.2 2021-11-30 [1] CRAN (R 4.2.0) #> reprex 2.0.2 2022-08-17 [1] CRAN (R 4.2.0) #> rlang 1.1.1 2023-04-28 [1] CRAN (R 4.2.0) #> rmarkdown 2.22 2023-06-01 [1] CRAN (R 4.2.0) #> roxygen2 * 7.2.3 2022-12-08 [1] CRAN (R 4.2.0) #> rstudioapi 0.14 2022-08-22 [1] CRAN (R 4.2.0) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.2.0) #> shiny 1.7.4 2022-12-15 [1] CRAN (R 4.2.0) #> stringi 1.7.12 2023-01-11 [1] CRAN (R 4.2.0) #> stringr 1.5.0 2022-12-02 [1] CRAN (R 4.2.0) #> styler 1.10.1 2023-06-05 [1] CRAN (R 4.2.2) #> testthat * 3.1.8 2023-05-04 [1] CRAN (R 4.2.0) #> urlchecker 1.0.1 2021-11-30 [1] CRAN (R 4.2.0) #> usethis * 2.2.0 2023-06-06 [1] CRAN (R 4.2.2) #> vctrs 0.6.2 2023-04-19 [1] CRAN (R 4.2.0) #> withr 2.5.0 2022-03-03 [1] CRAN (R 4.2.0) #> xfun 0.39 2023-04-20 [1] CRAN (R 4.2.0) #> xml2 1.3.4 2023-04-27 [1] CRAN (R 4.2.0) #> xtable 1.8-4 2019-04-21 [1] CRAN (R 4.2.0) #> yaml 2.3.7 2023-01-23 [1] CRAN (R 4.2.0) #> #> [1] /Users/jenny/Library/R/x86_64/4.2/library #> [2] /Library/Frameworks/R.framework/Versions/4.2/Resources/library #> #> ────────────────────────────────────────────────────────────────────────────── ``` -------------------------------- ### Basic Quarto Callout Source: https://github.com/hadley/r-pkgs/blob/main/README.md Demonstrates the basic syntax for a Quarto callout of type 'tip'. ```markdown ::: callout-tip Here's a super handy nifty thing! ::: ``` -------------------------------- ### Package Load-Time Configuration Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/configuration.md Captures the current system time when the package is loaded. This timestamp is reused for all outfile_path() calls within the session. ```r now <- Sys.time() ``` -------------------------------- ### Minimal Data Cleaning with Delta Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/quick-start.md Demonstrates a minimal data cleaning pipeline using the delta package, including localization and temperature conversion. ```r library(delta) # Create sample data with beach terminology and temperature data <- data.frame( where = "beach", temp = 72, stringsAsFactors = FALSE ) # Add region localization data <- localize_beach(data) # Convert temperature from Fahrenheit to Celsius data <- celsify_temp(data) # Generate output filename output_file <- outfile_path("input.csv") # Write cleaned data write.csv(data, output_file, row.names = FALSE) ``` -------------------------------- ### Import Delta or Echo Package Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/quick-start.md Loads the delta or echo package into the current R session. ```r library(delta) # or library(echo) ``` -------------------------------- ### Package Structure Overview Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/INDEX.md Illustrates the directory structure and key files for two R packages, 'delta' and 'echo'. Shows source code locations, exported functions, and data files. ```tree delta/ ├── R/cleaning-helpers.R # Source code (24 lines) │ ├── localize_beach() # Exported │ ├── celsify_temp() # Exported │ ├── outfile_path() # Exported │ ├── f_to_c() # Private │ └── timestamp() # Private ├── DESCRIPTION # Metadata (no explicit imports) ├── NAMESPACE # Exports: localize_beach, celsify_temp, outfile_path └── beach-lookup-table.csv # External data echo/ ├── R/cleaning-helpers.R # Source code (27 lines) │ ├── lookup_table (tribble) # Private data │ ├── localize_beach() # Exported │ ├── celsify_temp() # Exported │ ├── outfile_path() # Exported │ ├── f_to_c() # Private │ └── timestamp() # Private ├── DESCRIPTION # Metadata (dplyr in Imports) └── NAMESPACE # Exports: localize_beach, celsify_temp, outfile_path ``` -------------------------------- ### Quarto Callout Types Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/book-chapters.md Demonstrates the usage of different Quarto callout types for structuring special content within documentation. Use these to highlight tips, warnings, notes, and important information. ```markdown ::: callout-tip Helpful hints and best practices ::: ::: callout-warning Things to be careful about, especially for CRAN ::: ::: callout-note Additional information and context ::: ::: callout-important Critical information ::: ``` -------------------------------- ### Stage 5: Echo Best Practices Package Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/book-chapters.md This stage presents an improved R package following best practices, including explicit dependency declarations (e.g., dplyr) in the DESCRIPTION file, embedded data, and standard package structure files. ```text package-within-files/echo/ ├── R/cleaning-helpers.R # Source code ├── DESCRIPTION # Package metadata with dplyr dependency ├── NAMESPACE # Export declarations ├── LICENSE # License file └── echo.Rproj # RStudio project ``` -------------------------------- ### RStudio Specific Quarto Callout Source: https://github.com/hadley/r-pkgs/blob/main/README.md Shows how to create a Quarto callout specifically for RStudio tips, using a level 2 header for the caption. ```markdown ::: callout-tip ## RStudio Here's a super handy nifty thing about RStudio specifically. ::: ``` -------------------------------- ### Enable Markdown in Roxygen2 Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/configuration.md Configure roxygen2 to use markdown syntax for documentation comments. This setting should be placed in the DESCRIPTION file. ```R Roxygen: list(markdown = TRUE) ``` -------------------------------- ### Output Data After Cleaning Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/book-chapters.md CSV data after applying the cleaning workflow, including localized terms and converted temperatures. ```csv beach,temp,where,english beach,22.22,beach,US coast,21.11,coast,US seashore,18.33,seashore,UK seaside,20.00,seaside,UK ``` -------------------------------- ### Stage 4: Delta Real Package Structure Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/book-chapters.md This stage introduces the first real R package, showcasing the basic structure including R source code, package metadata (DESCRIPTION, NAMESPACE), and license files. It also includes associated data and an RStudio project file. ```text package-within-files/delta/ ├── R/cleaning-helpers.R # Source code ├── DESCRIPTION # Package metadata ├── NAMESPACE # Export declarations ├── LICENSE # License file ├── beach-lookup-table.csv └── delta.Rproj # RStudio project ``` -------------------------------- ### Delta Package Dependencies Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/module-structure.md Illustrates the dependencies of the 'cleaning-helpers.R' module in the Delta package, including external files and exported/internal functions. ```text cleaning-helpers.R (exports 3 functions) ↓ (imports) - library(tidyverse) - beach-lookup-table.csv (external file) Exported API: - localize_beach() [public] - celsify_temp() [public] - outfile_path() [public] Internal: - f_to_c() [private] - timestamp() [private] ``` -------------------------------- ### R Packages Book DESCRIPTION File Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/book-chapters.md This is the DESCRIPTION file for the R Packages book, used to declare site dependencies. It lists required and suggested packages. ```r Package: rpkgs Type: Book Title: R Packages Version: 0.1 Authors@R: person("Hadley", "Wickham", role = c("aut", "cre")) Description: This is not a package, but we just use this file to declare the dependencies of the site. URL: https://r-pkgs.org/ ``` -------------------------------- ### Echo R/cleaning-helpers.R File Breakdown Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/module-structure.md Details the structure of the 'cleaning-helpers.R' file within the Echo package, outlining private data, exported functions, and configuration sections. ```text Lines | Content | Export Status |-------|---------|---------------- | 1-7 | lookup_table (tribble) | Private data | 8-12 | localize_beach() | **EXPORTED** | 13-14 | f_to_c() | Private | 15-19 | celsify_temp() | **EXPORTED** | 20-22 | Load-time configuration | — | 23-27 | outfile_path() | **EXPORTED** ``` -------------------------------- ### RStudio-Specific Callout Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/book-chapters.md Shows how to use a Quarto callout to specifically highlight RStudio-related features and workflows. This is useful for documentation targeting RStudio users. ```markdown ::: callout-tip ## RStudio RStudio-specific features and workflows ::: ``` -------------------------------- ### Delta R/cleaning-helpers.R File Breakdown Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/module-structure.md Provides a line-by-line breakdown of the 'cleaning-helpers.R' file in the Delta package, categorizing sections by content and export status. ```text Lines | Content | Export Status |-------|---------|---------------- | 1 | Library import | — | 2 | (blank) | — | 3-10 | localize_beach() | **EXPORTED** | 11-12 | f_to_c() | Private | 13-17 | celsify_temp() | **EXPORTED** | 18-20 | Load-time configuration | — | 21-24 | outfile_path() | **EXPORTED** ``` -------------------------------- ### Generate Timestamped Output File Path with outfile_path Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/api-reference-echo.md Generates a timestamped output file path by prepending a formatted timestamp to the input filename. Note that the timestamp is captured at load time, so all calls within a session use the same timestamp. ```r library(echo) output <- outfile_path("swim.csv") # Result: "2024-June-29_14-35-42_swim_clean.csv" (exact timestamp varies) ``` -------------------------------- ### Echo Package Dependencies Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/module-structure.md Details the dependencies of the 'cleaning-helpers.R' module in the Echo package, specifying explicit DESCRIPTION imports and internal data structures. ```text cleaning-helpers.R (exports 3 functions) ↓ (imports) - dplyr (explicit in DESCRIPTION) - lookup_table (internal tribble) Exported API: - localize_beach() [public] - celsify_temp() [public] - outfile_path() [public] Internal: - f_to_c() [private] - timestamp() [private] - lookup_table [private data] ``` -------------------------------- ### Echo Package DESCRIPTION File Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/configuration.md Full configuration for the echo R package as defined in its DESCRIPTION file. This configuration includes package metadata and an explicit import of the dplyr package. ```r Package: echo Title: What the Package Does (One Line, Title Case) Version: 0.0.0.9000 Authors@R: person("Jennifer", "Bryan", , "jenny@posit.co", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-6983-2759")) Description: What the package does (one paragraph). License: MIT + file LICENSE Encoding: UTF-8 Roxygen: list(markdown = TRUE) RoxygenNote: 7.2.3 Imports: dplyr ``` -------------------------------- ### Localize Beach Data and Celsify Temperature Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/quick-start.md Always call `localize_beach()` before `celsify_temp()` to ensure the 'english' column is available. This sequence is crucial for correct data processing. ```r data <- localize_beach(data) data <- celsify_temp(data) ``` -------------------------------- ### Delta Package DESCRIPTION File Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/configuration.md Full configuration for the delta R package as defined in its DESCRIPTION file. This configuration specifies package metadata and dependencies. ```r Package: delta Title: What the Package Does (One Line, Title Case) Version: 0.0.0.9000 Authors@R: person("Jennifer", "Bryan", , "jenny@posit.co", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-6983-2759")) Description: What the package does (one paragraph). License: MIT + file LICENSE Encoding: UTF-8 Roxygen: list(markdown = TRUE) RoxygenNote: 7.2.3 ``` -------------------------------- ### outfile_path() Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/api-reference-delta.md Generates a timestamped output file path by prepending a formatted timestamp to the input filename. The timestamp is captured at module load time. ```APIDOC ## outfile_path() ### Description Generates a timestamped output file path by prepending a formatted timestamp to the input filename. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Method None (R function) ### Endpoint None (R function) ### Parameters - **infile** (character) - Required - Input file name (typically with .csv extension). ### Returns character - Output file path with format `YYYY-Month-DD_HH-MM-SS_original_clean.csv` ### Example ```r library(delta) # Generate output filename output <- outfile_path("swim.csv") # Result: "2024-June-29_14-35-42_swim_clean.csv" (exact timestamp varies) ``` ``` -------------------------------- ### Stage 3: Charlie Helper Functions Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/book-chapters.md This stage demonstrates the extraction of reusable helper functions into a separate script, used by the main data cleaning script. It includes helper functions, the main script, and associated data files. ```text package-within-files/charlie/ ├── cleaning-helpers.R # Helper functions ├── data-cleaning.R # Main script using helpers ├── beach-lookup-table.csv ├── swim.csv └── swim_clean.csv ``` -------------------------------- ### Defensive Input Preparation Function in R Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/errors.md This pattern involves creating a dedicated function to prepare input data by ensuring it is a data frame, contains necessary columns, and has correct data types. This function should be called before any processing. ```r library(delta) # Prepare data defensively prepare_for_cleaning <- function(dat) { # Ensure data frame if (!is.data.frame(dat)) { dat <- as.data.frame(dat) } # Ensure required columns if (!("temp" %in% colnames(dat))) { stop("Input must have 'temp' column") } # Ensure correct types if (!is.numeric(dat$temp)) { dat$temp <- as.numeric(dat$temp) } return(dat) } dat <- data.frame(english = "US", temp = "72") dat <- prepare_for_cleaning(dat) dat <- celsify_temp(dat) ``` -------------------------------- ### Stage 1: Alfa Minimal Script Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/book-chapters.md This stage shows a basic R script for data cleaning before it is structured into a package. It includes input and output data files. ```text package-within-files/alfa/ ├── data-cleaning.R # Basic script without package structure ├── swim.csv # Input data └── swim_clean.csv # Output data ``` -------------------------------- ### Input Data for Cleaning Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/book-chapters.md CSV data representing beach and temperature readings, used as input for the data cleaning workflow. ```csv beach,temp beach,72 coast,70 seashore,65 seaside,68 ``` -------------------------------- ### Recovering from Missing Columns in celsify_temp() Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/errors.md Demonstrates how to ensure the input data frame has the required 'english' and 'temp' columns before calling celsify_temp(). This typically involves calling localize_beach() first and then adding or ensuring the 'temp' column. ```R # Ensure data has required columns dat <- localize_beach(dat) # Adds 'english' column dat$temp <- c(72, 32) # Add or ensure 'temp' column dat <- celsify_temp(dat) # Now safe to call ``` -------------------------------- ### Generate Timestamped Output Filename with outfile_path Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/quick-start.md Generates a timestamped output filename. Takes a filename as input and returns a path with a timestamp prepended. ```r output <- outfile_path("input.csv") ``` ```r output <- outfile_path("swim.csv") # Returns: "2024-June-29_14-35-42_swim_clean.csv" ``` -------------------------------- ### outfile_path() Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/api-reference-echo.md Generates a timestamped output file path by prepending a formatted timestamp to the input filename. The output path includes a timestamp and '_clean' before the extension. ```APIDOC ## outfile_path() ### Description Generates a timestamped output file path by prepending a formatted timestamp to the input filename. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **infile** (character) - Required - Input file name (typically with .csv extension) ### Returns character - Output file path with format `YYYY-Month-DD_HH-MM-SS_original_clean.csv` ### Details Uses the internal `timestamp()` function to format the current system time. The timestamp format is `%Y-%B-%d_%H-%M-%S` (4-digit year, full month name, day, hour-minute-second). Applies regex substitution `"(.*)([.]csv$)"` to insert `_clean` before the `.csv` extension. For example, `swim.csv` becomes `2024-June-29_14-35-42_swim_clean.csv`. ### Limitations The timestamp (`now`) is captured at load time via `now <- Sys.time()`, not at function call time. All invocations of `outfile_path()` within a session will use the same timestamp value. ### Example ```r library(echo) output <- outfile_path("swim.csv") # Result: "2024-June-29_14-35-42_swim_clean.csv" (exact timestamp varies) ``` ``` -------------------------------- ### Handle Non-String Input for outfile_path() Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/errors.md When providing non-character input to outfile_path(), an error will occur because no automatic string coercion is performed. Ensure all inputs are character strings. ```r outfile_path(123) # Error in paste0(...): invalid 'x' argument ``` ```r outfile_path(as.character(123)) # Convert to string first ``` -------------------------------- ### Function Signatures for Delta/Echo Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/INDEX.md These are the function signatures provided by both the Delta and Echo R packages. They cover localization, temperature conversion, and output file path generation. ```r # Localize beach terminology by joining with region data localize_beach(dat) # Convert temperatures to Celsius based on region celsify_temp(dat) # Generate timestamped output filename outfile_path(infile) ``` -------------------------------- ### Call outfile_path function Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/api-reference-delta.md Illustrates generating a timestamped output file path using the outfile_path function. The function takes a base filename and returns a formatted string with a timestamp and '_clean' appended before the extension. ```r library(delta) # Generate output filename output <- outfile_path("swim.csv") # Result: "2024-June-29_14-35-42_swim_clean.csv" (exact timestamp varies) ``` -------------------------------- ### CRAN Submission Quarto Callout Source: https://github.com/hadley/r-pkgs/blob/main/README.md Illustrates a Quarto callout for warnings related to submitting packages to CRAN, with a descriptive header. ```markdown ::: callout-warning ## Submitting to CRAN Here's something to be super careful about. ::: ``` -------------------------------- ### Load Delta Package Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/quick-start.md Loads the Delta package and implicitly uses tidyverse functions. The lookup table is loaded from a CSV file. ```r library(delta) # Uses tidyverse functions implicitly # Loads lookup table from CSV file ``` -------------------------------- ### Delta Package Structure Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/module-structure.md Illustrates the R code structure for the Delta package, including library loading, private helper functions, load-time configuration, and the public API. It shows implicit dependency on tidyverse. ```r # Package load-time execution library(tidyverse) # External dependency on tidyverse functions # Private helper function f_to_c <- function(x) (x - 32) * 5/9 # Load-time configuration now <- Sys.time() # Private helper function timestamp <- function(time) format(time, "%Y-%B-%d_%H-%M-%S") # Public API - 3 exported functions # 1. localize_beach(dat) - joins with lookup table # 2. celsify_temp(dat) - converts temperature # 3. outfile_path(infile) - generates timestamped filename ``` -------------------------------- ### Data Cleaning Pipeline with Delta or Echo Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/INDEX.md This R code demonstrates a common data cleaning pipeline using either the Delta or Echo package. It includes reading data, localizing beach terminology, converting temperatures, generating an output filename, and saving the processed data. ```r library(delta) # or library(echo) # Read raw data data <- read.csv("input.csv") # Step 1: Add region information data <- localize_beach(data) # Step 2: Standardize temperatures data <- celsify_temp(data) # Step 3: Generate output filename output <- outfile_path("input.csv") # Step 4: Save write.csv(data, output, row.names = FALSE) ``` -------------------------------- ### Load testthat package in R Source: https://github.com/hadley/r-pkgs/blob/main/data/colophon.txt Loads the testthat package, a framework for testing R code. It may mask functions from other packages like devtools. ```r library(testthat) #> #> Attaching package: 'testthat' #> The following object is masked from 'package:devtools': #> #> test_file ``` -------------------------------- ### Load Echo Package Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/quick-start.md Loads the Echo package and uses explicit dplyr function calls. The lookup table is embedded within the code. ```r library(echo) # Uses explicit dplyr function calls # Lookup table embedded in code ``` -------------------------------- ### Handle Unusual File Extensions with outfile_path() Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/errors.md The outfile_path() function's regex pattern expects a '.csv' extension. Inputs with different extensions or case variations may not be processed as expected, leading to unexpected output. Convert extensions to lowercase '.csv' before passing to the function. ```r outfile_path("data.txt") # Returns "2024-June-29_14-35-42_data.txt" (no _clean) ``` ```r outfile_path("data.CSV") # Returns "2024-June-29_14-35-42_data.CSV" (case-sensitive) ``` ```r # Convert to lowercase .csv infile <- tolower(sub("\.CSV$", ".csv", infile)) outfile_path(infile) ``` -------------------------------- ### Load roxygen2 package in R Source: https://github.com/hadley/r-pkgs/blob/main/data/colophon.txt Loads the roxygen2 package, used for documenting R code. This is a common dependency for package development workflows. ```r library(roxygen2) ``` -------------------------------- ### Call localize_beach function Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/api-reference-delta.md Demonstrates how to use the localize_beach function to add localization information to a data frame. This function requires a data frame as input and adds 'where' and 'english' columns. ```r library(delta) # Create sample data swim_data <- data.frame( location = "beach", temp = 72, stringsAsFactors = FALSE ) # Add localization information localized <- localize_beach(swim_data) # Result includes columns: location, temp, where, english ``` -------------------------------- ### localize_beach() Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/api-reference-echo.md Adds localization information to a data frame by joining with a region lookup table. It augments the input data with 'where' and 'english' columns based on location names. ```APIDOC ## localize_beach() ### Description Adds localization information to a data frame by joining with a region lookup table. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **dat** (data.frame or tibble) - Required - Input data to augment with localization information ### Returns data.frame or tibble - Input data joined with the lookup table, adding `where` and `english` columns. ### Details This function joins the input data with an internally-defined lookup table created using `dplyr::tribble()`. Unlike the delta version, the lookup table is embedded in the package code rather than loaded from an external CSV file. ### Example ```r library(echo) # Create sample data swim_data <- data.frame( location = "beach", temp = 72, stringsAsFactors = FALSE ) # Add localization information localized <- localize_beach(swim_data) # Result includes: location, temp, where, english columns ``` ``` -------------------------------- ### Complete Data Cleaning Pipeline with Delta Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/quick-start.md Executes a comprehensive data cleaning pipeline using the delta package, from reading raw data to saving cleaned output. ```r library(delta) # Read raw data raw_data <- read.csv("swim.csv") # Step 1: Add region information data <- localize_beach(raw_data) # Step 2: Standardize temperatures to Celsius data <- celsify_temp(data) # Step 3: Generate output path output_path <- outfile_path("swim.csv") # Step 4: Save cleaned data write.csv(data, output_path, row.names = FALSE) print(paste("Cleaned data saved to:", output_path)) ``` -------------------------------- ### Data Cleaning Pipeline with Error Handling in Delta Source: https://github.com/hadley/r-pkgs/blob/main/_autodocs/quick-start.md Applies a data cleaning pipeline with robust error handling using tryCatch to manage potential issues during data processing. ```r library(delta) # Safe version with validation tryCatch({ # Load data data <- read.csv("swim.csv") # Validate structure if (!is.data.frame(data)) { stop("Input must be a data frame") } # Apply transformations data <- localize_beach(data) if (!("english" %in% colnames(data))) { stop("localize_beach() failed to add region information") } data <- celsify_temp(data) # Save output output_file <- outfile_path("swim.csv") write.csv(data, output_file, row.names = FALSE) cat("Success! Cleaned data saved to:", output_file, "\n") }, error = function(e) { cat("Error in cleaning pipeline:\n", as.character(e), "\n") }) ```