diff --git a/.Rbuildignore b/.Rbuildignore index 88b0533..6624a45 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -6,3 +6,4 @@ ^docs$ ^pkgdown$ ^\.lintr$ +^data-raw$ diff --git a/.gitignore b/.gitignore index 0dddc4b..2743be2 100644 --- a/.gitignore +++ b/.gitignore @@ -39,4 +39,3 @@ vignettes/*.pdf # R Environment Variables .Renviron inst/doc -docs diff --git a/DESCRIPTION b/DESCRIPTION index e2161da..f663d85 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.0 Authors@R: person(given = "Joshua", family = "Campbell", @@ -22,3 +22,5 @@ Suggests: 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/NEWS.md b/NEWS.md index 4cd8ae2..86504fb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,6 @@ +# Changes in Version 0.1.0 (2022-05-20) +* Added function to calculate euclidean distance + # Changes in Version 0.0.1 (2022-05-08) * Created package * Added Hello World function diff --git a/R/data.R b/R/data.R new file mode 100644 index 0000000..394dcb5 --- /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" diff --git a/R/distance.R b/R/distance.R new file mode 100644 index 0000000..93f8b93 --- /dev/null +++ b/R/distance.R @@ -0,0 +1,38 @@ +#' @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 + .checkData(a) + .checkData(b) + + # Perform calculation + res <- sqrt(sum((a - b) ^ 2)) + return(res) +} + +#' Validity check +#' +#' This functions checks for NAs in a vector +#' +#' @param input A numeric vector +#' @return Nothing returned +.checkData <- function(input) { + if (any(is.na(input))) { + stop("'input' must not contain NAs") + } +} 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 0000000..cfdadd0 Binary files /dev/null and b/data/example_data.rda differ diff --git a/docs/404.html b/docs/404.html new file mode 100644 index 0000000..794dfbf --- /dev/null +++ b/docs/404.html @@ -0,0 +1,99 @@ + + +
+ + + + +MIT License + +Copyright (c) 2022 campbio + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. ++ +
Copyright (c) 2022 DevelExample authors
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+DevelExample.RmdThis is a simple vignette to demonstrate the functions that are available in the DevelExample package. The purpose of this package is to demonstrate the process of adding code to an R function. The full tutorial “Adding code to an R package” can be found at the Campbell lab website under the “Articles” tab.
+Let’s first load the library:
+
+library(DevelExample)Next we can run our function with default parameters:
+
+hello()
+#> [1] "Hello world!!!"We can also run the same function but without all of the exclamation marks:
+
+hello(withExcitement = FALSE)
+#> [1] "Hello world."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:
+set.seed(12345)
+v1 <- rnorm(10000, mean = 1)
+v2 <- rnorm(10000, mean = 2)
+res <- euclideanDist(v1, v2, verbose = FALSE)
+res
+#> [1] 171.4371The set.seed function is used for the random number generator and ensures the same vectors will be produced each time for reproducibility.
It is usually a good idea to show the session information to help with reproducibility:
+
+sessionInfo()
+#> R version 4.0.2 (2020-06-22)
+#> Platform: x86_64-apple-darwin17.0 (64-bit)
+#> Running under: macOS 10.16
+#>
+#> Matrix products: default
+#> BLAS: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
+#> LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
+#>
+#> locale:
+#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
+#>
+#> attached base packages:
+#> [1] stats graphics grDevices utils datasets methods base
+#>
+#> other attached packages:
+#> [1] DevelExample_0.1.0
+#>
+#> loaded via a namespace (and not attached):
+#> [1] rstudioapi_0.13 knitr_1.39 magrittr_2.0.1 R6_2.5.0
+#> [5] ragg_1.1.3 rlang_1.0.2 fastmap_1.1.0 stringr_1.4.0
+#> [9] tools_4.0.2 xfun_0.30 cli_3.3.0 jquerylib_0.1.3
+#> [13] htmltools_0.5.2 systemfonts_1.0.1 yaml_2.2.1 digest_0.6.27
+#> [17] rprojroot_2.0.2 pkgdown_2.0.3 crayon_1.4.1 textshaping_0.3.5
+#> [21] purrr_0.3.4 sass_0.4.1 fs_1.5.0 memoise_2.0.0
+#> [25] cachem_1.0.4 evaluate_0.15 rmarkdown_2.14 stringi_1.5.3
+#> [29] compiler_4.0.2 bslib_0.3.1 desc_1.3.0 jsonlite_1.7.2dot-checkData.RdThis functions checks for NAs in a vector
+.checkData(input)A numeric vector
Nothing returned
+euclideanDist.RdCalculates Euclidean distance between two vectors. An error +will be given if NAs are present in either vector.
+euclideanDist(a, b, verbose = FALSE)The first vector to use in the distance calculation.
The second vector to use in the distance calculation.
Boolean. If TRUE, a message will be printed.
+Default TRUE.
A numeric value of a distance
+data(example_data)
+euclideanDist(example_data[,1], example_data[,2], verbose = FALSE)
+#> [1] 13.69805
+example_data.RdA dataset containing a matrix with two columns that were generated +with a random normal distribution with a mean of 0 and stdev of 1.
+data("example_data")A matrix with 100 rows and 2 columns
+data("example_data")
+hello.RdA simple function to print Hello world
+hello(withExcitement = TRUE)Boolean. If TRUE, then the message
+will be printed with exlamation points. Default TRUE.
A string with the hello world text
+hello(withExcitement = TRUE)
+#> [1] "Hello world!!!"
+
+ All functions+ + |
+ |
|---|---|
| + + | +Validity check |
+
| + + | +Euclidean distance |
+
| + + | +Example dataset |
+
| + + | +Hello world |
+