From 33717a91bc1c2868bfc490ed56ed82434284b783 Mon Sep 17 00:00:00 2001 From: Gtnick1241 Date: Sun, 9 Apr 2023 16:00:52 -0400 Subject: [PATCH 1/6] Add new example_dataset and used it in the example for the euclideanDist function --- .Rbuildignore | 4 + .gitignore | 3 + DESCRIPTION | 4 +- NAMESPACE | 1 + R/data.R | 11 + R/distance.R | 31 ++ data-raw/example_data.R | 6 + data/example_data.rda | Bin 0 -> 2016 bytes man/euclideanDist.Rd | 26 ++ man/example_data.Rd | 20 + renv.lock | 854 ++++++++++++++++++++++++++++++++++++++++ 11 files changed, 959 insertions(+), 1 deletion(-) create mode 100644 R/data.R create mode 100644 R/distance.R create mode 100644 data-raw/example_data.R create mode 100644 data/example_data.rda create mode 100644 man/euclideanDist.Rd create mode 100644 man/example_data.Rd create mode 100644 renv.lock diff --git a/.Rbuildignore b/.Rbuildignore index 88b0533..7334216 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -1,3 +1,5 @@ +^renv$ +^renv\.lock$ ^LICENSE\.md$ ^.*\.Rproj$ ^\.Rproj\.user$ @@ -6,3 +8,5 @@ ^docs$ ^pkgdown$ ^\.lintr$ +^data-raw$ +^renv.* diff --git a/.gitignore b/.gitignore index 0dddc4b..fbe71ed 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,6 @@ vignettes/*.pdf .Renviron inst/doc docs + +.Rprofile +renv/ \ No newline at end of file diff --git a/DESCRIPTION b/DESCRIPTION index e2161da..2d973a0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -15,10 +15,12 @@ Encoding: UTF-8 biocViews: Clustering LazyData: false Roxygen: list(markdown = TRUE) -RoxygenNote: 7.1.1 +RoxygenNote: 7.2.3 Suggests: knitr, rmarkdown, testthat (>= 3.0.0) Config/testthat/edition: 3 VignetteBuilder: knitr +Depends: + R (>= 2.10) diff --git a/NAMESPACE b/NAMESPACE index 01843dc..6bd620a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,3 +1,4 @@ # Generated by roxygen2: do not edit by hand +export(euclideanDist) export(hello) diff --git a/R/data.R b/R/data.R new file mode 100644 index 0000000..be00067 --- /dev/null +++ b/R/data.R @@ -0,0 +1,11 @@ +#' Example dataset +#' +#' A dataset containing a matrix with two columns that were generated +#' with a random normal distribution with a mean of 0 and stdev of 1. +#' +#' @format A matrix with 100 rows and 2 columns +#' @keywords datasets +#' @usage data("example_data") +#' @examples +#' data("example_data") +"example_data" \ No newline at end of file diff --git a/R/distance.R b/R/distance.R new file mode 100644 index 0000000..d0bb8cd --- /dev/null +++ b/R/distance.R @@ -0,0 +1,31 @@ +#' @title Euclidean distance +#' @description Calculates Euclidean distance between two vectors. An error will be +#' given if NAs are present in either vector. +#' +#' @param a The first vector to use in the distance calculation. +#' @param b The second vector to use in the distance calculation. +#' @param verbose Boolean. If \code{TRUE}, a message will be printed. Default \code{TRUE}. +#' @return A numeric value of a distance +#' @examples +#' data(example_data) +#' euclideanDist(example_data[,1], example_data[,2], verbose = FALSE) +#' @export +euclideanDist <- function(a, b, verbose = FALSE) { + if (isTRUE(verbose)) { + message("Calculating distance ...") + } + + # Check validity of data + .check_data(a) + .check_data(b) + + # Perform calculation + res <- sqrt(sum((a-b)^2)) + return(res) +} + +.check_data <- function(input) { + if (any(is.na(input))) { + stop("'input' must not contain NAs") + } +} \ No newline at end of file diff --git a/data-raw/example_data.R b/data-raw/example_data.R new file mode 100644 index 0000000..24efc73 --- /dev/null +++ b/data-raw/example_data.R @@ -0,0 +1,6 @@ +## code to prepare `example_data` dataset goes here +set.seed(123) +a <- rnorm(100) +b <- rnorm(100) +example_data <- cbind(a, b) +usethis::use_data(example_data, overwrite = TRUE) diff --git a/data/example_data.rda b/data/example_data.rda new file mode 100644 index 0000000000000000000000000000000000000000..ea06561304ce415574823364cc75a5d01bfc2516 GIT binary patch literal 2016 zcmV<62Os!CT4*^jL0KkKSsHj7&;S84fB*mg|NsC0|NsC0|NsC0|NsC0|NsC0|NsC0 z|NsC0|NqbgeqQ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k8qT?$_)-d-e)OCfNx`*%4%!}NcS0P< zkbOnS*l3BIKr*!KDn%*<#X>dr+?2Gxlq=p%>%9ft5?U(ZoD7Ff<1^tgsjBulK^q7p z_0s44uIphOfPjzpGN_$uiA$vFiQ?2r;80586+0>_zMMCELWy?w7Js@CKuOD_ubLQ| ztK~hQg&`TwR(tsNy7~1j?GQN2IZ}j^iTsY@+=vhg^iBz<bJ6vRYq~8@ittAUvtPK)p6{9_E9lqj~A0B6O7 zyAwM$Dnoec4LOS50EuMma@TiH-LMe`E9h_s>^ z=VIA&)d}HQu2aRJnM?Ab<0VZGzc@R=){_b$pm Date: Sun, 9 Apr 2023 17:20:24 -0400 Subject: [PATCH 2/6] change format and package version --- DESCRIPTION | 2 +- NEWS.md | 3 ++- R/data.R | 2 -- R/distance.R | 1 - R/functions.R | 3 ++- _pkgdown.yml | 4 ++++ man/example_data.Rd | 5 ++++- tests/testthat.R | 8 ++++++++ tests/testthat/test-euclidean.R | 7 +++++++ vignettes/DevelExample.Rmd | 9 +++++++++ 10 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 tests/testthat/test-euclidean.R diff --git a/DESCRIPTION b/DESCRIPTION index 2d973a0..1658ef4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: DevelExample Title: A Basic R Package to Demonstrate a Cycle of Code Development -Version: 0.0.1 +Version: 0.1.1 Authors@R: person(given = "Joshua", family = "Campbell", diff --git a/NEWS.md b/NEWS.md index 4cd8ae2..8bf56cb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ -# Changes in Version 0.0.1 (2022-05-08) +# Changes in Version 0.1.1 (2023-04-09) * Created package * Added Hello World function * Added documentation, unit tests, and vignettes * Added a `NEWS.md` file to track changes to the package. +* Added function to calculate euclidean dist diff --git a/R/data.R b/R/data.R index be00067..20b993d 100644 --- a/R/data.R +++ b/R/data.R @@ -1,8 +1,6 @@ #' Example dataset -#' #' A dataset containing a matrix with two columns that were generated #' with a random normal distribution with a mean of 0 and stdev of 1. -#' #' @format A matrix with 100 rows and 2 columns #' @keywords datasets #' @usage data("example_data") diff --git a/R/distance.R b/R/distance.R index d0bb8cd..40a8cf7 100644 --- a/R/distance.R +++ b/R/distance.R @@ -1,7 +1,6 @@ #' @title Euclidean distance #' @description Calculates Euclidean distance between two vectors. An error will be #' given if NAs are present in either vector. -#' #' @param a The first vector to use in the distance calculation. #' @param b The second vector to use in the distance calculation. #' @param verbose Boolean. If \code{TRUE}, a message will be printed. Default \code{TRUE}. diff --git a/R/functions.R b/R/functions.R index 5e2937a..e4fa2eb 100644 --- a/R/functions.R +++ b/R/functions.R @@ -1,3 +1,4 @@ + #' @title Hello world #' @description A simple function to print Hello world #' @@ -14,4 +15,4 @@ hello <- function(withExcitement = TRUE) { str <- "Hello world." } return(str) -} + } diff --git a/_pkgdown.yml b/_pkgdown.yml index e69de29..d71acfb 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -0,0 +1,4 @@ +url: ~ +template: + bootstrap: 5 + diff --git a/man/example_data.Rd b/man/example_data.Rd index e724347..be597cf 100644 --- a/man/example_data.Rd +++ b/man/example_data.Rd @@ -3,7 +3,9 @@ \docType{data} \name{example_data} \alias{example_data} -\title{Example dataset} +\title{Example dataset +A dataset containing a matrix with two columns that were generated +with a random normal distribution with a mean of 0 and stdev of 1.} \format{ A matrix with 100 rows and 2 columns } @@ -11,6 +13,7 @@ A matrix with 100 rows and 2 columns data("example_data") } \description{ +Example dataset A dataset containing a matrix with two columns that were generated with a random normal distribution with a mean of 0 and stdev of 1. } diff --git a/tests/testthat.R b/tests/testthat.R index 35ab969..94ef759 100644 --- a/tests/testthat.R +++ b/tests/testthat.R @@ -1,3 +1,11 @@ +# This file is part of the standard setup for testthat. +# It is recommended that you do not modify it. +# +# Where should you do additional test configuration? +# Learn more about the roles of various files in: +# * https://r-pkgs.org/tests.html +# * https://testthat.r-lib.org/reference/test_package.html#special-files + library(testthat) library(DevelExample) diff --git a/tests/testthat/test-euclidean.R b/tests/testthat/test-euclidean.R new file mode 100644 index 0000000..4717226 --- /dev/null +++ b/tests/testthat/test-euclidean.R @@ -0,0 +1,7 @@ +library("DevelExample") +data(example_data) + +test_that("Testing euclideanDist function", { + res <- dist(rbind(example_data[, 1], example_data[, 2]))[1] + expect_equal(euclideanDist(example_data[, 1], example_data[, 2]), res) + expect_error(euclideanDist(c(1, 2), c(NA, 2)), regexp = "contain NAs")}) \ No newline at end of file diff --git a/vignettes/DevelExample.Rmd b/vignettes/DevelExample.Rmd index 170fadb..5c30b4d 100644 --- a/vignettes/DevelExample.Rmd +++ b/vignettes/DevelExample.Rmd @@ -39,4 +39,13 @@ It is usually a good idea to show the session information to help with reproduci ```{r session} sessionInfo() ``` +To calculate the euclidean distance between two vectors, we can use the `euclideanDist` function. In this example we will generate two random vectors from normal distributions with two different means and calculate the distance between them: +```{r dist} +set.seed(12345) +v1 <- rnorm(10000, mean = 1) +v2 <- rnorm(10000, mean = 2) +res <- euclideanDist(v1, v2, verbose = FALSE) +res +``` +The `set.seed` function is used for the random number generator and ensures the same vectors will be produced each time for reproducibility. From 1131ea4a7b3c1e24873e68d3bf0b9d4ed5858be2 Mon Sep 17 00:00:00 2001 From: Gtnick1241 Date: Tue, 11 Apr 2023 19:12:40 -0400 Subject: [PATCH 3/6] fixed error --- .github/workflows/R-CMD-check.yaml | 26 +++++++++++++++----------- .github/workflows/lint.yaml | 20 ++++++++++++-------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index d9de82f..4c86f65 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -29,33 +29,37 @@ jobs: - {os: windows-latest, r: '3.6'} # Use older ubuntu to maximise backward compatibility - - {os: ubuntu-18.04, r: 'devel', http-user-agent: 'release'} - - {os: ubuntu-18.04, r: 'release'} - - {os: ubuntu-18.04, r: 'oldrel-1'} - - {os: ubuntu-18.04, r: 'oldrel-2'} - - {os: ubuntu-18.04, r: 'oldrel-3'} - - {os: ubuntu-18.04, r: 'oldrel-4'} + - {os: ubuntu-20.04, r: 'devel', http-user-agent: 'release'} + - {os: ubuntu-20.04, r: 'release'} + - {os: ubuntu-20.04, r: 'oldrel-1'} + - {os: ubuntu-20.04, r: 'oldrel-2'} + - {os: ubuntu-20.04, r: 'oldrel-3'} + - {os: ubuntu-20.04, r: 'oldrel-4'} env: GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} R_KEEP_PKG_SOURCE: yes steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - - uses: r-lib/actions/setup-pandoc@v1 + - uses: r-lib/actions/setup-pandoc@v2 + - name: Install dependencies - - uses: r-lib/actions/setup-r@v1 + - uses: r-lib/actions/setup-r@v2 with: r-version: ${{ matrix.config.r }} http-user-agent: ${{ matrix.config.http-user-agent }} use-public-rspm: true - - uses: r-lib/actions/setup-r-dependencies@v1 + - uses: r-lib/actions/setup-r-dependencies@v2 with: extra-packages: rcmdcheck + + - name: Install knitr + run: Rscript -e 'install.packages("knitr")' - - uses: r-lib/actions/check-r-package@v1 + - uses: r-lib/actions/check-r-package@v2 - name: Show testthat output if: always() diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index 6388f75..5884ec6 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -1,10 +1,10 @@ -# Workflow derived from https://github.com/r-lib/actions/tree/master/examples +# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help on: push: - branches: [main, master, devel] + branches: [main, master] pull_request: - branches: [main, master, devel] + branches: [main, master] name: lint @@ -14,16 +14,20 @@ jobs: env: GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - - uses: r-lib/actions/setup-r@v1 + - uses: r-lib/actions/setup-r@v2 with: use-public-rspm: true - - uses: r-lib/actions/setup-r-dependencies@v1 + - uses: r-lib/actions/setup-r-dependencies@v2 with: - extra-packages: lintr + extra-packages: any::lintr, local::. + needs: lint - name: Lint - run: lintr::lint_package() + run: lintr::lint_package(linters = lintr::linters_with_defaults(object_name_linter = + lintr::object_name_linter("camelCase"))) shell: Rscript {0} + env: + LINTR_ERROR_ON_LINT: true From a8854da660a285dbf5e7b818db83934f486de259 Mon Sep 17 00:00:00 2001 From: Gtnick1241 Date: Tue, 11 Apr 2023 20:17:03 -0400 Subject: [PATCH 4/6] update workflow file --- .github/workflows/R-CMD-check.yaml | 5 +---- .github/workflows/lint.yaml | 3 +-- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 4c86f65..3de812d 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -54,10 +54,7 @@ jobs: - uses: r-lib/actions/setup-r-dependencies@v2 with: - extra-packages: rcmdcheck - - - name: Install knitr - run: Rscript -e 'install.packages("knitr")' + extra-packages: rcmdcheck, knitr - uses: r-lib/actions/check-r-package@v2 diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index 5884ec6..f69b43d 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -26,8 +26,7 @@ jobs: needs: lint - name: Lint - run: lintr::lint_package(linters = lintr::linters_with_defaults(object_name_linter = - lintr::object_name_linter("camelCase"))) + run: lintr::lint_package(linters = lintr::linters_with_defaults(object_name_linter = lintr::object_name_linter("camelCase"))) shell: Rscript {0} env: LINTR_ERROR_ON_LINT: true From c26fd0ea15871b5d9e7be53faa4cd897d976a4da Mon Sep 17 00:00:00 2001 From: Gtnick1241 Date: Tue, 11 Apr 2023 20:57:04 -0400 Subject: [PATCH 5/6] Update R-CMD-check.yaml, lint.yaml --- .github/workflows/R-CMD-check.yaml | 3 +-- .github/workflows/lint.yaml | 14 +++++++------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 3de812d..47c376f 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -41,10 +41,9 @@ jobs: R_KEEP_PKG_SOURCE: yes steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v2 - uses: r-lib/actions/setup-pandoc@v2 - - name: Install dependencies - uses: r-lib/actions/setup-r@v2 with: diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index f69b43d..b05cb88 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -2,9 +2,9 @@ # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help on: push: - branches: [main, master] + branches: [main, master, devel] pull_request: - branches: [main, master] + branches: [main, master, devel] name: lint @@ -22,11 +22,11 @@ jobs: - uses: r-lib/actions/setup-r-dependencies@v2 with: - extra-packages: any::lintr, local::. - needs: lint + extra-packages: lintr - name: Lint - run: lintr::lint_package(linters = lintr::linters_with_defaults(object_name_linter = lintr::object_name_linter("camelCase"))) + run: lintr::lint_package(linters = + lintr::linters_with_defaults(object_name_linter = + lintr::object_name_linter("camelCase"))) shell: Rscript {0} - env: - LINTR_ERROR_ON_LINT: true + From 5985be4aae980b296722ea8b1dfb9617c067ce78 Mon Sep 17 00:00:00 2001 From: Gtnick1241 Date: Tue, 11 Apr 2023 21:16:01 -0400 Subject: [PATCH 6/6] update lint.yaml --- .github/workflows/lint.yaml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index b05cb88..212c4fc 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -1,4 +1,4 @@ -# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples +# Workflow derived from https://github.com/r-lib/actions/tree/master/examples # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help on: push: @@ -25,8 +25,6 @@ jobs: extra-packages: lintr - name: Lint - run: lintr::lint_package(linters = - lintr::linters_with_defaults(object_name_linter = - lintr::object_name_linter("camelCase"))) + run: lintr::lint_package(linters = lintr::linters_with_defaults(object_name_linter = lintr::object_name_linter("camelCase"))) shell: Rscript {0}