Skip to content

Commit e341126

Browse files
ASA-150: R skeleton
1 parent f84794a commit e341126

16 files changed

+868
-4
lines changed

.gitignore

Lines changed: 506 additions & 4 deletions
Large diffs are not rendered by default.

r-package/.Rbuildignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
^renv$
2+
^renv\.lock$
3+
^.*\.Rproj$
4+
^\.Rproj\.user$

r-package/.Rprofile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
source("renv/activate.R")

r-package/DESCRIPTION

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Package: itscalledsoccer
2+
Type: Package
3+
Title: What the Package Does (Title Case)
4+
Version: 0.1.0
5+
Author: Who wrote it
6+
Maintainer: The package maintainer <[email protected]>
7+
Description: More about what it does (maybe more than one line)
8+
Use four spaces when indenting paragraphs within the Description.
9+
License: What license is it under?
10+
Encoding: UTF-8
11+
LazyData: true
12+
Imports: Rcpp (>= 1.0.3)
13+
LinkingTo: Rcpp

r-package/NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
exportPattern("^[[:alpha:]]+")
2+
importFrom(Rcpp, evalCpp)
3+
useDynLib(itscalledsoccer, .registration = TRUE)

r-package/R/RcppExports.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
2+
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
3+
4+
rcpp_hello <- function() {
5+
.Call(`_itscalledsoccer_rcpp_hello`)
6+
}
7+

r-package/R/hello.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Hello, world!
2+
#
3+
# This is an example function named 'hello'
4+
# which prints 'Hello, world!'.
5+
#
6+
# You can learn more about package authoring with RStudio at:
7+
#
8+
# http://r-pkgs.had.co.nz/
9+
#
10+
# Some useful keyboard shortcuts for package authoring:
11+
#
12+
# Install Package: 'Cmd + Shift + B'
13+
# Check Package: 'Cmd + Shift + E'
14+
# Test Package: 'Cmd + Shift + T'
15+
16+
hello <- function() {
17+
print("Hello, world!")
18+
}

r-package/itscalledsoccer.Rproj

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Version: 1.0
2+
3+
RestoreWorkspace: Default
4+
SaveWorkspace: Default
5+
AlwaysSaveHistory: Default
6+
7+
EnableCodeIndexing: Yes
8+
UseSpacesForTab: Yes
9+
NumSpacesForTab: 4
10+
Encoding: UTF-8
11+
12+
RnwWeave: Sweave
13+
LaTeX: pdfLaTeX
14+
15+
AutoAppendNewline: Yes
16+
StripTrailingWhitespace: Yes
17+
18+
BuildType: Package
19+
PackageUseDevtools: Yes
20+
PackageInstallArgs: --no-multiarch --with-keep.source

r-package/man/hello.Rd

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
\name{hello}
2+
\alias{hello}
3+
\title{Hello, World!}
4+
\usage{
5+
hello()
6+
}
7+
\description{
8+
Prints 'Hello, world!'.
9+
}
10+
\examples{
11+
hello()
12+
}

r-package/man/rcpp_hello.Rd

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
\name{rcpp_hello}
2+
\alias{rcpp_hello}
3+
\title{Hello, Rcpp!}
4+
\usage{
5+
rcpp_hello()
6+
}
7+
\description{
8+
Returns an \R \code{list} containing the character vector
9+
\code{c("foo", "bar")} and the numeric vector \code{c(0, 1)}.
10+
}
11+
\examples{
12+
rcpp_hello()
13+
}

r-package/renv.lock

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"R": {
3+
"Version": "3.6.2",
4+
"Repositories": [
5+
{
6+
"Name": "CRAN",
7+
"URL": "https://cran.rstudio.com"
8+
}
9+
]
10+
},
11+
"Packages": {
12+
"Rcpp": {
13+
"Package": "Rcpp",
14+
"Version": "1.0.3",
15+
"Source": "Repository",
16+
"Repository": "CRAN",
17+
"Hash": "f3ca785924863b0e4c8cb23b6a5c75a1"
18+
},
19+
"renv": {
20+
"Package": "renv",
21+
"Version": "0.9.3",
22+
"Source": "Repository",
23+
"Repository": "CRAN",
24+
"Hash": "c1a367437d8a8a44bec4b9d4974cb20c"
25+
}
26+
}
27+
}

r-package/renv/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
library/
2+
python/
3+
staging/

r-package/renv/activate.R

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
2+
local({
3+
4+
# the requested version of renv
5+
version <- "0.9.3"
6+
7+
# avoid recursion
8+
if (!is.na(Sys.getenv("RENV_R_INITIALIZING", unset = NA)))
9+
return(invisible(TRUE))
10+
11+
# signal that we're loading renv during R startup
12+
Sys.setenv("RENV_R_INITIALIZING" = "true")
13+
on.exit(Sys.unsetenv("RENV_R_INITIALIZING"), add = TRUE)
14+
15+
# signal that we've consented to use renv
16+
options(renv.consent = TRUE)
17+
18+
# load the 'utils' package eagerly -- this ensures that renv shims, which
19+
# mask 'utils' packages, will come first on the search path
20+
library(utils, lib.loc = .Library)
21+
22+
# check to see if renv has already been loaded
23+
if ("renv" %in% loadedNamespaces()) {
24+
25+
# if renv has already been loaded, and it's the requested version of renv,
26+
# nothing to do
27+
spec <- .getNamespaceInfo(.getNamespace("renv"), "spec")
28+
if (identical(spec[["version"]], version))
29+
return(invisible(TRUE))
30+
31+
# otherwise, unload and attempt to load the correct version of renv
32+
unloadNamespace("renv")
33+
34+
}
35+
36+
# construct path to renv in library
37+
libpath <- local({
38+
39+
root <- Sys.getenv("RENV_PATHS_LIBRARY", unset = "renv/library")
40+
prefix <- paste("R", getRversion()[1, 1:2], sep = "-")
41+
42+
# include SVN revision for development versions of R
43+
# (to avoid sharing platform-specific artefacts with released versions of R)
44+
devel <-
45+
identical(R.version[["status"]], "Under development (unstable)") ||
46+
identical(R.version[["nickname"]], "Unsuffered Consequences")
47+
48+
if (devel)
49+
prefix <- paste(prefix, R.version[["svn rev"]], sep = "-r")
50+
51+
file.path(root, prefix, R.version$platform)
52+
53+
})
54+
55+
# try to load renv from the project library
56+
if (requireNamespace("renv", lib.loc = libpath, quietly = TRUE)) {
57+
58+
# warn if the version of renv loaded does not match
59+
loadedversion <- utils::packageDescription("renv", fields = "Version")
60+
if (version != loadedversion) {
61+
62+
# assume four-component versions are from GitHub; three-component
63+
# versions are from CRAN
64+
components <- strsplit(loadedversion, "[.-]")[[1]]
65+
remote <- if (length(components) == 4L)
66+
paste("rstudio/renv", loadedversion, sep = "@")
67+
else
68+
paste("renv", loadedversion, sep = "@")
69+
70+
fmt <- paste(
71+
"renv %1$s was loaded from project library, but renv %2$s is recorded in lockfile.",
72+
"Use `renv::record(\"%3$s\")` to record this version in the lockfile.",
73+
"Use `renv::restore(packages = \"renv\")` to install renv %2$s into the project library.",
74+
sep = "\n"
75+
)
76+
77+
msg <- sprintf(fmt, loadedversion, version, remote)
78+
warning(msg, call. = FALSE)
79+
80+
}
81+
82+
# load the project
83+
return(renv::load())
84+
85+
}
86+
87+
# failed to find renv locally; we'll try to install from GitHub.
88+
# first, set up download options as appropriate (try to use GITHUB_PAT)
89+
install_renv <- function() {
90+
91+
message("Failed to find installation of renv -- attempting to bootstrap...")
92+
93+
# ensure .Rprofile doesn't get executed
94+
rpu <- Sys.getenv("R_PROFILE_USER", unset = NA)
95+
Sys.setenv(R_PROFILE_USER = "<NA>")
96+
on.exit({
97+
if (is.na(rpu))
98+
Sys.unsetenv("R_PROFILE_USER")
99+
else
100+
Sys.setenv(R_PROFILE_USER = rpu)
101+
}, add = TRUE)
102+
103+
# prepare download options
104+
pat <- Sys.getenv("GITHUB_PAT")
105+
if (nzchar(Sys.which("curl")) && nzchar(pat)) {
106+
fmt <- "--location --fail --header \"Authorization: token %s\""
107+
extra <- sprintf(fmt, pat)
108+
saved <- options("download.file.method", "download.file.extra")
109+
options(download.file.method = "curl", download.file.extra = extra)
110+
on.exit(do.call(base::options, saved), add = TRUE)
111+
} else if (nzchar(Sys.which("wget")) && nzchar(pat)) {
112+
fmt <- "--header=\"Authorization: token %s\""
113+
extra <- sprintf(fmt, pat)
114+
saved <- options("download.file.method", "download.file.extra")
115+
options(download.file.method = "wget", download.file.extra = extra)
116+
on.exit(do.call(base::options, saved), add = TRUE)
117+
}
118+
119+
# fix up repos
120+
repos <- getOption("repos")
121+
on.exit(options(repos = repos), add = TRUE)
122+
repos[repos == "@CRAN@"] <- "https://cloud.r-project.org"
123+
options(repos = repos)
124+
125+
# check for renv on CRAN matching this version
126+
db <- as.data.frame(available.packages(), stringsAsFactors = FALSE)
127+
if ("renv" %in% rownames(db)) {
128+
entry <- db["renv", ]
129+
if (identical(entry$Version, version)) {
130+
message("* Installing renv ", version, " ... ", appendLF = FALSE)
131+
dir.create(libpath, showWarnings = FALSE, recursive = TRUE)
132+
utils::install.packages("renv", lib = libpath, quiet = TRUE)
133+
message("Done!")
134+
return(TRUE)
135+
}
136+
}
137+
138+
# try to download renv
139+
message("* Downloading renv ", version, " ... ", appendLF = FALSE)
140+
prefix <- "https://api.github.com"
141+
url <- file.path(prefix, "repos/rstudio/renv/tarball", version)
142+
destfile <- tempfile("renv-", fileext = ".tar.gz")
143+
on.exit(unlink(destfile), add = TRUE)
144+
utils::download.file(url, destfile = destfile, mode = "wb", quiet = TRUE)
145+
message("Done!")
146+
147+
# attempt to install it into project library
148+
message("* Installing renv ", version, " ... ", appendLF = FALSE)
149+
dir.create(libpath, showWarnings = FALSE, recursive = TRUE)
150+
151+
# invoke using system2 so we can capture and report output
152+
bin <- R.home("bin")
153+
exe <- if (Sys.info()[["sysname"]] == "Windows") "R.exe" else "R"
154+
r <- file.path(bin, exe)
155+
args <- c("--vanilla", "CMD", "INSTALL", "-l", shQuote(libpath), shQuote(destfile))
156+
output <- system2(r, args, stdout = TRUE, stderr = TRUE)
157+
message("Done!")
158+
159+
# check for successful install
160+
status <- attr(output, "status")
161+
if (is.numeric(status) && !identical(status, 0L)) {
162+
text <- c("Error installing renv", "=====================", output)
163+
writeLines(text, con = stderr())
164+
}
165+
166+
167+
}
168+
169+
try(install_renv())
170+
171+
# try again to load
172+
if (requireNamespace("renv", lib.loc = libpath, quietly = TRUE)) {
173+
message("Successfully installed and loaded renv ", version, ".")
174+
return(renv::load())
175+
}
176+
177+
# failed to download or load renv; warn the user
178+
msg <- c(
179+
"Failed to find an renv installation: the project will not be loaded.",
180+
"Use `renv::activate()` to re-initialize the project."
181+
)
182+
183+
warning(paste(msg, collapse = "\n"), call. = FALSE)
184+
185+
})

r-package/renv/settings.dcf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
external.libraries:
2+
ignored.packages:
3+
package.dependency.fields: Imports, Depends, LinkingTo
4+
snapshot.type: packrat
5+
use.cache: TRUE
6+
vcs.ignore.library: TRUE

r-package/src/RcppExports.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Generated by using Rcpp::compileAttributes() -> do not edit by hand
2+
// Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
3+
4+
#include <Rcpp.h>
5+
6+
using namespace Rcpp;
7+
8+
// rcpp_hello
9+
List rcpp_hello();
10+
RcppExport SEXP _itscalledsoccer_rcpp_hello() {
11+
BEGIN_RCPP
12+
Rcpp::RObject rcpp_result_gen;
13+
Rcpp::RNGScope rcpp_rngScope_gen;
14+
rcpp_result_gen = Rcpp::wrap(rcpp_hello());
15+
return rcpp_result_gen;
16+
END_RCPP
17+
}
18+
19+
static const R_CallMethodDef CallEntries[] = {
20+
{"_itscalledsoccer_rcpp_hello", (DL_FUNC) &_itscalledsoccer_rcpp_hello, 0},
21+
{NULL, NULL, 0}
22+
};
23+
24+
RcppExport void R_init_itscalledsoccer(DllInfo *dll) {
25+
R_registerRoutines(dll, NULL, CallEntries, NULL, NULL);
26+
R_useDynamicSymbols(dll, FALSE);
27+
}

r-package/src/rcpp_hello.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <Rcpp.h>
2+
using namespace Rcpp;
3+
4+
// This is a simple function using Rcpp that creates an R list
5+
// containing a character vector and a numeric vector.
6+
//
7+
// Learn more about how to use Rcpp at:
8+
//
9+
// http://www.rcpp.org/
10+
// http://adv-r.had.co.nz/Rcpp.html
11+
//
12+
// and browse examples of code using Rcpp at:
13+
//
14+
// http://gallery.rcpp.org/
15+
//
16+
17+
// [[Rcpp::export]]
18+
List rcpp_hello() {
19+
CharacterVector x = CharacterVector::create("foo", "bar");
20+
NumericVector y = NumericVector::create(0.0, 1.0);
21+
List z = List::create(x, y);
22+
return z;
23+
}

0 commit comments

Comments
 (0)