I'm running into a problem using RInside vs the console. This is all run on ubuntu 14.04 using R 3.2.4 installed via apt-get from CRAN. Here is the c++ and R code:
#include <RInside.h> // for the embedded R via RInside
int main(int argc, char *argv[]) {
RInside R(argc, argv); // create an embedded R instance
R.parseEval("source('abline.R')");
}
abline.R
bp <- data.frame(
age = c(28, 23, 52, 42, 27, 29, 43, 34, 40, 28),
systolic = c(70, 68, 90, 75, 68, 80, 78, 70, 80, 72))
str(bp)
attach(bp)
bp.lm <- lm(systolic ~ age)
plot(age, systolic)
abline(bp.lm)
lines(lowess(age, systolic, f=0.75), lty=2)
The R code works fine from the console, but errors when the program is run.
mlilback#rc2x:/tmp/abtest$ ./abtest
'data.frame': 10 obs. of 2 variables:
$ age : num 28 23 52 42 27 29 43 34 40 28
$ systolic: num 70 68 90 75 68 80 78 70 80 72
Error in if (noInt) { : argument is of length zero
terminate called after throwing an instance of 'std::runtime_error'
what(): Error evaluating: source('abline.R')
Aborted (core dumped)
The if (noInt) { is from the source of abline (line 18 in my version of R). I'm completely stumped as to why this only happens via RInside.
Any ideas?
Works for me without any issues. Ubuntu 16.04. Running out of the examples directory to get the GNUmakefile-based build for free:
~/git/rinside/inst/examples/standard(master)$ vi soquestion.cpp
~/git/rinside/inst/examples/standard(master)$ make soquestion
ccache g++ -I/usr/share/R/include -I/usr/local/lib/R/site-library/Rcpp/include \
-I/usr/local/lib/R/site-library/RInside/include -g -O3 -Wall -pipe \
-Wno-unused -Wall soquestion.cpp -Wl,--export-dynamic -fopenmp \
-L/usr/lib/R/lib -lR -lpcre -llzma -lbz2 -lz -lrt -ldl -lm -lblas -llapack \
-L/usr/local/lib/R/site-library/RInside/lib -lRInside \
-Wl,-rpath,/usr/local/lib/R/site-library/RInside/lib -o soquestion
~/git/rinside/inst/examples/standard(master)$ vi abline.R
~/git/rinside/inst/examples/standard(master)$ ./soquestion
'data.frame': 10 obs. of 2 variables:
$ age : num 28 23 52 42 27 29 43 34 40 28
$ systolic: num 70 68 90 75 68 80 78 70 80 72
~/git/rinside/inst/examples/standard(master)$
I literally just copied an pasted your two files. Also:
~/git/rinside/inst/examples/standard(master)$ ls -1tr | tail -4
soquestion.cpp
soquestion
abline.R
Rplots.pdf
~/git/rinside/inst/examples/standard(master)$
You probably want to open a device file via pdf() or png() ...
Related
I am trying to understand glog and therefore trying to run the example code on their github page.
I have installed glog (version - 0.6.0) and its dependency gflags (version - 2.2) on my mac OS (10.15.7)
I compile the example below
#include <glog/logging.h>
int main(int argc, char* argv[]) {
// Initialize Google’s logging library.
google::InitGoogleLogging(argv[0]);
// test with setting a value for num_cookies
int num_cookies = 3;
// ...
LOG(INFO) << "Found " << num_cookies << " cookies";
}
using the following command (and it compiles without any errors or warnings).
g++ glog-test.cpp -I/usr/local/include -L/usr/local/lib -lglog -lgflags -o glog-test.o
When I run the example using the following command,
./glog-test.o --logtostderr=1 --stderrthreshold=0
I expect to see the message Found 3 cookies on my terminal, but I see nothing being printed.
I have also experimented with different values for logtostderr (0, 1) and stderrthreshold (0, 1, 2, 3) and nothing gets written to the directory or gets printed on the terminal.
Any help in understanding what I am doing wrong here would be much appreciated, thank you!
You have to parse the command line flags manually through gflags::ParseCommandLineFlags
#include <glog/logging.h>
#incldue <gflags/gflags.h>
int main(int argc, char* argv[]) {
// Initialize Google’s logging library.
google::InitGoogleLogging(argv[0]);
gflags::ParseCommandLineFlags(&argc, &argv, true);
// test with setting a value for num_cookies
int num_cookies = 3;
// ...
LOG(INFO) << "Found " << num_cookies << " cookies";
}
Just checked, the following works..
GLOG_stderrthreshold=0 GLOG_logtostderr=1 ./glog-test.o
which gives
Found 3 cookies
I do have gflags and installed, as seen by
cd /usr/local/lib
ls -l | grep "libgflags"
which gives
-lrwxr-xr-x 1 sn admin 48 Jun 5 2020 libgflags.2.2.2.dylib -> ../Cellar/gflags/2.2.2/lib/libgflags.2.2.2.dylib
lrwxr-xr-x 1 sn admin 46 Jun 5 2020 libgflags.2.2.dylib -> ../Cellar/gflags/2.2.2/lib/libgflags.2.2.dylib
-rw-r--r-- 1 sn admin 170520 Jun 27 00:35 libgflags.a
lrwxr-xr-x 1 sn admin 42 Jun 5 2020 libgflags.dylib -> ../Cellar/gflags/2.2.2/lib/libgflags.dylib
lrwxr-xr-x 1 sn admin 58 Jun 5 2020 libgflags_nothreads.2.2.2.dylib -> ../Cellar/gflags/2.2.2/lib/libgflags_nothreads.2.2.2.dylib
lrwxr-xr-x 1 sn admin 56 Jun 5 2020 libgflags_nothreads.2.2.dylib -> ../Cellar/gflags/2.2.2/lib/libgflags_nothreads.2.2.dylib
-rw-r--r-- 1 sn admin 168096 Jun 27 00:35 libgflags_nothreads.a
lrwxr-xr-x 1 sn admin 52 Jun 5 2020 libgflags_nothreads.dylib -> ../Cellar/gflags/2.2.2/lib/libgflags_nothreads.dylib
so I am not sure why
./glog-test.o --logtostderr=1 --stderrthreshold=0
didn't work.
I'm parsing the total.info file which is the result of running a few lcov commands on my source code in order to build a service that provides coverage information when needed.
lcov also provides a nice html ui to browse code coverage.
The problem is that i'm getting different values in terms of function coverage in my service than the html ui of lcov.
I'm calculating code coverage on end to end tests and not on unit tests.
Gcc version is 4.8.5 Red Hat
Lcov version is 1.14
The launched lcov commands are in the following order:
1- time lcov -c -i -d . --rc geninfo_adjust_src_path="{path} => " -o base.info
2- time lcov -c -d . --rc geninfo_adjust_src_path="{path} => " -o test.info
3- time lcov -rc geninfo_adjust_src_path="{path} => " -a base.info -a test.info -o total.info
To make it simpler here are 2 sections from the total.info file.
In this section, the parsing is working correctly
There are 5 functions
TN:
SF:{path}/FileOne.hpp
FN:22,_methodName
FN:29,_methodName
FN:31,_methodName
FN:40,_methodName
FN:48,_methodName
FNDA:0,_methodName
FNDA:0,_methodName
FNDA:6,_methodName
FNDA:6,_methodName
FNDA:0,_methodName
FNF:5
FNH:2
DA:22,6
DA:29,12
DA:31,6
DA:33,24
DA:34,0
DA:35,6
DA:36,6
DA:40,0
DA:42,0
DA:43,0
DA:45,0
DA:48,0
DA:50,0
DA:51,0
DA:54,0
LF:15
LH:6
end_of_record
The parsing problems are happening in the section below as some functions appear to have duplicate FN statement (They all start at the same line, so I assume this is referring to the same function)
TN:
SF:{path}/FileTwo.hpp
FN:32,_methodName
FN:39,_methodName
FN:48,_methodName
FN:58,_methodName
FN:64,_methodName
FN:100,_methodName
FN:116,_methodName
FN:116,_methodName
FN:128,_methodName
FN:128,_methodName
FN:128,_methodName
FN:128,_methodName
FN:132,_methodName
FN:138,_methodName
FNDA:0,_methodName
FNDA:0,_methodName
FNDA:0,_methodName
FNDA:0,_methodName
FNDA:0,_methodName
FNDA:0,_methodName
FNDA:0,_methodName
FNDA:0,_methodName
FNDA:0,_methodName
FNDA:0,_methodName
FNDA:0,_methodName
FNDA:0,_methodName
FNDA:0,_methodName
FNDA:0,_methodName
FNF:14
FNH:0
DA:32,0
DA:33,0
DA:39,0
DA:40,0
DA:41,0
DA:44,0
DA:48,0
DA:49,0
DA:51,0
DA:52,0
DA:55,0
DA:56,0
DA:58,0
DA:59,0
DA:60,0
DA:61,0
DA:62,0
DA:64,0
DA:65,0
DA:66,0
DA:68,0
DA:91,0
DA:92,0
DA:96,0
DA:97,0
DA:100,0
DA:101,0
DA:102,0
DA:104,0
DA:105,0
DA:106,0
DA:112,0
DA:116,0
DA:117,0
DA:118,0
DA:121,0
DA:122,0
DA:125,0
DA:128,0
DA:129,0
DA:130,0
DA:132,0
DA:133,0
DA:134,0
DA:135,0
DA:138,0
DA:139,0
DA:140,0
DA:141,0
DA:142,0
DA:145,0
LF:51
LH:0
end_of_record
At lines 116 & 128, the functions are as follows:
115 : template <typename UnaryOp>
116 0 : void remove_if(UnaryOp op) {
117 0 : for (typename Items::iterator it = m_items.begin(), end = m_items.end(); it != end;) {
118 0 : if (!op(*it))
119 : ++it;
120 : else {
121 0 : shutItem(*it);
122 0 : m_items.erase(it++);
123 : }
124 : }
125 0 : }
127 : template <typename Op>
128 0 : void for_each(Op &op) {
129 0 : std::for_each(m_items.begin(), m_items.end(), op);
130 0 : }
My question is why are some functions appearing multiple times (who's "FN:{}" start at the same line)?
In boost::filesystem, the path class always tries to dereference symlinks. A lot of the API is catered towards trying to make symlinks seem invisible. I'm guessing a lot of their syscalls underneath are stat related instead of lstat related. This is problematic for me, as I'm trying to get the symlink itself.
For example, doing fs::exists("some_symlink") because, although some_symlink exists, its referent does not exist. I want this to give me back true, and in the older versions of boost it supports it: https://www.boost.org/doc/libs/1_32_0/libs/filesystem/doc/operations.htm#symbolic_link_exists
However, now it doesnt. Is there any good way of doing this?
You can use boost::filesystem::symlink_status to get a file_status, and then check its type().
#include <boost/filesystem.hpp>
#include <iostream>
#include <iomanip>
int main(){
auto a = boost::filesystem::symlink_status("a_symlink");
auto b = boost::filesystem::symlink_status("main.cpp");
auto c = boost::filesystem::symlink_status("not_existing");
auto d = boost::filesystem::symlink_status("a_broken_symlink");
std::cout << std::boolalpha
<< (a.type() == boost::filesystem::file_not_found) << " "
<< (b.type() == boost::filesystem::file_not_found) << " "
<< (c.type() == boost::filesystem::file_not_found) << " "
<< (d.type() == boost::filesystem::file_not_found) << "\n";
}
Execution:
$ touch f
$ ln -s main.cpp a_symlink
$ ln -s f a_broken_symlink
$ rm f
$ ls -l
total 4
lrwxrwxrwx 1 2001 2000 1 Jun 3 23:33 a_broken_symlink -> f
lrwxrwxrwx 1 2001 2000 8 Jun 3 23:33 a_symlink -> main.cpp
-rw-rw-rw- 1 2001 2000 637 Jun 3 23:33 main.cpp
$ g++ -std=c++14 -Wall -Wextra -pedantic -lboost_filesystem -lboost_system main.cpp && ./a.out
false false true false
Live on Coliru
I am using the latest tree.hh from http://tree.phi-sci.com/download.html.
It produces the errors (please see below). How can I fix it? How can this class produce so many errors?
Thanks in advance for your suggesstions?
In file included from item_tree.h:4:0,
from Player.h:44,
from xinemediaplayer.c:26:
tree.hh:82:36: error: expected ‘,’ or ‘...’ before ‘&&’ token
tree(tree<T, tree_node_allocator>&&); // move constructor^M
^
tree.hh:82:38: error: invalid constructor; you probably meant ‘tree<T, tree_node_allocator> (const tree<T, tree_node_allocator>&)’
tree(tree<T, tree_node_allocator>&&); // move constructor^M
^
tree.hh:85:70: error: expected ‘,’ or ‘...’ before ‘&&’ token
tree<T,tree_node_allocator>& operator=(tree<T, tree_node_allocator>&&); // move assignment^M
^
tree.hh:503:64: error: expected ‘,’ or ‘...’ before ‘&&’ token
tree<T, tree_node_allocator>::tree(tree<T, tree_node_allocator>&& x) ^M
^
tree.hh:503:1: error: prototype for ‘tree<T, tree_node_allocator>::tree(tree<T, tree_node_allocator>)’ does not match any in class ‘tree<T, tree_node_allocator>’
tree<T, tree_node_allocator>::tree(tree<T, tree_node_allocator>&& x) ^M
^
tree.hh:81:3: error: candidates are: tree<T, tree_node_allocator>::tree(const tree<T, tree_node_allocator>&)
tree(const tree<T, tree_node_allocator>&); // copy constructor^M
^
tree.hh:80:3: error: tree<T, tree_node_allocator>::tree(const tree<T, tree_node_allocator>::iterator_base&)
tree(const iterator_base&);^M
^
tree.hh:496:1: error: tree<T, tree_node_allocator>::tree(const T&)
tree<T, tree_node_allocator>::tree(const T& x) ^M
^
tree.hh:490:1: error: tree<T, tree_node_allocator>::tree()
tree<T, tree_node_allocator>::tree() ^M
^
tree.hh:562:98: error: expected ‘,’ or ‘...’ before ‘&&’ token
tree<T,tree_node_allocator>& tree<T, tree_node_allocator>::operator=(tree<T, tree_node_allocator>&& x)^M
^
tree.hh: In member function ‘tree<T, tree_node_allocator>& tree<T, tree_node_allocator>::operator=(tree<T, tree_node_allocator>)’:...
an example of tree.hh:
64 template <class T, class tree_node_allocator = std::allocator<tree_node_<T> > >
65 class tree {
66 protected:
67 typedef tree_node_<T> tree_node;
68 public:
69 /// Value of the data stored at a node.
70 typedef T value_type;
71
72 class iterator_base;
73 class pre_order_iterator;
74 class post_order_iterator;
75 class sibling_iterator;
76 class leaf_iterator;
77
78 tree(); // empty constructor
79 tree(const T&); // constructor setting given element as head
80 tree(const iterator_base&);
81 tree(const tree<T, tree_node_allocator>&); // copy constructor
82 tree(tree<T, tree_node_allocator>&&); // move constructor
83 ~tree();
84 tree<T,tree_node_allocator>& operator=(const tree<T, tree_node_allocator>&); // copy assignment
85 tree<T,tree_node_allocator>& operator=(tree<T, tree_node_allocator>&&); // move assignment
86
87 /// Base class for iterators, only pointers stored, no traversal logic.
88 #ifdef __SGI_STL_PORT
89 class iterator_base : public stlport::bidirectional_iterator<T, ptrdiff_t> {
90 #else
The Makefile is:
1 PLUGIN = xinemediaplayer
2 OBJS = $(PLUGIN).o Control.o Player.o Reel.o SpuDecode.o Utils.o \
3 curl_download.o imagetools.o \
4 item_tree.o m3u_parser.o pls_parser.o timecounter.o \
5 xineOsd.o xineOsdInfoMenu.o xineOsdFunction.o Playlist.o
6 VDRDIR = ../../..
7 LIBDIR = ../../lib
8 TMPDIR = /tmp
9 -include $(VDRDIR)/Make.config
10 ARCHIVE = $(PLUGIN)-$(VERSION)
11 PACKAGE = vdr-$(ARCHIVE)
12 OBJS += cddb.o cdtext.o cdrom.o
13 OBJS += XineLib.o GstreamerLib.o gstTools.o ExternalLib.o
14 LIBS += -lxine `pkg-config --libs gstreamer-0.10` `pkg-config --libs gstreamer-interfaces-0.10`
15 INCLUDES += `pkg-config --cflags gstreamer-0.10` `pkg-config --cflags gstreamer-interfaces-0.10`
16 -include $(VDRDIR)/Make.common
17 INCLUDES +=
18 INCLUDES +=`taglib-config --cflags`
19 LDFLAGS +=`taglib-config --libs`
20 LIBS += `curl-config --libs` -lcdio -lcddb
21
22 DEFINES += -DPLAYER_VERSION=\"$(PLAYER_VERSION)\" -D__LINUX__ -D__STL_CONFIG_H
23
24 target-for-compatibility-with-vanilla-vdr:
25 $(LIBDIR)/$#.$(APIVERSION)
Make.config:
1 REELVDR=1
2 DEBUG = 1
3 VDRVER ?= 2.1
4 CCACHE := $(shell which ccache)
5 MAKE += -j3
6 LSB_RELEASE := $(shell lsb_release -sr)
7 MACHINE ?= -m32 -march=pentium3 -mmmx -msse -mfpmath=sse
8 CC = $(CCACHE) $(DISTCC) $(CROSS)gcc
9 CFLAGS = -O2 -g -pg $(MACHINE)
10 CXX = $(CCACHE) $(DISTCC) $(CROSS)g++
11 CXXFLAGS = -g3 -funroll-loops -fomit-frame-pointer $(MACHINE) -Wall -Woverloaded-virtual -D_FILE_OFFSET_BITS=64 -D_ LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE
12 STRIP = $(CROSS)strip
13 export CC CXX CFLAGS CXXFLAGS
14 CC := $(CC)-4.8
15 CXX := $(CXX)-4.8
16 DEFINES += -DDVBAPI_V5
17 DEFINES += -DNEW_FFMPEG
18 USEMYSQL = 1
19 DEVICE_ATTRIBUTES = 1
20 DVBDIR := /usr/src/linux-headers-3.13.0-66-generic/include/config/dvb
21 VDRVER := 2.1
&& is C++ 11 feature.
You should use --std=c++11 or --std=gnu++11 command line option when making g++ call.
I am trying to report errors from my rcpp code. I am using the constructor exception (const char *message_, const char *file, int line) from http://dirk.eddelbuettel.com/code/rcpp/html/classRcpp_1_1exception.html. To isolate the problem, I wrote the following bar.cpp:
#include <Rcpp.h>
RcppExport SEXP bar( SEXP x){
throw(Rcpp::exception("My Error Message","bar.cpp",4));
return x ;
}
When I run it in R, this is what I get:
> dyn.load("bar.so")
> is.loaded("bar")
[1] TRUE
> .Call("bar",12)
Error: SET_VECTOR_ELT() can only be applied to a 'list', not a 'NULL'
>
You can either
use the inline package which puts a try/catch block into the function it generates for you (by using two simple macros)
or do it manually yourself as shown in a bunch of examples on my blog, or in the examples/ section of the Rcpp package,
but doing what you (ie: throwing outside of a try/catch block) can never work.
As an added bonus, here is a complete example (which essentially already existed in the unit tests):
R> library(inline)
R> f <- cxxfunction(signature(), plugin="Rcpp", body='
+ throw std::range_error("boom");
+ return R_NilValue;
+ ')
R> f()
Error in f() : boom
R>
Again, cxxfunction() puts a try/catch() block in here for you as you can see if you turn verbose on:
R> f <- cxxfunction(signature(), plugin="Rcpp", body='
+ throw std::range_error("boom");
+ return R_NilValue;
+ ', verbose=TRUE)
>> setting environment variables:
PKG_LIBS = -L/usr/local/lib/R/site-library/Rcpp/lib \
-lRcpp -Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib
>> LinkingTo : Rcpp
CLINK_CPPFLAGS = -I"/usr/local/lib/R/site-library/Rcpp/include"
>> Program source :
1 :
2 : // includes from the plugin
3 :
4 : #include <Rcpp.h>
5 :
6 :
7 : #ifndef BEGIN_RCPP
8 : #define BEGIN_RCPP
9 : #endif
10 :
11 : #ifndef END_RCPP
12 : #define END_RCPP
13 : #endif
14 :
15 : using namespace Rcpp;
16 :
17 :
18 :
19 : // user includes
20 :
21 :
22 : // declarations
23 : extern "C" {
24 : SEXP file4cc53282( ) ;
25 : }
26 :
27 : // definition
28 :
29 : SEXP file4cc53282( ){
30 : BEGIN_RCPP
31 :
32 : throw std::range_error("boom");
33 : return R_NilValue;
34 :
35 : END_RCPP
36 : }
37 :
38 :
Compilation argument:
/usr/lib/R/bin/R CMD SHLIB file4cc53282.cpp 2> file4cc53282.cpp.err.txt
ccache g++-4.6 -I/usr/share/R/include \
-I"/usr/local/lib/R/site-library/Rcpp/include" \
-fpic -g0 -O3 -Wall -pipe -Wno-unused -pedantic -c file4cc53282.cpp \
-o file4cc53282.o
g++ -shared -o file4cc53282.so file4cc53282.o \
-L/usr/local/lib/R/site-library/Rcpp/lib \
-lRcpp -Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib \
-L/usr/lib/R/lib -lR
R>
The BEGIN_RCPP and END_CPP add the magic you need here.
Please do move your questions to rcpp-devel.
Just wrap your code inside BEGIN_RCPP/END_RCPP:
RcppExport SEXP bar( SEXP x){
BEGIN_RCPP
throw(Rcpp::exception("My Error Message","bar.cpp",4));
return x ;
END_RCPP
}
Note that you can throw normal std exceptions too:
throw std::invalid_argument("'x' is too short");