Skip to content

Commit 8c360a8

Browse files
authored
Merge pull request #852 from filipsch/master
execute embedded R script in specified env
2 parents 001db74 + 6f45d4c commit 8c360a8

File tree

4 files changed

+107
-1
lines changed

4 files changed

+107
-1
lines changed

R/Attributes.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ sourceCpp <- function(file = "",
196196
if (embeddedR && (length(context$embeddedR) > 0)) {
197197
srcConn <- textConnection(context$embeddedR)
198198
setwd(rWorkingDir) # will be reset by previous on.exit handler
199-
source(file=srcConn, echo=TRUE)
199+
source(file = srcConn, local = env, echo = TRUE)
200200
}
201201

202202
# cleanup the cache dir if requested

inst/unitTests/cpp/embeddedR.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2+
//
3+
// Environment.cpp: Rcpp R/C++ interface class library -- Environment unit tests
4+
//
5+
// Copyright (C) 2018 Dirk Eddelbuettel and Romain Francois
6+
//
7+
// This file is part of Rcpp.
8+
//
9+
// Rcpp is free software: you can redistribute it and/or modify it
10+
// under the terms of the GNU General Public License as published by
11+
// the Free Software Foundation, either version 2 of the License, or
12+
// (at your option) any later version.
13+
//
14+
// Rcpp is distributed in the hope that it will be useful, but
15+
// WITHOUT ANY WARRANTY; without even the implied warranty of
16+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
// GNU General Public License for more details.
18+
//
19+
// You should have received a copy of the GNU General Public License
20+
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21+
22+
#include <Rcpp.h>
23+
using namespace Rcpp ;
24+
25+
// [[Rcpp::export]]
26+
int foo(){
27+
return 42 ;
28+
}
29+
30+
/*** R
31+
x <- foo()
32+
x
33+
*/

inst/unitTests/cpp/embeddedR2.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2+
//
3+
// Environment.cpp: Rcpp R/C++ interface class library -- Environment unit tests
4+
//
5+
// Copyright (C) 2018 Dirk Eddelbuettel and Romain Francois
6+
//
7+
// This file is part of Rcpp.
8+
//
9+
// Rcpp is free software: you can redistribute it and/or modify it
10+
// under the terms of the GNU General Public License as published by
11+
// the Free Software Foundation, either version 2 of the License, or
12+
// (at your option) any later version.
13+
//
14+
// Rcpp is distributed in the hope that it will be useful, but
15+
// WITHOUT ANY WARRANTY; without even the implied warranty of
16+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
// GNU General Public License for more details.
18+
//
19+
// You should have received a copy of the GNU General Public License
20+
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21+
22+
#include <Rcpp.h>
23+
using namespace Rcpp ;
24+
25+
// [[Rcpp::export]]
26+
int bar(){
27+
return 42 ;
28+
}
29+
30+
/*** R
31+
x <- foo()
32+
x
33+
*/

inst/unitTests/runit.embeddedR.R

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env r
2+
# -*- mode: R; ess-indent-level: 4; tab-width: 4; indent-tabs-mode: nil; -*
3+
#
4+
# Copyright (C) 2012 - 2016 Dirk Eddelbuettel and Romain Francois
5+
#
6+
# This file is part of Rcpp.
7+
#
8+
# Rcpp is free software: you can redistribute it and/or modify it
9+
# under the terms of the GNU General Public License as published by
10+
# the Free Software Foundation, either version 2 of the License, or
11+
# (at your option) any later version.
12+
#
13+
# Rcpp is distributed in the hope that it will be useful, but
14+
# WITHOUT ANY WARRANTY; without even the implied warranty of
15+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
# GNU General Public License for more details.
17+
#
18+
# You should have received a copy of the GNU General Public License
19+
# along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
20+
21+
.runThisTest <- Sys.getenv("RunAllRcppTests") == "yes"
22+
23+
if (.runThisTest) {
24+
25+
test.embeddedR <- function() {
26+
path <- file.path(system.file("unitTests", package = "Rcpp"), "cpp")
27+
expectedVars <- c("foo", "x")
28+
29+
# embeddedR.cpp exposes the function foo, R snippet calls foo
30+
newEnv <- new.env()
31+
Rcpp::sourceCpp(file.path(path, "embeddedR.cpp"),
32+
env = newEnv)
33+
checkEquals(ls(newEnv), expectedVars, msg = " sourcing code in custom env")
34+
35+
# R snippet in embeddedR2.cpp also contains a call to foo from previous cpp
36+
newEnv2 <- new.env()
37+
checkException(Rcpp::sourceCpp(file.path(path, "embeddedR2.cpp"), env = newEnv2),
38+
" not available in other env")
39+
}
40+
}

0 commit comments

Comments
 (0)