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

Christofer Bogaso bogaso.christofer at gmail.com
Sat Dec 17 18:34:45 CET 2022


Hi,

Thanks for your reply.

Can you please help which setup I need to change?

My Rcpp appears to be latest Rcpp_1.0.9, as I get from session
information as below,

> sessionInfo()

R version 4.2.1 (2022-06-23)

Platform: x86_64-apple-darwin17.0 (64-bit)

Running under: macOS Big Sur ... 10.16


Matrix products: default

BLAS:   /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib

LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib


locale:

[1] C/UTF-8/C/C/C/C


attached base packages:

[1] stats     graphics  grDevices utils     datasets  methods   base


other attached packages:

[1] GCPM_1.2.2     devtools_2.4.5 usethis_2.1.6  copula_1.1-1   dplyr_1.0.9


loaded via a namespace (and not attached):

 [1] tidyselect_1.1.2    remotes_2.4.2       purrr_0.3.4

 [4] lattice_0.20-45     pcaPP_2.0-3         vctrs_0.4.1

 [7] generics_0.1.3      miniUI_0.1.1.1      htmltools_0.5.3

[10] stats4_4.2.1        utf8_1.2.2          rlang_1.0.4

[13] pkgbuild_1.3.1      urlchecker_1.0.1    pillar_1.8.1

[16] later_1.3.0         glue_1.6.2          DBI_1.1.3

[19] sessioninfo_1.2.2   lifecycle_1.0.1     stringr_1.4.1

[22] pspline_1.0-19      htmlwidgets_1.5.4   mvtnorm_1.1-3

[25] memoise_2.0.1       callr_3.7.1         fastmap_1.1.0

[28] httpuv_1.6.6        ps_1.7.1            ADGofTest_0.3

[31] parallel_4.2.1      fansi_1.0.3         Rcpp_1.0.9

[34] xtable_1.8-4        promises_1.2.0.1    cachem_1.0.6

[37] pkgload_1.3.0       mime_0.12           fs_1.5.2

[40] RcppProgress_0.4.2  digest_0.6.29       stringi_1.7.8

[43] processx_3.7.0      shiny_1.7.2         gsl_2.1-7.1

[46] numDeriv_2016.8-1.1 grid_4.2.1          stabledist_0.7-1

[49] cli_3.4.1           tools_4.2.1         magrittr_2.0.3

[52] tibble_3.1.8        profvis_0.3.7       crayon_1.5.1

[55] pkgconfig_2.0.3     ellipsis_0.3.2      Matrix_1.5-3

[58] prettyunits_1.1.1   assertthat_0.2.1    R6_2.5.1

[61] compiler_4.2.1

On Sat, Dec 17, 2022 at 7:45 PM Dirk Eddelbuettel <edd at debian.org> wrote:
>
>
> 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