Unable to integrate Rcpp package of renjin - rcpp

I am trying to integrate r code in java and want to use c++ code in r code, but i am getting the following error.
"org.renjin.eval.EvalException: Could not resolve native method
'sourceCppContext' in package 'Rcpp'"
while running my java code.
My Java Code piece:
r.eval(String.format("source(\"" + appdir + "/config/MyRscript.R\")"));
String rS = ((SEXP)r.eval("test("+i+")")).toString();
R code :
library(Rcpp)
sourceCpp("Rfunction.cpp")
test <- function(a) {
x <<- timesTwo(a)
print(x)
}
C++ code :
#include <Rcpp.h>
using namespace Rcpp;
int timesTwo(int x) {
return x * 2;
}

Renjin doesn't, and probably can't reasonably support sourceCpp(). You need to either move your C++ code to package where it can be compiled to JVM bytecode at build time, or rewrite in R or Java.

Related

Function Signatures/Interfaces from Pybind11 Module (IDE Suggestions)

Let's assume we have a simple module called _sample built with pybind11:
/* py_bindings.cpp */
#include <pybind11/pybind11.h>
namespace py = pybind11;
PYBIND11_MODULE(_sample, m) {
m.def("add", [](int a, int b) { return a + b; });
m.def("add", [](const std::string& lhs, const std::string& rhs) { return lhs + rhs; });
}
This produces a dynamic module file _sample.pyd (Windows) or _sample.so (Linux), which we can then import in the actual module sample:
## sample\__init__.py ##
from ._sample import *
So that we can write:
## script.py ##
import sample as s
print(s.add(4, 2)) # 6
print(s.add('AB', 'C')) # ABC
The above code works fine, but the IDE does not know which functions are included in _sample until the code is actually run. And as a result, there are no function suggestions at all (and no function signature suggestions either).
As I would like to help the users of my library, my question is: how do I include function suggestions (or "function hints") in my module?
I've tried including the below code in sample\__init__.py as I thought the ... might work as a "hint". But unfortunately, this overrides the original add function from _sample.
def add(arg0: int, arg1: int) -> int:
...
Are there ways to hint the function signatures to a Python IDE?
Of course, I want to extend this to classes, class functions & module attributes too. I just picked functions as a starting point.
I think what you're looking for is a stub or interface (pyi) file. The IDE can understand the signature of functions and classes from this file.
If you're using pybind11, check out pybind11-stubgen for automatic generation of a stub file.

How do I disable MSVC warnings from headers included with quotes instead of angle brackets when using /analyze?

I am trying to add the /analyze flag to cl.
I am getting a warnings from external headers I #include "...", even though I am using /analyze:external- and /external:I ....
What am I doing wrong?
Example project:
main.cpp
#include "external.h"
// #include <external.h> <- hides external's warnings
int main() {
int shadowed = 0;
{ float shadowed = 0; }
return 0;
}
external.h
void something() {
int external_shadowed = 0;
{ float external_shadowed = 0; }
}
I run this command from the VS developer prompt:
cl /EHsc /analyze /analyze:external- /I include /external:I external /external:W0 main.cpp
And I proceed to get warnings from both files.
It seems MSVC only considers /external:I directories to actually be external if they are included with <> instead of "". This is not documented anywhere, and I would consider it a bug with the compiler. Third-party libraries should not necessarily always be included with <>.
More info and possibly updates in the future here: https://developercommunity.visualstudio.com/t/analyze:external--and-external:I-flags/1688240
I have not tested myself, but according to Hwi-sung Im [MSFT], <> and "" now act the same w.r.t /external:I in Visual Studio 2022 17.0.

Using seamless R in Rcpp function

I thought I have seen before that you can write a Rcpp function and use R code inside of it. I just can't find the link anymore and using google doesn't help at all. Can you provide me an example or a link where it will be explained how to do it?
The key is the /*** R ... */ expression at the bottom, see the 'Rcpp Attributes' vignette.
So for the code
#include <Rcpp.h>
// [[Rcpp::export]]
void reallyWorks() {
Rcpp::Rcout << "Oh, wow, it works" << std::endl;
}
/*** R
reallyWorks()
*/
we get the expected behaviour straight from sourceCpp():
> sourceCpp("/tmp/soQ.cpp")
> reallyWorks()
Oh, wow, it works
>

to_string not recognized as a member of string OSX Mavericks using g++

I'm trying to use to_string to append an integer value to a string like this:
#include <iostream>
#include <string>
using namespace std;
int main(){
int number = 1;
string p = "Player";
p += to_string(number);
return 0;
}
But when I try to compile it I get an error stating to_string was not declared in this scope, so I removed the using namespace std; and replaced to_string with std::to_string, but now I get a new error saying that to_string is not a member of std.
This problem only occurs when I compile from the command line but it works perfectly in XCode.
Any ideas on why this is? (I need to be able to compile it from the command line for a hw assignment)
to_string is a feature of C++11, did you use the flag --std=c++11?

Enabling string conversion functions in MinGW

this code:
#include <iostream>
#include <string>
int main()
{
std::string s = "52123210";
int derp = std::stoi(s, 0, 10);
std::to_string(derp);
return 0;
}
with this error:
test.cpp:10:2: error: 'stoi' is not a member of 'std'
test.cpp:11:2: error: 'to_string' is not a member of 'std'
tried this:
http://tehsausage.com/mingw-to-string
(not work)
Update my MingW from 4.6.1 to 4.8.1
(not work)
possible bug:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37522
(beyond of my knowledge to apply anything, I don't dare to touch the compiler's code)
**Also not work with "using namespace std" but produced 'stoi' and 'to_string' not declared
error instead.
This is a result of a non-standard declaration of vswprintf on Windows. The GNU Standard Library defines _GLIBCXX_HAVE_BROKEN_VSWPRINTF on this platform, which in turn disables the conversion functions you're attempting to use.
https://stackoverflow.com/a/8543540/2684539 proposes a hack/work around.

Resources