generated from UofUEpiBio/egpkg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
279 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
export(myplot) | ||
export(rcpp_hello_world) | ||
import(dplyr) | ||
import(graphics) | ||
importFrom(Rcpp,evalCpp) | ||
useDynLib(egpkg, .registration = TRUE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#' Add myplot function | ||
#' @param x A numeric vector. | ||
#' @param y A numeric vector. | ||
#' @return A plot of x and y. | ||
#' @examples | ||
# Here is an example | ||
#' set.seed(312) | ||
#' x <- rnorm(100) | ||
#' y <- rnorm(100) | ||
#' myplot(x, y) | ||
#' @import graphics | ||
#' @import dplyr | ||
#' @export | ||
# Here is the function | ||
myplot <- function(x, y) { | ||
|
||
if (!is.numeric(x) | !is.numeric(y)) { | ||
stop("x and y must be numeric") | ||
} | ||
|
||
plot(x, y, col = "blue", pch = 19, cex = 2) | ||
|
||
invisible( | ||
list( | ||
x = x, | ||
y = y | ||
) | ||
) | ||
|
||
} | ||
|
||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
* using log directory ‘/Users/u6046094/Desktop/Fall 24/PHS 7045 R Programming/Labs/R_package/egpkg-lab07/egpkg.Rproj.Rcheck’ | ||
* using R version 4.4.1 (2024-06-14) | ||
* using platform: aarch64-apple-darwin20 | ||
* R was compiled by | ||
Apple clang version 14.0.0 (clang-1400.0.29.202) | ||
GNU Fortran (GCC) 12.2.0 | ||
* running under: macOS Sonoma 14.6.1 | ||
* using session charset: UTF-8 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
#include <Rcpp.h> | ||
|
||
using namespace Rcpp; | ||
|
||
// [[Rcpp::export]] | ||
List ps_match1(const NumericVector & x) { | ||
|
||
int n = static_cast<int>(x.size()); | ||
|
||
IntegerVector indices(n); | ||
NumericVector values(n); | ||
|
||
for (int i = 0; i < n; ++i) { | ||
|
||
// Maximum value | ||
double cur_best = std::numeric_limits< double >::max(); | ||
int cur_i = 0; | ||
|
||
for (int j = 0; j < n; ++j) { | ||
|
||
// We can't compare to oneself | ||
if (i == j) | ||
continue; | ||
|
||
// If it is lower, then update | ||
if (std::abs(x[i] - x[j]) < cur_best) { | ||
|
||
cur_best = std::abs(x[i] - x[j]); | ||
cur_i = j; | ||
|
||
} | ||
|
||
} | ||
|
||
// In the end, we register the result | ||
indices[i] = cur_i; | ||
values[i] = x[cur_i]; | ||
|
||
} | ||
|
||
return List::create( | ||
_["match_id"] = indices + 1, // We add one to match R's indices | ||
_["match_x"] = values | ||
); | ||
|
||
} | ||
|
||
// [[Rcpp::export]] | ||
List ps_match2(const NumericVector & x) { | ||
|
||
int n = static_cast<int>(x.size()); | ||
|
||
IntegerVector indices(n); | ||
NumericVector values(n); | ||
|
||
for (int i = 0; i < n; ++i) { | ||
|
||
// Instead of allocating new memory, we can point by reference | ||
// (saves operations) | ||
double & cur_best = values[i]; | ||
int & cur_i = indices[i]; | ||
|
||
cur_best = std::numeric_limits< double >::max(); | ||
|
||
cur_i = 0; | ||
|
||
for (int j = 0; j < n; ++j) { | ||
|
||
// We can't compare to oneself | ||
if (i == j) | ||
continue; | ||
|
||
// If it is lower, then update | ||
if (std::abs(x[i] - x[j]) < cur_best) { | ||
|
||
cur_best = std::abs(x[i] - x[j]); | ||
cur_i = j; | ||
|
||
} | ||
|
||
} | ||
|
||
} | ||
|
||
for (int i = 0; i < n; ++i) | ||
values[i] = x[indices[i]]; | ||
|
||
return List::create( | ||
_["match_id"] = indices + 1, // We add one to match R's indices | ||
_["match_x"] = values | ||
); | ||
|
||
} | ||
|
||
// [[Rcpp::export]] | ||
List ps_match3(const NumericVector & x) { | ||
|
||
int n = static_cast<int>(x.size()); | ||
|
||
IntegerVector indices(n); | ||
NumericVector values(n); | ||
values.fill(std::numeric_limits< double >::max()); | ||
|
||
for (int i = 0; i < n; ++i) { | ||
|
||
// Instead of allocating new memory, we can point by reference | ||
// (saves operations) | ||
double & cur_best = values[i]; | ||
auto & cur_i = indices[i]; | ||
|
||
for (int j = 0; j < i; ++j) { | ||
|
||
// If it is lower, then update | ||
double d = std::abs(x[i] - x[j]); | ||
if (d < cur_best) { | ||
|
||
cur_best = d; | ||
cur_i = j; | ||
|
||
} | ||
|
||
if (d < values[j]) { | ||
|
||
values[j] = d; | ||
indices[j] = i; | ||
|
||
} | ||
|
||
} | ||
|
||
} | ||
|
||
for (int i = 0; i < n; ++i) | ||
values[i] = x[indices[i]]; | ||
|
||
return List::create( | ||
_["match_id"] = indices + 1, // We add one to match R's indices | ||
_["match_x"] = values | ||
); | ||
|
||
} | ||
|
||
|
||
/***R | ||
set.seed(1231) | ||
x <- cbind(runif(5)) | ||
ps_matchR <- function(x) { | ||
match_expected <- dist(x) |> as.matrix() | ||
diag(match_expected) <- .Machine$integer.max | ||
indices <- apply(match_expected, 1, which.min) | ||
list( | ||
match_id = as.integer(unname(indices)), | ||
match_x = x[indices] | ||
) | ||
} | ||
resR <- ps_matchR(x) | ||
res1 <- ps_match1(x) | ||
res2 <- ps_match2(x) | ||
res3 <- ps_match3(x) | ||
cbind( | ||
X = x, | ||
R = resR$match_id, | ||
Rcpp1 = res$match_id, | ||
Rcpp2 = res2$match_id, | ||
Rcpp3 = res3$match_id | ||
) |> head() | ||
# Benchmarking | ||
x <- cbind(runif(10000)) | ||
bench::mark( | ||
resR = ps_matchR(x)$match_x, | ||
res1 = ps_match1(x)$match_x, | ||
res2 = ps_match2(x)$match_x, | ||
res3 = ps_match3(x)$match_x, | ||
relative = TRUE | ||
) | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# 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/testing-design.html#sec-tests-files-overview | ||
# * https://testthat.r-lib.org/articles/special-files.html | ||
|
||
library(testthat) | ||
library(egpkg) | ||
|
||
test_check("egpkg") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# test add_plot function | ||
# when input is not numeric | ||
|
||
library(testthat) | ||
library(egpkg) | ||
|
||
test_that("add_plot function", { | ||
expect_error(myplot("a", "b"), "x and y must be numeric") | ||
}) |