File: Rcpp.package.skeleton.R

package info (click to toggle)
rcpp 1.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,480 kB
  • sloc: cpp: 27,436; ansic: 7,778; sh: 53; makefile: 2
file content (266 lines) | stat: -rw-r--r-- 11,404 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# Copyright (C) 2009 - 2025  Dirk Eddelbuettel and Romain Francois
#
# This file is part of Rcpp.
#
# Rcpp is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# Rcpp is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.

Rcpp.package.skeleton <- function(name = "anRpackage", list = character(),
                                  environment = .GlobalEnv,
                                  path = ".", force = FALSE,
                                  code_files = character(), cpp_files = character(),
                                  example_code = TRUE, attributes = TRUE, module = FALSE,
                                  author = "Your Name",
                                  maintainer = if (missing(author)) "Your Name"
                                               else author,
                                  email = "your@email.com",
                                  githubuser = NA_character_,
                                  license = "GPL (>= 2)") {

    havePkgKitten <- requireNamespace("pkgKitten", quietly=TRUE)

    call <- match.call()
    call[[1]] <- as.name("package.skeleton")
    env <- parent.frame(1)

    if (!is.character(cpp_files))
        stop("'cpp_files' must be a character vector")				# #nocov

    if (!length(list)) {
        fake <- TRUE
        assign("Rcpp.fake.fun", function() {}, envir = env)
        if (example_code && !isTRUE(attributes)) {
            assign("rcpp_hello_world", function() {}, envir = env)
            remove_hello_world <- TRUE
        } else {
            remove_hello_world <- FALSE
        }
    } else {
        if (example_code && !isTRUE(attributes)) {
            if (!"rcpp_hello_world" %in% list) {					# #nocov start
                assign( "rcpp_hello_world", function() {}, envir = env)
                call[["list"]] <- as.call(c(as.name("c"),
                                            as.list(c("rcpp_hello_world", list))))
            }
            remove_hello_world <- TRUE								# #nocov end
        } else {
            remove_hello_world <- FALSE
        }
        fake <- FALSE
    }

    ## first let the traditional version do its business
    ## remove Rcpp specific arguments

    call <- call[ c(1L, which(names(call) %in% names(formals(package.skeleton)))) ]

    if (fake) {
        call[["list"]] <- c(if(isTRUE(example_code)
                               && !isTRUE(attributes)) "rcpp_hello_world", "Rcpp.fake.fun")
    }

    tryCatch(eval(call, envir = env), error = function(e){
        stop(sprintf("error while calling `package.skeleton` : %s", conditionMessage(e)))	# #nocov
    })

    message("\nAdding Rcpp settings")

    ## now pick things up
    root <- file.path(path, name)

    # Add Rcpp to the DESCRIPTION
    DESCRIPTION <- file.path(root, "DESCRIPTION")
    if (file.exists(DESCRIPTION)) {
        imports <- c(if (isTRUE(module)) "methods", sprintf("Rcpp (>= %s)", getRcppVersion()))
        splitname <- strsplit(author, " ")[[1]]
        x <- cbind(read.dcf(DESCRIPTION,  fields = c("Package", "Type", "Title", "Version", "Date",
                                                     "Description", "License")),
                   "Imports" = paste(imports, collapse = ", "),
                   "LinkingTo" = "Rcpp",
                   "Authors@R" = sprintf("person(\"%s\", \"%s\", role = c(\"aut\", \"cre\"), email = \"%s\")",
                                         paste(splitname[-length(splitname)], collapse=" "),
                                         splitname[length(splitname)],
                                         email))
        fields_written <- c("Package", "Type", "Title", "Version", "Date",
                            "Authors@R", "Description", "License", "Imports", "LinkingTo")
        if (!is.na(githubuser)) {
            x <- cbind(x, matrix("", 1, 1, dimnames=list("", "URL")))
            x[1, "URL"] <- paste0("https://github.com/", githubuser, "/", name)
            x <- cbind(x, matrix("", 1, 1, dimnames=list("", "BugReports")))
            x[1, "BugReports"] <- paste0("https://github.com/", githubuser, "/", name, "/issues")

            fields_written <- c("Package", "Type", "Title", "Version", "Date",
                                "Authors@R", "Description", "URL", "BugReports",
                                "License", "Imports", "LinkingTo")
            }

        x[, "License"] <- license
        x[, "Title"] <- "Concise Summary of What the Package Does"
        x[, "Description"] <- "More about what it does (maybe more than one line)."
        x[, "Version"] <- "0.0.1"
        message( " >> added Imports: Rcpp" )
        message( " >> added LinkingTo: Rcpp" )
        write.dcf(x[1, fields_written, drop = FALSE], file = DESCRIPTION)
    }

    ## add useDynLib and importFrom to NAMESPACE
    NAMESPACE <- file.path(root, "NAMESPACE")
    lines <- readLines(NAMESPACE)
    ns <- file(NAMESPACE, open="w")
    if (!any(grepl("useDynLib", lines))) {
        if (getRversion() >= "3.4.0") {
            lines <- c(sprintf( "useDynLib(%s, .registration=TRUE)", name), lines)
        } else {
            lines <- c(sprintf( "useDynLib(%s)", name), lines)				# #nocov
        }
        writeLines(lines, con = ns)
        message(" >> added useDynLib directive to NAMESPACE" )
    }
    if (isTRUE(module)) {
        writeLines('import(methods, Rcpp)', ns)
        message(" >> added import(methods, Rcpp) directive to NAMESPACE")
    } else {
        writeLines('importFrom(Rcpp, evalCpp)', ns)
        message(" >> added importFrom(Rcpp, evalCpp) directive to NAMESPACE" )
    }
    if (!any(grepl("^exportPattern", lines))) {
        writeLines("exportPattern(\"^[[:alpha:]]+\")", ns)
    }
    close( ns )

    ## update the package description help page
    if (havePkgKitten) {                # if pkgKitten is available, use it
        pkgKitten::playWithPerPackageHelpPage(name, path, maintainer, email)
    } else {
        .playWithPerPackageHelpPage(name, path, maintainer, email)			# #nocov
    }

    ## lay things out in the src directory
    src <- file.path(root, "src")
    if (!file.exists(src)) {
        dir.create(src)
    }
    skeleton <- system.file("skeleton", package = "Rcpp")

    if (length(cpp_files) > 0L) {
        for (file in cpp_files) {											# #nocov start
            file.copy(file, src)
            message(" >> copied ", file, " to src directory" )				# #nocov end
        }
    }

    if (example_code) {
        if (isTRUE(attributes)) {
            file.copy(file.path( skeleton, "rcpp_hello_world_attributes.cpp"),
                      file.path( src, "rcpp_hello_world.cpp"))
            message(" >> added example src file using Rcpp attributes")
        } else {
            header <- readLines(file.path(skeleton, "rcpp_hello_world.h"))
            header <- gsub("@PKG@", name, header, fixed = TRUE)
            writeLines(header , file.path(src, "rcpp_hello_world.h"))
            message(" >> added example header file using Rcpp classes")

            file.copy(file.path(skeleton, "rcpp_hello_world.cpp"), src)
            message(" >> added example src file using Rcpp classes")

            rcode <- readLines(file.path( skeleton, "rcpp_hello_world.R"))
            rcode <- gsub("@PKG@", name, rcode, fixed = TRUE)
            writeLines( rcode , file.path( root, "R", "rcpp_hello_world.R"))
            message(" >> added example R file calling the C++ example")
        }

        hello.Rd <- file.path(root, "man", "rcpp_hello_world.Rd")
        unlink(hello.Rd)
        file.copy(system.file("skeleton", "rcpp_hello_world.Rd", package = "Rcpp"), hello.Rd)
        message( " >> added Rd file for rcpp_hello_world")
    }

    if (isTRUE(module)) {
        file.copy(system.file("skeleton", "rcpp_module.cpp", package="Rcpp"),
                  file.path(root, "src"))
        file.copy(system.file("skeleton", "Num.cpp", package="Rcpp"),
                  file.path(root, "src"))
        file.copy(system.file("skeleton", "stdVector.cpp", package="Rcpp"),
                  file.path(root, "src"))
        file.copy(system.file("skeleton", "zzz.R", package ="Rcpp"),
                  file.path(root, "R"))
        file.copy(system.file("skeleton", "Rcpp_modules_examples.Rd", package ="Rcpp"),
                  file.path(root, "man"))
        message(" >> copied the example module file ")
    }

    # generate native routines if we aren't using attributes (which already generate
    # them automatically) and we have at least R 3.4
    if (!attributes) {
        if (getRversion() >= "3.4.0") {
            con <- file(file.path(src, "init.c"), "wt")
            tools::package_native_routine_registration_skeleton(root, con=con)
            close(con)
            message(" >> created init.c for package registration")
        } else {
            message(" >> R version older than 3.4.0 detected, so NO file init.c created.")	# #nocov
        }
    }

    lines <- readLines(package.doc <- file.path( root, "man", sprintf("%s-package.Rd", name)))
    lines <- sub("~~ simple examples", "%% ~~ simple examples", lines)

    lines <- lines[! grepl("~~ package title", lines)]
    lines <- lines[! grepl("~~ The author and", lines)]
    lines <- sub("Who wrote it", author, lines )
    lines <- sub("Who to complain to.*", sprintf("%s <%s>", maintainer, email), lines)

    writeLines(lines, package.doc)

    if (fake) {
        rm("Rcpp.fake.fun", envir = env)
        unlink(file.path(root, "R"  , "Rcpp.fake.fun.R"))
        unlink(file.path(root, "man", "Rcpp.fake.fun.Rd"))

        ## cleansing NAMESPACE of fake function entry
        lines <- readLines(NAMESPACE)
        lines <- lines[!grepl("^export.*fake\\.fun", lines)]
        writeLines(lines, NAMESPACE)
    }

    if (isTRUE(remove_hello_world)) {
        rm("rcpp_hello_world", envir = env)
    }

    if (attributes) {
        compileAttributes(root)
        message(" >> compiled Rcpp attributes ")
    }

    invisible(NULL)
}

## Borrowed with love from pkgKitten, and modified slightly
.playWithPerPackageHelpPage <- function(name = "anRpackage",
                                        path = ".",
                                        maintainer = "Your Name",
                                        email = "your@mail.com") {
    root <- file.path(path, name)										# #nocov start
    helptgt <- file.path(root, "man", sprintf( "%s-package.Rd", name))
    helpsrc <- system.file("skeleton", "manual-page-stub.Rd", package="Rcpp")
    ## update the package description help page
    if (file.exists(helpsrc)) {
        lines <- readLines(helpsrc)
        lines <- gsub("__placeholder__", name, lines, fixed = TRUE)
        lines <- gsub("Who to complain to <yourfault@somewhere.net>",
                      sprintf( "%s <%s>", maintainer, email),
                      lines, fixed = TRUE)
        writeLines(lines, helptgt)
    }
    invisible(NULL)														# #nocov end
}