Forum Navigation:


FORUMS > Programming and Software Forum < refresh >
Topic Title: R/C++ interface
Created On Fri Jul 16, 10 10:22 AM
Topic View:

View thread in raw text format


humtumiit
Member

Posts: 178
Joined: Apr 2007

Fri Jul 16, 10 10:22 AM
User is offline

Hello,
I have a C++ code and need to acess this code in R. I found that Rcpp interface provide me the flexibility to acess C++ code in R.
I have 2 questions:

1. In which folder my C++ code should be? How will R know the C++ code location?
2. What is the command in R prompt I should use to execute C++ program in R.

I would really appreciate your help or suggestions.
 
Reply
   
Quote
   
Top
   
Bottom
     



frenchyWill
Member

Posts: 59
Joined: Jun 2010

Fri Jul 16, 10 10:32 AM
User is offline

For point 1 you've got two choices :
1. set in R the working directory where you C++ files are located
2. give in your .r file the complete directory path of your C++ files

For point 2 : google is your best friend.

Edited: Fri Jul 16, 10 at 10:33 AM by frenchyWill
 
Reply
   
Quote
   
Top
   
Bottom
     



Hansi
Senior Member

Posts: 2987
Joined: Jan 2010

Fri Jul 16, 10 10:48 AM
User is online

Also note that RCPP 0.8.1 does add additional functionality that may make things easier for you:
http://dirk.eddelbuettel.com/code/rcpp/Rcpp-modules.pdf
 
Reply
   
Quote
   
Top
   
Bottom
     



frenchyWill
Member

Posts: 59
Joined: Jun 2010

Fri Jul 16, 10 11:11 AM
User is offline

This rocks !
 
Reply
   
Quote
   
Top
   
Bottom
     



humtumiit
Member

Posts: 178
Joined: Apr 2007

Fri Jul 16, 10 11:57 AM
User is offline

Thanks Guys for the help. I will try now as per the infomation.

 
Reply
   
Quote
   
Top
   
Bottom
     



humtumiit
Member

Posts: 178
Joined: Apr 2007

Mon Jul 19, 10 08:18 AM
User is offline

I have following code.


File name: c1.cpp

#include <iostream>
#include<conio.h>
using namespace std;


int main()
{
cout << "sum is = " << '\n';
getch();
}



RCPP_MODULE(yada){
function( "c1", &c1 ) ;
}

In C++, I am getting 2 errors:
1. Error 1 error C2065: 'yada' : undeclared identifier
2. Error 2 error C2448: 'RCPP_MODULE' : function-style initializer appears to be a function definition

Could you help me in exposing this code to R? I am not able to do so. yada is neither recognised by C++ nor by R.



Edited: Mon Jul 19, 10 at 09:57 AM by humtumiit
 
Reply
   
Quote
   
Top
   
Bottom
     



humtumiit
Member

Posts: 178
Joined: Apr 2007

Mon Jul 19, 10 10:48 AM
User is offline

How to use RCPP_MODULE(yada) in C++.
My C++ program gives error if I add

const char* hello( std::string who ){
std::string result( "hello " ) ;
result += who ;
return result.c_str() ;
}

RCPP_MODULE(yada)
{
using namespace std;
function( "hello", &hello ) ;
};


Error are:
1. Error 1 error C2065: 'yada' : undeclared identifier
2. Error 2 error C2448: 'RCPP_MODULE' : function-style initializer appears to be a function definition

Can anyone help me in fixing these error?






Edited: Mon Jul 19, 10 at 12:00 PM by humtumiit
 
Reply
   
Quote
   
Top
   
Bottom
     



Hansi
Senior Member

Posts: 2987
Joined: Jan 2010

Mon Jul 19, 10 09:35 PM
User is online

Okay it seems like you simply wanted to expose C++ but this is more of a modules based method.

First of run 'vignette( "Rcpp-package" )' in R to see how the package structure works.
Then read the above link for the module definition.

But if you just want to do something simple the inline package may be a better choice, as an example:

> install.packages("inline")
> library(inline)
> fx <- cxxfunction( signature( x = "numeric" ),
+ ' NumericVector xx(x); return wrap( std::accumulate( xx.begin(), xx.end(), 0.0 ) ) ; '
+ , plugin = "Rcpp" )
> res <- fx( seq( 1, 10, by = 0.5 ) )
> res
[1] 104.5
 
Reply
   
Quote
   
Top
   
Bottom
     



humtumiit
Member

Posts: 178
Joined: Apr 2007

Tue Jul 20, 10 04:39 AM
User is offline

Quote

Originally posted by: Hansi
Okay it seems like you simply wanted to expose C++ but this is more of a modules based method.

First of run 'vignette( "Rcpp-package" )' in R to see how the package structure works.
Then read the above link for the module definition.

But if you just want to do something simple the inline package may be a better choice, as an example:

> install.packages("inline")
> library(inline)
> fx <- cxxfunction( signature( x = "numeric" ),
+ ' NumericVector xx(x); return wrap( std::accumulate( xx.begin(), xx.end(), 0.0 ) ) ; '
+ , plugin = "Rcpp" )
> res <- fx( seq( 1, 10, by = 0.5 ) )
> res
[1] 104.5



after installing inline package. I get the following error:

fx <- cxxfunction( signature( x = "numeric" ),
+ ' NumericVector xx(x); return wrap( std::accumulate( xx.begin(), xx.end(), 0.0 ) ) ; '
+ , plugin = "Rcpp")

ERROR(s) during compilation: source code errors or compiler configuration errors!

Program source:
1: // includes from the plugin
2:
3: #include <Rcpp.h>
4:
5:
6: #ifndef BEGIN_RCPP
7: #define BEGIN_RCPP
8: #endif
9:
10: #ifndef END_RCPP
11: #define END_RCPP
12: #endif
13:
14: using namespace Rcpp;
15:
16:
17: // user includes
18:
19:
20: // declaration
21: extern "C" {
22: SEXP file712b6459( SEXP x) ;
23: }
24:
25: // definition
26:
27: SEXP file712b6459( SEXP x ){
28: BEGIN_RCPP
29: NumericVector xx(x); return wrap( std::accumulate( xx.begin(), xx.end(), 0.0 ) ) ;
30: END_RCPP
31: }
32:
33:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created!


TextError
 
Reply
   
Quote
   
Top
   
Bottom
     



Hansi
Senior Member

Posts: 2987
Joined: Jan 2010

Tue Jul 20, 10 09:26 AM
User is online

My guess is that it's related to which ever underlying compiler library it's using/linking against being out of whack, under MacOS it uses /usr/local/lib/libstdc++.dylib not sure about Windows.

Maybe contact the Rcpp author as that example is straight from the Rcpp faq and I can't see anything wrong from a quick glance.
 
Reply
   
Quote
   
Top
   
Bottom
     



humtumiit
Member

Posts: 178
Joined: Apr 2007

Tue Jul 20, 10 09:37 AM
User is offline

using namespace Rcpp;

It doesnot work in R direct.
Program gets stop when it strikes this statement.

 
Reply
   
Quote
   
Top
   
Bottom
     



Hansi
Senior Member

Posts: 2987
Joined: Jan 2010

Tue Jul 20, 10 09:54 AM
User is online

Nope needs to be defined within a module ergo the use of cxxfunction + plugin = "Rcpp" which should encapsulate a module with the above.
 
Reply
   
Quote
   
Top
   
Bottom
     



humtumiit
Member

Posts: 178
Joined: Apr 2007

Tue Jul 20, 10 10:10 AM
User is offline

To use cxxfunction, is this function something to do with the visual studio compiler path i.e. visual studio folder?

Does the visual studio and R folder path matters?
 
Reply
   
Quote
   
Top
   
Bottom
     



Hansi
Senior Member

Posts: 2987
Joined: Jan 2010

Tue Jul 20, 10 08:32 PM
User is online

R folder path shouldn't but there may be some environmental variable being used to locate the correct compiling libraries.

Under MacOS Rcpp is hard coded to use /usr/local/lib/libstdc++.dylib which is a symlink to /usr/local/lib/libstdc++.6.dylib but that's the 32 bit version and if you want to use the 64 bit version of R then you need to re-bind /usr/local/lib/libstdc++.dylib to point to /usr/local/lib/x86-64/libstdc++.6.dylib

Maybe there is something similar going on here? I'll check it out on a Windows machine tomorrow.
 
Reply
   
Quote
   
Top
   
Bottom
     



Hansi
Senior Member

Posts: 2987
Joined: Jan 2010

Wed Jul 21, 10 11:18 AM
User is online

FAQ says:

2.7 Can I use Rcpp with Visual Studio
Not a chance.
And that is not because we are meanies but because R and Visual Studio simply do not get along. As
Rcpp is all about extending R with C++ interfaces, we are bound by the available toolchain. And R simply
does not compile with Visual Studio. Go complain to its vendor if you are still upset.

You'll need to install MinGW + gcc to get this working in Windows. RTools should install all this for you: http://www.murdoch-sutherland.com/Rtools/

Don't have time to test it but maybe someone else can commend on it?

Edited: Wed Jul 21, 10 at 11:21 AM by Hansi
 
Reply
   
Quote
   
Top
   
Bottom
     

View thread in raw text format
FORUMS > Programming and Software Forum < refresh >

Forum Navigation:

© All material, including contents and design, copyright Wilmott Electronic Media Limited - FuseTalk 4.01 © 1999-2014 FuseTalk Inc. Terms & Conditions