Re: [R] Adding comment in C++ code for debugging purpose

2022-12-17 Thread Rui Barradas

Às 13:38 de 17/12/2022, Christofer Bogaso escreveu:

Hi,

Below is the position of Rcout and underlying C++ function

#ifdef _OPENMP
#include 
#endif
// [[Rcpp::depends(RcppProgress)]]
#include 
#include "cpploss.h"
#include 
#include 
#include 

using namespace Rcpp;

// [[Rcpp::export]]
SEXP  GCPM_cpploss(SEXP default_distr_a,SEXP link_function_a, SEXP
S_a,SEXP Sigma_a, SEXP W_a, SEXP PD_a, SEXP PL_a, SEXP calc_rc_a, SEXP
loss_thr_a, SEXP max_entries_a){
   Rcpp::Rcout << "Some Value" << std::endl << 1.23 << std::endl;
   NumericMatrix S(S_a), W(W_a),Sigma(Sigma_a);
   NumericVector PD(PD_a),
PL(PL_a),max_entries(max_entries_a),default_distr(default_distr_a),link_function(link_function_a),calc_rc(calc_rc_a),loss_thr(loss_thr_a);
   List ret;

etc

This still does not print in the console.

Not sure if I am missing something.

Thanks and regards,

On Sat, Dec 17, 2022 at 6:57 PM Rui Barradas  wrote:


Às 11:05 de 17/12/2022, Christofer Bogaso escreveu:

Hi Rui,

Unfortunately, the code Rcpp::Rcout << "-->.My values" <<
"\n"; still not printing the value.

Regarding your second suggestion, R_Print("My values\n"); - where
should I put this statement?

On Sat, Dec 17, 2022 at 11:39 AM Rui Barradas  wrote:


Às 23:32 de 16/12/2022, Christofer Bogaso escreveu:

Hi,

I am using an R package where there are some C++ code.

To check some intermediate values generated by that C++ code, I added
a line like

std::cout << "My values";

Now with this modification, I next build a modified package (source) using

R CMD build

Next I install this modified package using install.packages() function

However during the run-time of the C++ code with above modification, I
dont get any print with "My values"

Can you please help to understand why am not getting that printed?

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Hello,

If the package uses Rcpp try instead


Rcpp::Rcout << "My values" << "\n";   // or std::endl


More generally, there is also


R_Print("My values\n");



Hope this helps,

Rui Barradas






Hello,

Put R_Print where your cout is and instead of it.

As for Rcpp::Rcout, instead of "\n" try ending the line with std::endl
to immediately flush the buffer.

Hope this helps,

Rui Barradas



Hello,

1. Forget R_print, it is meant for C code, not for Rcpp.
   Besides, it is also an error of mine, I was thinking about Rprintf, 
see below.


2. The following works as expected.
   It's the first example in

vignette("Rcpp-introduction", package = "Rcpp")

   with the if/Rcout and Rprintf instructions added.



File: convolve_rcpp.cpp



#include "Rcpp.h"

using namespace Rcpp;
// [[Rcpp::export]]
NumericVector convolve_cpp(const NumericVector& a, const NumericVector& b) {
  // Declare loop counters, and vector sizes
  int i, j,
na = a.size(), nb = b.size(),
nab = na + nb - 1;
// Create vector filled with 0
NumericVector ab(nab);
// Crux of the algorithm
for(i = 0; i < na; i++) {
// this works as expected, it prints to the console
// printing is on every 10 times through the loop
if(i % 10 == 0)  {
Rcpp::Rcout << "Some Value " << i << "\n";
Rcpp::Rcout << " Value " << a[i] << std::endl;
}
for(j = 0; j < nb; j++) {
ab[i + j] += a[i] * b[j];
}
}
Rprintf("Done\n");
// Return result
return ab;
}



Then run the R script


path <- "~/path/to/Rcpp/code"
fl <- list.files(path, pattern = "convolve\\.cpp")
fl <- file.path(path, fl)

Rcpp::sourceCpp(fl)

x <- 1:1e4
y <- 10:1

z <- convolve_cpp(x, y)
str(z)
# num [1:10009] 10 29 56 90 130 175 224 276 330 385 ...


Hope this helps,

Rui Barradas

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Adding comment in C++ code for debugging purpose

2022-12-17 Thread Christofer Bogaso
Hi,

Below is the position of Rcout and underlying C++ function

#ifdef _OPENMP
#include 
#endif
// [[Rcpp::depends(RcppProgress)]]
#include 
#include "cpploss.h"
#include 
#include 
#include 

using namespace Rcpp;

// [[Rcpp::export]]
SEXP  GCPM_cpploss(SEXP default_distr_a,SEXP link_function_a, SEXP
S_a,SEXP Sigma_a, SEXP W_a, SEXP PD_a, SEXP PL_a, SEXP calc_rc_a, SEXP
loss_thr_a, SEXP max_entries_a){
  Rcpp::Rcout << "Some Value" << std::endl << 1.23 << std::endl;
  NumericMatrix S(S_a), W(W_a),Sigma(Sigma_a);
  NumericVector PD(PD_a),
PL(PL_a),max_entries(max_entries_a),default_distr(default_distr_a),link_function(link_function_a),calc_rc(calc_rc_a),loss_thr(loss_thr_a);
  List ret;

etc

This still does not print in the console.

Not sure if I am missing something.

Thanks and regards,

On Sat, Dec 17, 2022 at 6:57 PM Rui Barradas  wrote:
>
> Às 11:05 de 17/12/2022, Christofer Bogaso escreveu:
> > Hi Rui,
> >
> > Unfortunately, the code Rcpp::Rcout << "-->.My values" <<
> > "\n"; still not printing the value.
> >
> > Regarding your second suggestion, R_Print("My values\n"); - where
> > should I put this statement?
> >
> > On Sat, Dec 17, 2022 at 11:39 AM Rui Barradas  wrote:
> >>
> >> Às 23:32 de 16/12/2022, Christofer Bogaso escreveu:
> >>> Hi,
> >>>
> >>> I am using an R package where there are some C++ code.
> >>>
> >>> To check some intermediate values generated by that C++ code, I added
> >>> a line like
> >>>
> >>> std::cout << "My values";
> >>>
> >>> Now with this modification, I next build a modified package (source) using
> >>>
> >>> R CMD build
> >>>
> >>> Next I install this modified package using install.packages() function
> >>>
> >>> However during the run-time of the C++ code with above modification, I
> >>> dont get any print with "My values"
> >>>
> >>> Can you please help to understand why am not getting that printed?
> >>>
> >>> __
> >>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >>> https://stat.ethz.ch/mailman/listinfo/r-help
> >>> PLEASE do read the posting guide 
> >>> http://www.R-project.org/posting-guide.html
> >>> and provide commented, minimal, self-contained, reproducible code.
> >>
> >> Hello,
> >>
> >> If the package uses Rcpp try instead
> >>
> >>
> >> Rcpp::Rcout << "My values" << "\n";   // or std::endl
> >>
> >>
> >> More generally, there is also
> >>
> >>
> >> R_Print("My values\n");
> >>
> >>
> >>
> >> Hope this helps,
> >>
> >> Rui Barradas
> >>
> >>
> >>
> >>
>
> Hello,
>
> Put R_Print where your cout is and instead of it.
>
> As for Rcpp::Rcout, instead of "\n" try ending the line with std::endl
> to immediately flush the buffer.
>
> Hope this helps,
>
> Rui Barradas
>
>

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Adding comment in C++ code for debugging purpose

2022-12-17 Thread Rui Barradas

Às 11:05 de 17/12/2022, Christofer Bogaso escreveu:

Hi Rui,

Unfortunately, the code Rcpp::Rcout << "-->.My values" <<
"\n"; still not printing the value.

Regarding your second suggestion, R_Print("My values\n"); - where
should I put this statement?

On Sat, Dec 17, 2022 at 11:39 AM Rui Barradas  wrote:


Às 23:32 de 16/12/2022, Christofer Bogaso escreveu:

Hi,

I am using an R package where there are some C++ code.

To check some intermediate values generated by that C++ code, I added
a line like

std::cout << "My values";

Now with this modification, I next build a modified package (source) using

R CMD build

Next I install this modified package using install.packages() function

However during the run-time of the C++ code with above modification, I
dont get any print with "My values"

Can you please help to understand why am not getting that printed?

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Hello,

If the package uses Rcpp try instead


Rcpp::Rcout << "My values" << "\n";   // or std::endl


More generally, there is also


R_Print("My values\n");



Hope this helps,

Rui Barradas






Hello,

Put R_Print where your cout is and instead of it.

As for Rcpp::Rcout, instead of "\n" try ending the line with std::endl 
to immediately flush the buffer.


Hope this helps,

Rui Barradas

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Adding comment in C++ code for debugging purpose

2022-12-17 Thread Christofer Bogaso
Hi Rui,

Unfortunately, the code Rcpp::Rcout << "-->.My values" <<
"\n"; still not printing the value.

Regarding your second suggestion, R_Print("My values\n"); - where
should I put this statement?

On Sat, Dec 17, 2022 at 11:39 AM Rui Barradas  wrote:
>
> Às 23:32 de 16/12/2022, Christofer Bogaso escreveu:
> > Hi,
> >
> > I am using an R package where there are some C++ code.
> >
> > To check some intermediate values generated by that C++ code, I added
> > a line like
> >
> > std::cout << "My values";
> >
> > Now with this modification, I next build a modified package (source) using
> >
> > R CMD build
> >
> > Next I install this modified package using install.packages() function
> >
> > However during the run-time of the C++ code with above modification, I
> > dont get any print with "My values"
> >
> > Can you please help to understand why am not getting that printed?
> >
> > __
> > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
> Hello,
>
> If the package uses Rcpp try instead
>
>
> Rcpp::Rcout << "My values" << "\n";   // or std::endl
>
>
> More generally, there is also
>
>
> R_Print("My values\n");
>
>
>
> Hope this helps,
>
> Rui Barradas
>
>
>
>

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Adding comment in C++ code for debugging purpose

2022-12-17 Thread Christofer Bogaso
Hi Ivan,

This is a very nice point. I will check this out.

Thanks and regards,

On Sat, Dec 17, 2022 at 1:53 PM Ivan Krylov  wrote:
>
> On Sat, 17 Dec 2022 05:02:33 +0530
> Christofer Bogaso  wrote:
>
> > I am using an R package where there are some C++ code.
> >
> > To check some intermediate values generated by that C++ code, I added
> > a line like
> >
> > std::cout << "My values";
>
> A more efficient way of debugging C++ code running under R could be
> with the use of a C++ debugger. For example,
>
> 0) Make sure you have GDB (or a different debugger, e.g. lldb on a Mac)
> installed. You probably do, since you already have a working C++
> toolchain.
>
> 1) Run R using the command line: R -g gdb. The debugger will start and
> wait for your commands. Type: run. This will start R.
>
> 2) Type library(YOUR_PACKAGE_NAME) to load the package. You don't have
> to do it in this order (GDB can remember to set a breakpoint on a
> function that's not yet loaded in the address space), but this way you
> can be sure about the name of the function.
>
> 3) Press Ctrl-C, sending an interrupt to the application. GDB handles
> the interrupt and presents you with its prompt again. Type:
> break YOUR_FUNCTION_NAME. If the function name is right, GDB will tell
> you that a breakpoint is set. If not, it will ask you whether to
> postpone setting a breakpoint until the function shows up. Since the
> shared library should have been loaded by this time, the correct answer
> would be probably "no".
>
> 4) Type: continue. The debugger will give the control back to R. You
> can press Enter once and see R print the prompt symbol (">") again. Run
> your code that uses the package.
>
> 5) Eventually, you'll hit the breakpoint and find yourself in the
> debugger again. Like in the R browser, you can use "n" to step line by
> line, "s" to step inside function calls, and "c" to continue execution
> uninterrupted. You can also use "bt" and "frame NUMBER" to inspect the
> call stack and "print EXPRESSION" to see the values of various objects.
>
> A longer guide to GDB can be found at .
> Telling GDB where the package source code is [*] will ease the process
> even further, as would obtaining debugging symbols and the source code
> for R itself.
>
> --
> Best regards,
> Ivan
>
> [*]
> https://sourceware.org/gdb/onlinedocs/gdb/Source-Path.html
> https://alex.dzyoba.com/blog/gdb-source-path/

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Adding comment in C++ code for debugging purpose

2022-12-17 Thread Ivan Krylov
On Sat, 17 Dec 2022 05:02:33 +0530
Christofer Bogaso  wrote:

> I am using an R package where there are some C++ code.
> 
> To check some intermediate values generated by that C++ code, I added
> a line like
> 
> std::cout << "My values";

A more efficient way of debugging C++ code running under R could be
with the use of a C++ debugger. For example,

0) Make sure you have GDB (or a different debugger, e.g. lldb on a Mac)
installed. You probably do, since you already have a working C++
toolchain.

1) Run R using the command line: R -g gdb. The debugger will start and
wait for your commands. Type: run. This will start R.

2) Type library(YOUR_PACKAGE_NAME) to load the package. You don't have
to do it in this order (GDB can remember to set a breakpoint on a
function that's not yet loaded in the address space), but this way you
can be sure about the name of the function.

3) Press Ctrl-C, sending an interrupt to the application. GDB handles
the interrupt and presents you with its prompt again. Type:
break YOUR_FUNCTION_NAME. If the function name is right, GDB will tell
you that a breakpoint is set. If not, it will ask you whether to
postpone setting a breakpoint until the function shows up. Since the
shared library should have been loaded by this time, the correct answer
would be probably "no".

4) Type: continue. The debugger will give the control back to R. You
can press Enter once and see R print the prompt symbol (">") again. Run
your code that uses the package.

5) Eventually, you'll hit the breakpoint and find yourself in the
debugger again. Like in the R browser, you can use "n" to step line by
line, "s" to step inside function calls, and "c" to continue execution
uninterrupted. You can also use "bt" and "frame NUMBER" to inspect the
call stack and "print EXPRESSION" to see the values of various objects.

A longer guide to GDB can be found at .
Telling GDB where the package source code is [*] will ease the process
even further, as would obtaining debugging symbols and the source code
for R itself.

-- 
Best regards,
Ivan

[*]
https://sourceware.org/gdb/onlinedocs/gdb/Source-Path.html
https://alex.dzyoba.com/blog/gdb-source-path/

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Adding comment in C++ code for debugging purpose

2022-12-16 Thread Rui Barradas

Às 23:32 de 16/12/2022, Christofer Bogaso escreveu:

Hi,

I am using an R package where there are some C++ code.

To check some intermediate values generated by that C++ code, I added
a line like

std::cout << "My values";

Now with this modification, I next build a modified package (source) using

R CMD build

Next I install this modified package using install.packages() function

However during the run-time of the C++ code with above modification, I
dont get any print with "My values"

Can you please help to understand why am not getting that printed?

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Hello,

If the package uses Rcpp try instead


Rcpp::Rcout << "My values" << "\n";   // or std::endl


More generally, there is also


R_Print("My values\n");



Hope this helps,

Rui Barradas

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] Adding comment in C++ code for debugging purpose

2022-12-16 Thread Jeff Newmiller
a) This is not the Rcpp help list.

b) Writing to stdout without going through R is not supported in packages. Do 
read the Writing R Extensions manual.

On December 16, 2022 3:32:33 PM PST, Christofer Bogaso 
 wrote:
>Hi,
>
>I am using an R package where there are some C++ code.
>
>To check some intermediate values generated by that C++ code, I added
>a line like
>
>std::cout << "My values";
>
>Now with this modification, I next build a modified package (source) using
>
>R CMD build
>
>Next I install this modified package using install.packages() function
>
>However during the run-time of the C++ code with above modification, I
>dont get any print with "My values"
>
>Can you please help to understand why am not getting that printed?
>
>__
>R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

-- 
Sent from my phone. Please excuse my brevity.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] Adding comment in C++ code for debugging purpose

2022-12-16 Thread Christofer Bogaso
Hi,

I am using an R package where there are some C++ code.

To check some intermediate values generated by that C++ code, I added
a line like

std::cout << "My values";

Now with this modification, I next build a modified package (source) using

R CMD build

Next I install this modified package using install.packages() function

However during the run-time of the C++ code with above modification, I
dont get any print with "My values"

Can you please help to understand why am not getting that printed?

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.