Rcpp not sure what class return type should be. error: double not able to convert to const int&What should main() return in C and C++?error: request for member '..' in '..' which is of non-class typeConverting element of 'const Rcpp::CharacterVector&' to 'std::string'How to convert Rcpp::List to std::vector<double>Rcpp: Error: not compatible with requested typeRcpp error: invalid static_cast from type 'Rcpp::Vector<13, Rcpp::PreserveStorage>' to type 'int'Convert Rcpp Armadillo matrix to double*What does “const int&” do as a return type?Rcpp: Converting SEXP to float/doubleRcpp: Floating point exception by using double type
How do I delete cookies from a specific site?
French equivalent of "my cup of tea"
Entering the US with dual citizenship but US passport is long expired?
How do I anonymously report the Establishment Clause being broken?
Professor refuses to write a recommendation letter to students who haven't written a research paper with him
Infinitely many Primes
Who's this voice acting performer?
"syntax error near unexpected token" after editing .bashrc
Draw the ☣ (Biohazard Symbol)
Global variables and information security
How to create large inductors (1H) for audio use?
Why Is Sojdlg123aljg a Common Password?
Why does the UK Prime Minister need the permission of Parliament to call a general election?
If I have an accident, should I file a claim with my car insurance company?
What is the purpose of the rotating plate in front of the lock?
Is there some sort of French saying for "a person's signature move"?
Did the US Climate Reference Network Show No New Warming Since 2005 in the US?
Why did Tony's Arc Reactor do this?
Why would one hemisphere of a planet be very mountainous while the other is flat?
Why there is no wireless switch?
Mute single speaker?
Are language and thought the same?
Is the interior of a Bag of Holding actually an extradimensional space?
Does the Commodore CDTV-CR contain a 65C02 for some reason?
Rcpp not sure what class return type should be. error: double not able to convert to const int&
What should main() return in C and C++?error: request for member '..' in '..' which is of non-class typeConverting element of 'const Rcpp::CharacterVector&' to 'std::string'How to convert Rcpp::List to std::vector<double>Rcpp: Error: not compatible with requested typeRcpp error: invalid static_cast from type 'Rcpp::Vector<13, Rcpp::PreserveStorage>' to type 'int'Convert Rcpp Armadillo matrix to double*What does “const int&” do as a return type?Rcpp: Converting SEXP to float/doubleRcpp: Floating point exception by using double type
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am trying to use Rcpp for the first time. I have made a script in C++ that works fine independently of R, but when trying to implement it in Rcpp I am getting an error message about the return type. Here is the gist of what I've done:
cppSim <- '
#include <iostream>
#include <random>
#include <math.h>
//[[Rcpp::plugins(cpp11)]]
#include <Rcpp.h>
#include <cmath>
#include <iomanip>
#include <boost/math/distributions/students_t.hpp>
using boost::math::students_t;
using namespace std;
using namespace Rcpp;
// function to calculate p-value from a t-test
double ttestPValue(vector<double> obs1, vector<double> obs2, int nSamples)
...
return(q); \ q is a double, this bit works fine
NumericVector powerSimulation(int nSamples, int meanDiff, int nPerm, double pValueCutOff)
...
// create vectors to hold data
double sim1T[totalRD][nPerm];
...
sim1T[rdmu - minRD][j] = ttestPValue(obs1, obs2, nSamples);
double power[totalRD];
for (int j = 0; j < 100; j++)
power[j] = 0;
for (int i = 0; i < totalRD; i++)
for (int sum = 0; sum < nPerm; sum ++)
if (sim1T[i][sum] < pValueCutOff)
power[i] = power[i] + 1;
power[i] = (power[i] / nPerm)*100;
return power;
'
To run it I am using:
settings=getPlugin("Rcpp")
settings$env$PKG_CXXFLAGS=paste('-std=c++11',settings$env$PKG_CXXFLAGS,sep=' ')
simRcpp <- cxxfunction(signature(nSamplesR = "int", meanDiffR = "int", nPermR = "int", pValueCutOffR = "double"),
plugin="Rcpp",
settings = settings,
includes = cppSim,
body='
int nSamples = Rcpp::as<int>(nSamplesR);
int meanDiff = Rcpp::as<int>(meanDiffR);
int nPerm = Rcpp::as<int>(nPermR);
double pValueCutOff = Rcpp::as<double>(pValueCutOffR);
return Rcpp::wrap( cppSim(nSamples, meanDiff, nPerm, pValueCutOff));')
The error that I'm getting is:
file928498473e2.cpp: In function ‘Rcpp::NumericVector powerSimulation(int, int, int, double)’:
file928498473e2.cpp:165:8: error: invalid conversion from ‘double*’ to ‘const int&’ [-fpermissive]
return power;
^
I get that my return type for power is wrong but how do I make it right? I've tried wrapping it as a NumericVector but that doesn't seem to work and I can't find an example with the same issue, as not everyone uses cxxfunction().
c++ r rcpp
add a comment |
I am trying to use Rcpp for the first time. I have made a script in C++ that works fine independently of R, but when trying to implement it in Rcpp I am getting an error message about the return type. Here is the gist of what I've done:
cppSim <- '
#include <iostream>
#include <random>
#include <math.h>
//[[Rcpp::plugins(cpp11)]]
#include <Rcpp.h>
#include <cmath>
#include <iomanip>
#include <boost/math/distributions/students_t.hpp>
using boost::math::students_t;
using namespace std;
using namespace Rcpp;
// function to calculate p-value from a t-test
double ttestPValue(vector<double> obs1, vector<double> obs2, int nSamples)
...
return(q); \ q is a double, this bit works fine
NumericVector powerSimulation(int nSamples, int meanDiff, int nPerm, double pValueCutOff)
...
// create vectors to hold data
double sim1T[totalRD][nPerm];
...
sim1T[rdmu - minRD][j] = ttestPValue(obs1, obs2, nSamples);
double power[totalRD];
for (int j = 0; j < 100; j++)
power[j] = 0;
for (int i = 0; i < totalRD; i++)
for (int sum = 0; sum < nPerm; sum ++)
if (sim1T[i][sum] < pValueCutOff)
power[i] = power[i] + 1;
power[i] = (power[i] / nPerm)*100;
return power;
'
To run it I am using:
settings=getPlugin("Rcpp")
settings$env$PKG_CXXFLAGS=paste('-std=c++11',settings$env$PKG_CXXFLAGS,sep=' ')
simRcpp <- cxxfunction(signature(nSamplesR = "int", meanDiffR = "int", nPermR = "int", pValueCutOffR = "double"),
plugin="Rcpp",
settings = settings,
includes = cppSim,
body='
int nSamples = Rcpp::as<int>(nSamplesR);
int meanDiff = Rcpp::as<int>(meanDiffR);
int nPerm = Rcpp::as<int>(nPermR);
double pValueCutOff = Rcpp::as<double>(pValueCutOffR);
return Rcpp::wrap( cppSim(nSamples, meanDiff, nPerm, pValueCutOff));')
The error that I'm getting is:
file928498473e2.cpp: In function ‘Rcpp::NumericVector powerSimulation(int, int, int, double)’:
file928498473e2.cpp:165:8: error: invalid conversion from ‘double*’ to ‘const int&’ [-fpermissive]
return power;
^
I get that my return type for power is wrong but how do I make it right? I've tried wrapping it as a NumericVector but that doesn't seem to work and I can't find an example with the same issue, as not everyone uses cxxfunction().
c++ r rcpp
3
"I am trying to use Rcpp for the first time." Welcome! I (somewhat strongly) recommend you give the Brief Introduction to Rcpp vignette a read. You should not needcxxfunction()
.
– Dirk Eddelbuettel
Mar 20 at 13:52
1
You almost certainly want to declarepower
as astd:vector<double>
. In fact, unlesstotalRD
is a compile-time constant, your current code is invalid anyway.
– Konrad Rudolph
Mar 20 at 14:07
1
Please do not use theinline
package. For help writing code with Rcpp attributes, please see: "To Rcpp Attributers and Beyond from Inline" (Disclaimer: I wrote this.)
– coatless
Mar 20 at 14:09
@tsv are you planning on cleaning this up by using Rcpp Attributes and making it reproducible? If not, we can't really help out.
– coatless
Mar 21 at 14:27
@coatless I am usually an R user, I have made a script in C++ that works (although I don't know what compile-time constant and things mean so maybe I'm doing it wrong there??) I'm now trying to put my code into R as the original example that I saw you seemed to be able to do that. Do I need to include Rcpp Attributes within the C++ script in order to make this work?
– tsv
Mar 22 at 13:41
add a comment |
I am trying to use Rcpp for the first time. I have made a script in C++ that works fine independently of R, but when trying to implement it in Rcpp I am getting an error message about the return type. Here is the gist of what I've done:
cppSim <- '
#include <iostream>
#include <random>
#include <math.h>
//[[Rcpp::plugins(cpp11)]]
#include <Rcpp.h>
#include <cmath>
#include <iomanip>
#include <boost/math/distributions/students_t.hpp>
using boost::math::students_t;
using namespace std;
using namespace Rcpp;
// function to calculate p-value from a t-test
double ttestPValue(vector<double> obs1, vector<double> obs2, int nSamples)
...
return(q); \ q is a double, this bit works fine
NumericVector powerSimulation(int nSamples, int meanDiff, int nPerm, double pValueCutOff)
...
// create vectors to hold data
double sim1T[totalRD][nPerm];
...
sim1T[rdmu - minRD][j] = ttestPValue(obs1, obs2, nSamples);
double power[totalRD];
for (int j = 0; j < 100; j++)
power[j] = 0;
for (int i = 0; i < totalRD; i++)
for (int sum = 0; sum < nPerm; sum ++)
if (sim1T[i][sum] < pValueCutOff)
power[i] = power[i] + 1;
power[i] = (power[i] / nPerm)*100;
return power;
'
To run it I am using:
settings=getPlugin("Rcpp")
settings$env$PKG_CXXFLAGS=paste('-std=c++11',settings$env$PKG_CXXFLAGS,sep=' ')
simRcpp <- cxxfunction(signature(nSamplesR = "int", meanDiffR = "int", nPermR = "int", pValueCutOffR = "double"),
plugin="Rcpp",
settings = settings,
includes = cppSim,
body='
int nSamples = Rcpp::as<int>(nSamplesR);
int meanDiff = Rcpp::as<int>(meanDiffR);
int nPerm = Rcpp::as<int>(nPermR);
double pValueCutOff = Rcpp::as<double>(pValueCutOffR);
return Rcpp::wrap( cppSim(nSamples, meanDiff, nPerm, pValueCutOff));')
The error that I'm getting is:
file928498473e2.cpp: In function ‘Rcpp::NumericVector powerSimulation(int, int, int, double)’:
file928498473e2.cpp:165:8: error: invalid conversion from ‘double*’ to ‘const int&’ [-fpermissive]
return power;
^
I get that my return type for power is wrong but how do I make it right? I've tried wrapping it as a NumericVector but that doesn't seem to work and I can't find an example with the same issue, as not everyone uses cxxfunction().
c++ r rcpp
I am trying to use Rcpp for the first time. I have made a script in C++ that works fine independently of R, but when trying to implement it in Rcpp I am getting an error message about the return type. Here is the gist of what I've done:
cppSim <- '
#include <iostream>
#include <random>
#include <math.h>
//[[Rcpp::plugins(cpp11)]]
#include <Rcpp.h>
#include <cmath>
#include <iomanip>
#include <boost/math/distributions/students_t.hpp>
using boost::math::students_t;
using namespace std;
using namespace Rcpp;
// function to calculate p-value from a t-test
double ttestPValue(vector<double> obs1, vector<double> obs2, int nSamples)
...
return(q); \ q is a double, this bit works fine
NumericVector powerSimulation(int nSamples, int meanDiff, int nPerm, double pValueCutOff)
...
// create vectors to hold data
double sim1T[totalRD][nPerm];
...
sim1T[rdmu - minRD][j] = ttestPValue(obs1, obs2, nSamples);
double power[totalRD];
for (int j = 0; j < 100; j++)
power[j] = 0;
for (int i = 0; i < totalRD; i++)
for (int sum = 0; sum < nPerm; sum ++)
if (sim1T[i][sum] < pValueCutOff)
power[i] = power[i] + 1;
power[i] = (power[i] / nPerm)*100;
return power;
'
To run it I am using:
settings=getPlugin("Rcpp")
settings$env$PKG_CXXFLAGS=paste('-std=c++11',settings$env$PKG_CXXFLAGS,sep=' ')
simRcpp <- cxxfunction(signature(nSamplesR = "int", meanDiffR = "int", nPermR = "int", pValueCutOffR = "double"),
plugin="Rcpp",
settings = settings,
includes = cppSim,
body='
int nSamples = Rcpp::as<int>(nSamplesR);
int meanDiff = Rcpp::as<int>(meanDiffR);
int nPerm = Rcpp::as<int>(nPermR);
double pValueCutOff = Rcpp::as<double>(pValueCutOffR);
return Rcpp::wrap( cppSim(nSamples, meanDiff, nPerm, pValueCutOff));')
The error that I'm getting is:
file928498473e2.cpp: In function ‘Rcpp::NumericVector powerSimulation(int, int, int, double)’:
file928498473e2.cpp:165:8: error: invalid conversion from ‘double*’ to ‘const int&’ [-fpermissive]
return power;
^
I get that my return type for power is wrong but how do I make it right? I've tried wrapping it as a NumericVector but that doesn't seem to work and I can't find an example with the same issue, as not everyone uses cxxfunction().
c++ r rcpp
c++ r rcpp
asked Mar 20 at 13:46
tsvtsv
32 bronze badges
32 bronze badges
3
"I am trying to use Rcpp for the first time." Welcome! I (somewhat strongly) recommend you give the Brief Introduction to Rcpp vignette a read. You should not needcxxfunction()
.
– Dirk Eddelbuettel
Mar 20 at 13:52
1
You almost certainly want to declarepower
as astd:vector<double>
. In fact, unlesstotalRD
is a compile-time constant, your current code is invalid anyway.
– Konrad Rudolph
Mar 20 at 14:07
1
Please do not use theinline
package. For help writing code with Rcpp attributes, please see: "To Rcpp Attributers and Beyond from Inline" (Disclaimer: I wrote this.)
– coatless
Mar 20 at 14:09
@tsv are you planning on cleaning this up by using Rcpp Attributes and making it reproducible? If not, we can't really help out.
– coatless
Mar 21 at 14:27
@coatless I am usually an R user, I have made a script in C++ that works (although I don't know what compile-time constant and things mean so maybe I'm doing it wrong there??) I'm now trying to put my code into R as the original example that I saw you seemed to be able to do that. Do I need to include Rcpp Attributes within the C++ script in order to make this work?
– tsv
Mar 22 at 13:41
add a comment |
3
"I am trying to use Rcpp for the first time." Welcome! I (somewhat strongly) recommend you give the Brief Introduction to Rcpp vignette a read. You should not needcxxfunction()
.
– Dirk Eddelbuettel
Mar 20 at 13:52
1
You almost certainly want to declarepower
as astd:vector<double>
. In fact, unlesstotalRD
is a compile-time constant, your current code is invalid anyway.
– Konrad Rudolph
Mar 20 at 14:07
1
Please do not use theinline
package. For help writing code with Rcpp attributes, please see: "To Rcpp Attributers and Beyond from Inline" (Disclaimer: I wrote this.)
– coatless
Mar 20 at 14:09
@tsv are you planning on cleaning this up by using Rcpp Attributes and making it reproducible? If not, we can't really help out.
– coatless
Mar 21 at 14:27
@coatless I am usually an R user, I have made a script in C++ that works (although I don't know what compile-time constant and things mean so maybe I'm doing it wrong there??) I'm now trying to put my code into R as the original example that I saw you seemed to be able to do that. Do I need to include Rcpp Attributes within the C++ script in order to make this work?
– tsv
Mar 22 at 13:41
3
3
"I am trying to use Rcpp for the first time." Welcome! I (somewhat strongly) recommend you give the Brief Introduction to Rcpp vignette a read. You should not need
cxxfunction()
.– Dirk Eddelbuettel
Mar 20 at 13:52
"I am trying to use Rcpp for the first time." Welcome! I (somewhat strongly) recommend you give the Brief Introduction to Rcpp vignette a read. You should not need
cxxfunction()
.– Dirk Eddelbuettel
Mar 20 at 13:52
1
1
You almost certainly want to declare
power
as a std:vector<double>
. In fact, unless totalRD
is a compile-time constant, your current code is invalid anyway.– Konrad Rudolph
Mar 20 at 14:07
You almost certainly want to declare
power
as a std:vector<double>
. In fact, unless totalRD
is a compile-time constant, your current code is invalid anyway.– Konrad Rudolph
Mar 20 at 14:07
1
1
Please do not use the
inline
package. For help writing code with Rcpp attributes, please see: "To Rcpp Attributers and Beyond from Inline" (Disclaimer: I wrote this.)– coatless
Mar 20 at 14:09
Please do not use the
inline
package. For help writing code with Rcpp attributes, please see: "To Rcpp Attributers and Beyond from Inline" (Disclaimer: I wrote this.)– coatless
Mar 20 at 14:09
@tsv are you planning on cleaning this up by using Rcpp Attributes and making it reproducible? If not, we can't really help out.
– coatless
Mar 21 at 14:27
@tsv are you planning on cleaning this up by using Rcpp Attributes and making it reproducible? If not, we can't really help out.
– coatless
Mar 21 at 14:27
@coatless I am usually an R user, I have made a script in C++ that works (although I don't know what compile-time constant and things mean so maybe I'm doing it wrong there??) I'm now trying to put my code into R as the original example that I saw you seemed to be able to do that. Do I need to include Rcpp Attributes within the C++ script in order to make this work?
– tsv
Mar 22 at 13:41
@coatless I am usually an R user, I have made a script in C++ that works (although I don't know what compile-time constant and things mean so maybe I'm doing it wrong there??) I'm now trying to put my code into R as the original example that I saw you seemed to be able to do that. Do I need to include Rcpp Attributes within the C++ script in order to make this work?
– tsv
Mar 22 at 13:41
add a comment |
1 Answer
1
active
oldest
votes
Please change
double power[totalRD];
into
std::vector<double> power(totalRD);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55262252%2frcpp-not-sure-what-class-return-type-should-be-error-double-not-able-to-conver%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Please change
double power[totalRD];
into
std::vector<double> power(totalRD);
add a comment |
Please change
double power[totalRD];
into
std::vector<double> power(totalRD);
add a comment |
Please change
double power[totalRD];
into
std::vector<double> power(totalRD);
Please change
double power[totalRD];
into
std::vector<double> power(totalRD);
answered Mar 28 at 4:40
Qiang KouQiang Kou
4824 silver badges7 bronze badges
4824 silver badges7 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55262252%2frcpp-not-sure-what-class-return-type-should-be-error-double-not-able-to-conver%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
"I am trying to use Rcpp for the first time." Welcome! I (somewhat strongly) recommend you give the Brief Introduction to Rcpp vignette a read. You should not need
cxxfunction()
.– Dirk Eddelbuettel
Mar 20 at 13:52
1
You almost certainly want to declare
power
as astd:vector<double>
. In fact, unlesstotalRD
is a compile-time constant, your current code is invalid anyway.– Konrad Rudolph
Mar 20 at 14:07
1
Please do not use the
inline
package. For help writing code with Rcpp attributes, please see: "To Rcpp Attributers and Beyond from Inline" (Disclaimer: I wrote this.)– coatless
Mar 20 at 14:09
@tsv are you planning on cleaning this up by using Rcpp Attributes and making it reproducible? If not, we can't really help out.
– coatless
Mar 21 at 14:27
@coatless I am usually an R user, I have made a script in C++ that works (although I don't know what compile-time constant and things mean so maybe I'm doing it wrong there??) I'm now trying to put my code into R as the original example that I saw you seemed to be able to do that. Do I need to include Rcpp Attributes within the C++ script in order to make this work?
– tsv
Mar 22 at 13:41