[Rcpp-devel] Printing intermediate values in C++ code used in Rcpp

Dirk Eddelbuettel edd at debian.org
Sat Dec 17 15:15:28 CET 2022


On 17 December 2022 at 17:29, Christofer Bogaso wrote:
| Hi,
| 
| I am working with a package called
| 
| https://cran.r-project.org/src/contrib/GCPM_1.2.2.tar.gz
| 
| The source code contains C++ codes which are available in the src folder.
| 
| In this folder, the C++ codes are available in cpploss.cpp
| 
| In this cpp file, there is a function like below,
| 
| #ifdef _OPENMP
| #include <omp.h>
| #endif
| // [[Rcpp::depends(RcppProgress)]]
| #include <progress.hpp>
| #include "cpploss.h"
| #include <Rcpp.h>
| #include <Rmath.h>
| #include <iostream>
| 
| 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){
|   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.
| 
| However as I run this C++ codes, I want to print some intermediate
| values generated by underlying C++ codes for some debugging purposes.
| So I added below line (just an example)
| 
| Rcpp::Rcout << "Some Value" << std::endl << 1.23 << std::endl;
| 
| After adding this line, then after re-building the package and
| re-installng it, when I run the R code, I dont get above line printed
| in the R console.

Then you are running a different build of the package. The statement, if
unconditional, _will_ print.  Check your setup.

  > Rcpp::cppFunction('void foo() { Rcpp::Rcout << "boo" << std::endl; }')
  > foo()
  boo
  >

I am currently working a lot with a new package spdl for just this: logging.

  > Rcpp::cppFunction('void spdlfoo() { spdl::debug("Something {} or other {}", 42, "zing"); }', depends="RcppSpdlog", include="#include <spdl.h>")
  > spdl::setup("demo", "warn")  # new logger 'demo' at level 'warn'
  > spdlfoo()   # not shown as 'debug' < 'warn'
  > spdl::set_level("debug")  # lower the level
  > spdlfoo()   # now shown 
  [2022-12-17 08:13:50.491] [demo] [Process: 3817047] [debug] Something 42 or other zing
  > 

Of course you normally want that in a package. Import spdl, and LinkingTo
RcppSpdlog (which came first) does that, more in the vignette of RcppSpdlog.
 
| Could you please help how can I get designated intermadiate values
| printed in my R console when I run the R/C++ code?
| 
| Additionally, what is the meaning of the statements like NumericVector
| PD(PD_a) etc? I understand that PD_a is the function argument. But
| what is the meaning of PD(PD_a)?

Instantiate a new object named 'PD' from an object named 'PD_a'.

Dirk

-- 
dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org


More information about the Rcpp-devel mailing list