Skip to content

Commit 71eb020

Browse files
committed
new argument 'echo' (fixes #1126)
1 parent 2868f38 commit 71eb020

File tree

3 files changed

+49
-40
lines changed

3 files changed

+49
-40
lines changed

R/Attributes.R

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ sourceCpp <- function(file = "",
2828
showOutput = verbose,
2929
verbose = getOption("verbose"),
3030
dryRun = FALSE,
31-
windowsDebugDLL = FALSE) {
31+
windowsDebugDLL = FALSE,
32+
echo = TRUE) {
3233

3334
# use an architecture/version specific subdirectory of the cacheDir
3435
# (since cached dynlibs can now perist across sessions we need to be
@@ -214,7 +215,7 @@ sourceCpp <- function(file = "",
214215
if (embeddedR && (length(context$embeddedR) > 0)) {
215216
srcConn <- textConnection(context$embeddedR)
216217
setwd(rWorkingDir) # will be reset by previous on.exit handler
217-
source(file = srcConn, local = env, echo = TRUE)
218+
source(file = srcConn, local = env, echo = echo)
218219
}
219220

220221
# cleanup the cache dir if requested
@@ -258,7 +259,8 @@ cppFunction <- function(code,
258259
rebuild = FALSE,
259260
cacheDir = getOption("rcpp.cache.dir", tempdir()),
260261
showOutput = verbose,
261-
verbose = getOption("verbose")) {
262+
verbose = getOption("verbose"),
263+
echo = TRUE) {
262264

263265
# process depends
264266
if (!is.null(depends) && length(depends) > 0) { # #nocov start
@@ -315,7 +317,8 @@ cppFunction <- function(code,
315317
rebuild = rebuild,
316318
cacheDir = cacheDir,
317319
showOutput = showOutput,
318-
verbose = verbose)
320+
verbose = verbose,
321+
echo = echo)
319322

320323
# verify that a single function was exported and return it
321324
if (length(exported$functions) == 0)

man/cppFunction.Rd

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ Dynamically define an R function with C++ source code. Compiles and links a shar
88
}
99
\usage{
1010
cppFunction(code, depends = character(), plugins = character(), includes = character(),
11-
env = parent.frame(), rebuild = FALSE, cacheDir = getOption("rcpp.cache.dir",
12-
tempdir()), showOutput = verbose, verbose = getOption("verbose"))
11+
env = parent.frame(), rebuild = FALSE, cacheDir = getOption("rcpp.cache.dir",
12+
tempdir()), showOutput = verbose, verbose = getOption("verbose"), echo = TRUE)
1313
}
1414

1515
\arguments{
@@ -39,11 +39,14 @@ cppFunction(code, depends = character(), plugins = character(), includes = chara
3939
}
4040
\item{verbose}{
4141
\code{TRUE} to print detailed information about generated code to the console.
42+
}
43+
\item{echo}{
44+
\code{TRUE} to silence output from optional R evaluation if set to \code{FALSE}.
4245
}
4346
}
4447
\details{
4548
Functions defined using \code{cppFunction} must have return types that are compatible with \code{Rcpp::wrap} and parameter types that are compatible with \code{Rcpp::as}.
46-
49+
4750
The shared library will not be rebuilt if the underlying code has not changed since the last compilation.
4851
}
4952
\value{
@@ -63,36 +66,36 @@ cppFunction(code, depends = character(), plugins = character(), includes = chara
6366

6467
cppFunction(
6568
'int fibonacci(const int x) {
66-
if (x == 0) return(0);
69+
if (x == 0) return(0);
6770
if (x == 1) return(1);
6871
return (fibonacci(x - 1)) + fibonacci(x - 2);
6972
}')
7073

7174
cppFunction(depends = "RcppArmadillo",
7275
'List fastLm(NumericVector yr, NumericMatrix Xr) {
73-
76+
7477
int n = Xr.nrow(), k = Xr.ncol();
75-
76-
arma::mat X(Xr.begin(), n, k, false);
78+
79+
arma::mat X(Xr.begin(), n, k, false);
7780
arma::colvec y(yr.begin(), yr.size(), false);
78-
81+
7982
arma::colvec coef = arma::solve(X, y);
8083
arma::colvec resid = y - X*coef;
81-
84+
8285
double sig2 = arma::as_scalar(arma::trans(resid)*resid/(n-k) );
8386
arma::colvec stderrest = arma::sqrt(
8487
sig2 * arma::diagvec(arma::inv(arma::trans(X)*X)));
85-
88+
8689
return List::create(Named("coefficients") = coef,
8790
Named("stderr") = stderrest
8891
);
8992
}')
90-
93+
9194
cppFunction(plugins=c("cpp11"), '
9295
int useCpp11() {
9396
auto x = 10;
9497
return x;
9598
}')
96-
99+
97100
}
98101
}

man/sourceCpp.Rd

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ Source C++ Code from a File or String
88
and RCPP_MODULE declarations. A shared library is then built and its exported functions and Rcpp modules are made available in the specified environment.
99
}
1010
\usage{
11-
sourceCpp(file = "", code = NULL, env = globalenv(), embeddedR = TRUE, rebuild = FALSE,
11+
sourceCpp(file = "", code = NULL, env = globalenv(), embeddedR = TRUE, rebuild = FALSE,
1212
cacheDir = getOption("rcpp.cache.dir", tempdir()), cleanupCacheDir = FALSE,
1313
showOutput = verbose, verbose = getOption("verbose"), dryRun = FALSE,
14-
windowsDebugDLL = FALSE)
14+
windowsDebugDLL = FALSE, echo = TRUE)
1515
}
1616
\arguments{
1717
\item{file}{
1818
A character string giving the path name of a file
1919
}
2020
\item{code}{
21-
A character string with source code. If supplied, the code is taken from this string instead of a file.
21+
A character string with source code. If supplied, the code is taken from this string instead of a file.
2222
}
2323
\item{env}{
2424
Environment where the R functions and modules should be made available.
@@ -47,30 +47,33 @@ actually executing the commands).
4747
}
4848
\item{windowsDebugDLL}{
4949
\code{TRUE} to create a debug DLL on Windows (and ignored on other platforms).
50+
}
51+
\item{echo}{
52+
\code{TRUE} to silence output from optional R evaluation if set to \code{FALSE}.
5053
}
5154
}
5255
\details{
5356
If the \code{code} parameter is provided then the \code{file} parameter is ignored.
5457

55-
Functions exported using \code{sourceCpp} must meet several conditions,
56-
including being defined in the global namespace and having return types
57-
that are compatible with \code{Rcpp::wrap} and parameter types that are
58-
compatible with \code{Rcpp::as}.
58+
Functions exported using \code{sourceCpp} must meet several conditions,
59+
including being defined in the global namespace and having return types
60+
that are compatible with \code{Rcpp::wrap} and parameter types that are
61+
compatible with \code{Rcpp::as}.
5962
See the \code{\link[=exportAttribute]{Rcpp::export}} documentation for more details.
60-
61-
Content of Rcpp Modules will be automatically loaded into the specified
62-
environment using the \code{\link[=Module]{Module}} and
63-
\code{\link[=populate]{populate}} functions.
64-
65-
If the source file has compilation dependencies on other
66-
packages (e.g. \pkg{Matrix}, \pkg{RcppArmadillo}) then an
67-
\code{\link[=dependsAttribute]{Rcpp::depends}} attribute
68-
should be provided naming these dependencies.
69-
70-
It's possible to embed chunks of R code within a C++ source file by
71-
including the R code within a block comment with the
63+
64+
Content of Rcpp Modules will be automatically loaded into the specified
65+
environment using the \code{\link[=Module]{Module}} and
66+
\code{\link[=populate]{populate}} functions.
67+
68+
If the source file has compilation dependencies on other
69+
packages (e.g. \pkg{Matrix}, \pkg{RcppArmadillo}) then an
70+
\code{\link[=dependsAttribute]{Rcpp::depends}} attribute
71+
should be provided naming these dependencies.
72+
73+
It's possible to embed chunks of R code within a C++ source file by
74+
including the R code within a block comment with the
7275
prefix of \code{/*** R}. For example:
73-
76+
7477
\preformatted{
7578
/*** R
7679
@@ -80,11 +83,11 @@ fibonacci(10)
8083
*/
8184
}
8285
83-
Multiple R code chunks can be included in a C++ file. R code is sourced after the C++ compilation is completed so all functions and modules will be available to the R code.
86+
Multiple R code chunks can be included in a C++ file. R code is sourced after the C++ compilation is completed so all functions and modules will be available to the R code.
8487
}
8588
8689
\value{
87-
Returns (invisibly) a list with two elements:
90+
Returns (invisibly) a list with two elements:
8891
\tabular{ll}{
8992
\code{functions} \tab Names of exported functions\cr
9093
\code{modules} \tab Names of Rcpp modules\cr
@@ -95,9 +98,9 @@ fibonacci(10)
9598
The \code{sourceCpp} function will not rebuild the shared library if the source file has not changed since the last compilation.
9699
97100
The \code{sourceCpp} function is designed for compiling a standalone source file whose only dependencies are R packages. If you are compiling more than one source file or have external dependencies then you should create an R package rather than using \code{sourceCpp}. Note that the \code{\link[=exportAttribute]{Rcpp::export}} attribute can also be used within packages via the \code{\link{compileAttributes}} function.
98-
101+
99102
If you are sourcing a C++ file from within the \code{src} directory of a package then the package's \code{LinkingTo} dependencies, \code{inst/include}, and \code{src} directories are automatically included in the compilation.
100-
103+
101104
If no \code{Rcpp::export} attributes or \code{RCPP_MODULE} declarations are found within the source file then a warning is printed to the console. You can disable this warning by setting the \code{rcpp.warnNoExports} option to \code{FALSE}.
102105

103106
}

0 commit comments

Comments
 (0)