Skip to content

Commit a30e52f

Browse files
committed
build: simple R package with swig wrapper for C++
0 parents  commit a30e52f

18 files changed

+2929
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.o
2+
*.so
3+
.Rhistory

DESCRIPTION

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Package: swigr
2+
Title: What the Package Does (One Line, Title Case)
3+
Version: 0.0.0.9000
4+
Authors@R:
5+
person(given = "First",
6+
family = "Last",
7+
role = c("aut", "cre"),
8+
email = "first.last@example.com",
9+
comment = c(ORCID = "YOUR-ORCID-ID"))
10+
Description: What the package does (one paragraph).
11+
License: What license it uses
12+
Encoding: UTF-8
13+
LazyData: true
14+
RoxygenNote: 7.1.0
15+
Collate:
16+
'swigr.R'
17+
'package.R'
18+
Suggests:
19+
knitr,
20+
rmarkdown
21+
VignetteBuilder: knitr

Makefile

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
build: src/Shape_wrap.cpp
3+
R CMD SHLIB -o src/Shape.so src/swigr_wrap.cpp src/Shape.cpp
4+
5+
src/Shape_wrap.cpp:
6+
swig -c++ -r -o src/swigr_wrap.cpp src/Shape.i
7+
@mv src/swigr.R R/
8+
9+
.PHONY: create document install test-R clean
10+
create:
11+
cd .. && R --no-save -e 'devtools::create("swigr")'
12+
13+
document:
14+
R --no-save -e 'library(devtools); library(roxygen2); document()'
15+
16+
install:
17+
cd .. && R --no-save -e 'devtools::install("swigr")'
18+
19+
test-R:
20+
pwd
21+
cd $(R_PACKAGE) && R --no-save -e 'library(devtools); library(roxygen2); load_all(); test()'
22+
23+
clean:
24+
$(RM) src/*.so src/*.o src/*.R src/*_wrap.cpp

NAMESPACE

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Generated by roxygen2: do not edit by hand
2+
3+
export(test_example)
4+
export(test_r)
5+
exportPattern("^[[:alpha:]]+")
6+
useDynLib(swigr, .registration=TRUE)

R/package.R

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#' swigr
2+
#'
3+
#' R package to test SWIG
4+
#'
5+
#' @docType package
6+
#' @author you
7+
#' @useDynLib swigr, .registration=TRUE
8+
#' @exportPattern "^[[:alpha:]]+"
9+
#' @name swigr
10+
#' @include swigr.R
11+
NULL
12+
13+
14+
#' @export
15+
test_r <- function() {
16+
print("test_r works!")
17+
42
18+
}
19+
20+
#' Test example shapes
21+
#' @param r Circle radius
22+
#' @export
23+
test_example <- function(r=10) {
24+
print("Creating some objects:")
25+
circle <- Circle(r)
26+
print (" Created circle")
27+
square <- Square(20)
28+
print (" Created square")
29+
30+
# ----- Access a static member -----
31+
32+
sprintf("A total of %d shapes were created", Shape_nshapes())
33+
34+
# ----- Member data access -----
35+
36+
# Set the location of the object
37+
38+
circle$x <- 20
39+
circle$y <- 30
40+
41+
square$x <- -10
42+
square$y <- 5
43+
44+
print("Here is their current position:")
45+
sprintf(" Circle = (%f, %f)", circle$x,circle$y)
46+
sprintf(" Square = (%f, %f)", square$x,square$y)
47+
48+
# ----- Call some methods -----
49+
50+
circle$move(10, 20)
51+
sprintf("Cicle moved to (%f, %f)", circle$x,circle$y)
52+
}

0 commit comments

Comments
 (0)