[Rcpp-devel] Calling R from C++, seeing results in C++

2014-05-23 Thread F.Tusell
I have been struggling with the following:

file test4.R


library(Rcpp)
library(RcppArmadillo)

MyClass <- setRefClass("MyClass",
   fields=c(
 A="numeric"
 ),
   methods=c(
 initialize = function() {
   A <<- 1:4
 },
 update = function() {
   A <<- 2*A
 },
 modify  = function() {
   test4( .self$A, .self$update )
 }
 )
   )

file test4.cpp
==

// [[Rcpp::depends(RcppArmadillo)]]

#include 

// [[Rcpp::export]]
int test4(Rcpp::NumericVector A, Rcpp::Function fn) {
  arma::cube aF(A.begin(), 1, 2, 2, false) ;
  Rcpp::as( fn() ) ;
  Rcpp::Rcout << "aF =  " << aF << std::endl;
}


When I source these files, I can execute:

> MyObj   <- MyClass$new()

> MyObj
Reference class object of class "MyClass"
Field "A":
[1] 1 2 3 4

> MyObj$modify()
aF =  [cube slice 0]
   1.   2.

[cube slice 1]
   3.   4.


[1] 0

> MyObj$A
[1] 2 4 6 8

The intent was to overlay miobj$A with the Armadillo cube aF within
C++ function test4, so that changes in miobj$A triggered by calling
miobj$update (vía fn() inside test4) would be seen in aF.

However, this is not the case: miobj$A is multiplied by 2, as it
should, but aF remains unaltered. One of the problems (perhaps not the
only one) is that miobj$A keeps changing place whenever is assigned (I
was using a reference class in the hope that the memory used by objects
would not change).

So the question is: Is there a way to invoke from a C++ function an R
function which modifies an R object, and see the results reflected in a
C++
object immediately, without copying?

Grateful for any hints or leads on where to look. Best, ft.


___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Re: [Rcpp-devel] Not able to convert from Rcpp::NumericVector to Teuchos::ArrayRCP

2014-05-23 Thread Chaomei Lo
Hi, Romain,

Your suggestion works pretty well.  I appreciate your valuable help !

Chaomei


On Thu, May 22, 2014 at 4:18 PM, Romain François
wrote:

> Hello,
>
> From what I understand of ArrayRCP, having read the documentation for a
> few minutes. (
> http://trilinos.sandia.gov/packages/docs/r9.0/packages/teuchos/doc/html/classTeuchos_1_1ArrayRCP.html
> )
> This looks like a vector and therefore it is likely that the default
> Exporter will not be useful as it tries to use the constructor taking a
> SEXP :
>
> template 
> class Exporter{
> public:
> Exporter( SEXP x ) : t(x){}
> inline T get(){ return t ; }
>
> private:
> T t ;
> } ;
>
> you can use a RangeExporter instead which looks more compatible to what
> ArrayRCP looks like:
>
> template  class RangeExporter {
> public:
> typedef typename T::value_type r_export_type ;
>
> RangeExporter( SEXP x ) : object(x){}
> ~RangeExporter(){}
>
> T get(){
> T vec( ::Rf_length(object) );
> ::Rcpp::internal::export_range( object, vec.begin() ) ;
> return vec ;
> }
>
> private:
> SEXP object ;
> } ;
>
>
> You can probably do this by having something like this early enough (after
> RcppCommon.h but before Rcpp.h).
>
> namespace Rcpp {
> namespace traits {
>
>   template  class Exporter<  Teuchos::ArrayRCP > : public
> RangeExporter< Teuchos::ArrayRCP >{
>   public:
> Exporter( SEXP x) : RangeExporter< Teuchos::ArrayRCP >(x){}
>   }
>
> }
> }
>
>
> Romain
>
> Le 23 mai 2014 à 00:42, Chaomei Lo  a écrit :
>
> Sorry to confuse you, Dirk, I had my previous message title wrong.  Here
> below was the message with the correct title and content.
>
> 
> I have created R packages using Makevars and it works pretty good for me.
> I am able to convert from a Rcpp::NumericVector to std::vector as in below.
>
>
>
>
> *NumericVector col_cts = buildMatrix(Xr);vector
> col_counts = Rcpp::as>(col_cts);  *
> Now I am having a problem with an application involves the Trilinos
> library. Here below I am trying to convert a Rcpp::NumericVector to
> Teuchos::ArrayRCP in the following line, it gave me errors as shown
> below in red.  Would you please help me with this ?
>
> *Teuchos::ArrayRCP
> col_counts=Rcpp::as>(col_cts);*
>
> Thanks a lot.
>
> In file included from
> /share/apps/R/3.0.2/lib64/R/library/Rcpp/include/Rcpp/as.h:25:0,
>  from
> /share/apps/R/3.0.2/lib64/R/library/Rcpp/include/RcppCommon.h:169,
>  from
> /share/apps/R/3.0.2/lib64/R/library/Rcpp/include/Rcpp.h:27,
>  from buildMatrix.cpp:4:
> /share/apps/R/3.0.2/lib64/R/library/Rcpp/include/Rcpp/internal/Exporter.h:
> In constructor 'Rcpp::traits::Exporter::Exporter(SEXP) [with T =
> Teuchos::Array
> RCP, SEXP = SEXPREC*]':
> /share/apps/R/3.0.2/lib64/R/library/Rcpp/include/Rcpp/as.h:79:51:
> instantiated from 'T Rcpp::internal::as(SEXP,
> Rcpp::traits::r_type_generic_tag) [with T =
>  Teuchos::ArrayRCP, SEXP = SEXPREC*]'
> /share/apps/R/3.0.2/lib64/R/library/Rcpp/include/Rcpp/as.h:144:84:
> instantiated from 'T Rcpp::as(SEXP) [with T = Teuchos::ArrayRCP unsigned int>, SEXP
>  = SEXPREC*]'
> buildMatrix.cpp:84:87:   instantiated from here
> /share/apps/R/3.0.2/lib64/R/library/Rcpp/include/Rcpp/internal/Exporter.h:31:31:
> error: invalid conversion from 'SEXP' to 'long int' [-fpermissive]
> /people/d3j508/trilinos-11.6.1/trilinos-11.6.1_Install/include/Teuchos_ArrayRCP.hpp:129:1:
> error:   initializing argument 1 of 'Teuchos::ArrayRCP::ArrayRC
> P(Teuchos::ArrayRCP::size_type, const T&) [with T = long unsigned int,
> Teuchos::ArrayRCP::size_type = long int]' [-fpermissive]
> make: *** [buildMatrix.o] Error 1
> ERROR: compilation failed for package 'Tpkg'
>  ___
> Rcpp-devel mailing list
> Rcpp-devel@lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
>
>
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

[Rcpp-devel] Converting std::cout to Rcpp::Rcout automatically

2014-05-23 Thread Qiang Kou
Hi, dear all,

I am using Rcpp to integrate an available C++ library with R.

Since the std::cout and std::cerr are not permitted when uploading to CRAN,
I have to modify files manually. It will be somewhat laborious if the
library

Does anyone have any idea how can I do that automatically?

Thank you!

Best,

KK

-- 
Qiang Kou
q...@umail.iu.edu
School of Informatics and Computing, Indiana University
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Re: [Rcpp-devel] Converting std::cout to Rcpp::Rcout automatically

2014-05-23 Thread Dirk Eddelbuettel

On 23 May 2014 at 21:22, Qiang Kou wrote:
| I am using Rcpp to integrate an available C++ library with R.
|
| Since the std::cout and std::cerr are not permitted when uploading to CRAN, I
| have to modify files manually. It will be somewhat laborious if the library 
|
| Does anyone have any idea how can I do that automatically?

I am afraid there is none.  Rcpp::Rcout is a trick to fold 'our' output into
R's; there is no trick to fold the system output.

One trick may be to ... use the library as an external object that you link
to.  That way CRAN won't check the library code as part of the submission
(but if it is a custom/complicated library you may have the problem of
getting onto CRAN build servers -- I am currently for libhiredis to make it
onto the win-builder / CRAN windows build side of things.

But barring that you may need to change all std::cout instances (and yes,
I've done that once too).

Dirk


-- 
Dirk Eddelbuettel | e...@debian.org | http://dirk.eddelbuettel.com
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


Re: [Rcpp-devel] Converting std::cout to Rcpp::Rcout automatically

2014-05-23 Thread Kevin Ushey
find . -name "*.cpp" -exec sed -i 's/std::cout/Rcpp::Rcout/g' {} \;

This, or some variant, should get you there.

I suggest version controlling your files before executing, just in case ;)

Kevin
On May 23, 2014 7:22 PM, "Dirk Eddelbuettel"  wrote:

>
> On 23 May 2014 at 21:22, Qiang Kou wrote:
> | I am using Rcpp to integrate an available C++ library with R.
> |
> | Since the std::cout and std::cerr are not permitted when uploading to
> CRAN, I
> | have to modify files manually. It will be somewhat laborious if the
> library
> |
> | Does anyone have any idea how can I do that automatically?
>
> I am afraid there is none.  Rcpp::Rcout is a trick to fold 'our' output
> into
> R's; there is no trick to fold the system output.
>
> One trick may be to ... use the library as an external object that you link
> to.  That way CRAN won't check the library code as part of the submission
> (but if it is a custom/complicated library you may have the problem of
> getting onto CRAN build servers -- I am currently for libhiredis to make it
> onto the win-builder / CRAN windows build side of things.
>
> But barring that you may need to change all std::cout instances (and yes,
> I've done that once too).
>
> Dirk
>
>
> --
> Dirk Eddelbuettel | e...@debian.org | http://dirk.eddelbuettel.com
> ___
> Rcpp-devel mailing list
> Rcpp-devel@lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Re: [Rcpp-devel] Converting std::cout to Rcpp::Rcout automatically

2014-05-23 Thread Romain François
Hi, 

I used to redirect std::cout to Rcpp::Rcout back when I still had Rcout in 
Rcpp11. e.g. 
https://github.com/Rcpp11/Rcpp11/blob/22cc410ea87a2668e547acf6510d07ca812dfca8/src/Rcpp11_init.cpp

Which was basically leveraging std::cout.rdbuf. 
A variant of 
http://stackoverflow.com/questions/10150468/how-to-redirect-cin-and-cout-to-files

And then just using std::cout; Of course then you get CRAN complaining. 

Romain

> Hi, dear all,
> 
> I am using Rcpp to integrate an available C++ library with R.
> 
> Since the std::cout and std::cerr are not permitted when uploading to CRAN, I 
> have to modify files manually. It will be somewhat laborious if the library 
> 
> Does anyone have any idea how can I do that automatically?
> 
> Thank you!
> 
> Best,
> 
> KK
> 
> -- 
> Qiang Kou
> q...@umail.iu.edu
> School of Informatics and Computing, Indiana University
> 
> ___
> Rcpp-devel mailing list
> Rcpp-devel@lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

___
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel