Skip to content

Add echo parameter to sourcecpp #1138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
2021-01-21 Dirk Eddelbuettel <edd@debian.org>

* DESCRIPTION (Version, Date): Roll minor version
* inst/include/Rcpp/config.h: Idem

* inst/include/Rcpp/DataFrame.h: Remove unused variable

* R/Attributes.R (sourceCpp): Support argument echo to be passed to
source() to silence optional R evaluation

2021-01-19 Dirk Eddelbuettel <edd@debian.org>

* DESCRIPTION (Version, Date): Roll minor version
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: Rcpp
Title: Seamless R and C++ Integration
Version: 1.0.6.3
Date: 2021-01-19
Version: 1.0.6.4
Date: 2021-01-21
Author: Dirk Eddelbuettel, Romain Francois, JJ Allaire, Kevin Ushey, Qiang Kou,
Nathan Russell, Douglas Bates and John Chambers
Maintainer: Dirk Eddelbuettel <edd@debian.org>
Expand Down
11 changes: 7 additions & 4 deletions R/Attributes.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ sourceCpp <- function(file = "",
showOutput = verbose,
verbose = getOption("verbose"),
dryRun = FALSE,
windowsDebugDLL = FALSE) {
windowsDebugDLL = FALSE,
echo = TRUE) {

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

# cleanup the cache dir if requested
Expand Down Expand Up @@ -258,7 +259,8 @@ cppFunction <- function(code,
rebuild = FALSE,
cacheDir = getOption("rcpp.cache.dir", tempdir()),
showOutput = verbose,
verbose = getOption("verbose")) {
verbose = getOption("verbose"),
echo = TRUE) {

# process depends
if (!is.null(depends) && length(depends) > 0) { # #nocov start
Expand Down Expand Up @@ -315,7 +317,8 @@ cppFunction <- function(code,
rebuild = rebuild,
cacheDir = cacheDir,
showOutput = showOutput,
verbose = verbose)
verbose = verbose,
echo = echo)

# verify that a single function was exported and return it
if (length(exported$functions) == 0)
Expand Down
6 changes: 5 additions & 1 deletion inst/NEWS.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
\item Changes in Rcpp API:
\itemize{
\item Refactored \code{Rcpp_PreserveObject} and \code{Rcpp_ReleaseObject}
which are now O(1) (Dirk and Iñaki in \ghpr{1133} and \ghor{1135}
which are now O(1) (Dirk and Iñaki in \ghpr{1133} and \ghpr{1135}
fixing \ghit{382} and \ghit{1081}).
\item A spuriously assigned variable was removed (Dirk in
\ghpr{1138} fixing \ghit{1137})
}
\item Changes in Rcpp Attributes:
\itemize{
\item Packages with dots in their name can now have per-package
include files (Dirk in \ghpr{1132} fixes \ghit{1129}).
\item New argument \code{echo} to quieten optional evaluation in
\code{sourceCpp} (Dirk in \ghpr{1138} fixing \ghit{1126}).
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions inst/include/Rcpp/DataFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// DataFrame.h: Rcpp R/C++ interface class library -- data frames
//
// Copyright (C) 2010 - 2015 Dirk Eddelbuettel and Romain Francois
// Copyright (C) 2010 - 2021 Dirk Eddelbuettel and Romain Francois
//
// This file is part of Rcpp.
//
Expand Down Expand Up @@ -106,7 +106,7 @@ namespace Rcpp{
Parent::push_front(object, name);
set_type_after_push();
}

// Offer multiple variants to accomodate both old interface here and signatures in other classes
inline int nrows() const { return DataFrame_Impl::nrow(); }
inline int rows() const { return DataFrame_Impl::nrow(); }
Expand All @@ -133,7 +133,6 @@ namespace Rcpp{
void set_type_after_push(){
int max_rows = 0;
bool invalid_column_size = false;
SEXP data = Parent::get__();
List::iterator it;
// Get the maximum number of rows
for (it = Parent::begin(); it != Parent::end(); ++it) {
Expand Down
4 changes: 2 additions & 2 deletions inst/include/Rcpp/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#define RCPP_VERSION_STRING "1.0.6"

// the current source snapshot
#define RCPP_DEV_VERSION RcppDevVersion(1,0,6,3)
#define RCPP_DEV_VERSION_STRING "1.0.6.3"
#define RCPP_DEV_VERSION RcppDevVersion(1,0,6,4)
#define RCPP_DEV_VERSION_STRING "1.0.6.4"

#endif
27 changes: 15 additions & 12 deletions man/cppFunction.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Dynamically define an R function with C++ source code. Compiles and links a shar
}
\usage{
cppFunction(code, depends = character(), plugins = character(), includes = character(),
env = parent.frame(), rebuild = FALSE, cacheDir = getOption("rcpp.cache.dir",
tempdir()), showOutput = verbose, verbose = getOption("verbose"))
env = parent.frame(), rebuild = FALSE, cacheDir = getOption("rcpp.cache.dir",
tempdir()), showOutput = verbose, verbose = getOption("verbose"), echo = TRUE)
}

\arguments{
Expand Down Expand Up @@ -39,11 +39,14 @@ cppFunction(code, depends = character(), plugins = character(), includes = chara
}
\item{verbose}{
\code{TRUE} to print detailed information about generated code to the console.
}
\item{echo}{
\code{TRUE} to silence output from optional R evaluation if set to \code{FALSE}.
}
}
\details{
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}.

The shared library will not be rebuilt if the underlying code has not changed since the last compilation.
}
\value{
Expand All @@ -63,36 +66,36 @@ cppFunction(code, depends = character(), plugins = character(), includes = chara

cppFunction(
'int fibonacci(const int x) {
if (x == 0) return(0);
if (x == 0) return(0);
if (x == 1) return(1);
return (fibonacci(x - 1)) + fibonacci(x - 2);
}')

cppFunction(depends = "RcppArmadillo",
'List fastLm(NumericVector yr, NumericMatrix Xr) {

int n = Xr.nrow(), k = Xr.ncol();
arma::mat X(Xr.begin(), n, k, false);

arma::mat X(Xr.begin(), n, k, false);
arma::colvec y(yr.begin(), yr.size(), false);

arma::colvec coef = arma::solve(X, y);
arma::colvec resid = y - X*coef;

double sig2 = arma::as_scalar(arma::trans(resid)*resid/(n-k) );
arma::colvec stderrest = arma::sqrt(
sig2 * arma::diagvec(arma::inv(arma::trans(X)*X)));

return List::create(Named("coefficients") = coef,
Named("stderr") = stderrest
);
}')

cppFunction(plugins=c("cpp11"), '
int useCpp11() {
auto x = 10;
return x;
}')

}
}
51 changes: 27 additions & 24 deletions man/sourceCpp.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ Source C++ Code from a File or String
and RCPP_MODULE declarations. A shared library is then built and its exported functions and Rcpp modules are made available in the specified environment.
}
\usage{
sourceCpp(file = "", code = NULL, env = globalenv(), embeddedR = TRUE, rebuild = FALSE,
sourceCpp(file = "", code = NULL, env = globalenv(), embeddedR = TRUE, rebuild = FALSE,
cacheDir = getOption("rcpp.cache.dir", tempdir()), cleanupCacheDir = FALSE,
showOutput = verbose, verbose = getOption("verbose"), dryRun = FALSE,
windowsDebugDLL = FALSE)
windowsDebugDLL = FALSE, echo = TRUE)
}
\arguments{
\item{file}{
A character string giving the path name of a file
}
\item{code}{
A character string with source code. If supplied, the code is taken from this string instead of a file.
A character string with source code. If supplied, the code is taken from this string instead of a file.
}
\item{env}{
Environment where the R functions and modules should be made available.
Expand Down Expand Up @@ -47,30 +47,33 @@ actually executing the commands).
}
\item{windowsDebugDLL}{
\code{TRUE} to create a debug DLL on Windows (and ignored on other platforms).
}
\item{echo}{
\code{TRUE} to silence output from optional R evaluation if set to \code{FALSE}.
}
}
\details{
If the \code{code} parameter is provided then the \code{file} parameter is ignored.

Functions exported using \code{sourceCpp} must meet several conditions,
including being defined in the global namespace and having return types
that are compatible with \code{Rcpp::wrap} and parameter types that are
compatible with \code{Rcpp::as}.
Functions exported using \code{sourceCpp} must meet several conditions,
including being defined in the global namespace and having return types
that are compatible with \code{Rcpp::wrap} and parameter types that are
compatible with \code{Rcpp::as}.
See the \code{\link[=exportAttribute]{Rcpp::export}} documentation for more details.
Content of Rcpp Modules will be automatically loaded into the specified
environment using the \code{\link[=Module]{Module}} and
\code{\link[=populate]{populate}} functions.
If the source file has compilation dependencies on other
packages (e.g. \pkg{Matrix}, \pkg{RcppArmadillo}) then an
\code{\link[=dependsAttribute]{Rcpp::depends}} attribute
should be provided naming these dependencies.
It's possible to embed chunks of R code within a C++ source file by
including the R code within a block comment with the

Content of Rcpp Modules will be automatically loaded into the specified
environment using the \code{\link[=Module]{Module}} and
\code{\link[=populate]{populate}} functions.

If the source file has compilation dependencies on other
packages (e.g. \pkg{Matrix}, \pkg{RcppArmadillo}) then an
\code{\link[=dependsAttribute]{Rcpp::depends}} attribute
should be provided naming these dependencies.

It's possible to embed chunks of R code within a C++ source file by
including the R code within a block comment with the
prefix of \code{/*** R}. For example:

\preformatted{
/*** R

Expand All @@ -80,11 +83,11 @@ fibonacci(10)
*/
}

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.
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.
}

\value{
Returns (invisibly) a list with two elements:
Returns (invisibly) a list with two elements:
\tabular{ll}{
\code{functions} \tab Names of exported functions\cr
\code{modules} \tab Names of Rcpp modules\cr
Expand All @@ -95,9 +98,9 @@ fibonacci(10)
The \code{sourceCpp} function will not rebuild the shared library if the source file has not changed since the last compilation.

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.

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.

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}.

}
Expand Down