Skip to content

Commit

Permalink
allow custom msg in ProgressCounter (solves #70)
Browse files Browse the repository at this point in the history
  • Loading branch information
tnagler committed Oct 16, 2023
1 parent 71681f5 commit 2c33e09
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
7 changes: 4 additions & 3 deletions inst/include/RcppThread/Progress.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,12 @@ class ProgressCounter : public ProgressPrinter {
//! constructs a progress counter.
//! @param numIt total number of iterations.
//! @param printEvery how regularly to print updates (in seconds).
ProgressCounter(size_t numIt, size_t printEvery) :
ProgressPrinter(numIt, printEvery)
ProgressCounter(size_t numIt, size_t printEvery, std::string custom_msg = "Computing: ") :
ProgressPrinter(numIt, printEvery), cmsg(custom_msg)
{}

private:
std::string cmsg;
//! prints progress in percent to the R console (+ an estimate of remaining
//! time).
void printProgress() {
Expand All @@ -142,7 +143,7 @@ class ProgressCounter : public ProgressPrinter {
if (it_ == numIt_)
isDone_ = true;
std::ostringstream msg;
msg << "\rComputing: " << progressString();
msg << "\r" << cmsg << progressString();
Rcout << msg.str();
}
};
Expand Down
12 changes: 12 additions & 0 deletions tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,18 @@ testProgressCounter()
});
}

// [[Rcpp::export]]
void
testProgressCounter2()
{
// 20 iterations in loop, update progress every 1 sec
RcppThread::ProgressCounter cntr(20, 1, "Test message: ");
RcppThread::parallelFor(0, 20, [&](int i) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
cntr++;
});
}

// [[Rcpp::export]]
void
testProgressBar()
Expand Down
6 changes: 5 additions & 1 deletion tests/testthat/tests.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ if (RcppThread:::hasAtomicSupport()) {
# ------------------------------------------------------
context("Progress tracking")
test_that("ProgressCounter works", {
expect_output(testProgressCounter(), "100% \\(done\\)")
expect_output(testProgressCounter(), "Computing: 100% \\(done\\)")
})

test_that("ProgressCounter works", {
expect_output(testProgressCounter2(), "Test message: 100% \\(done\\)")
})

test_that("ProgressBar works", {
Expand Down

0 comments on commit 2c33e09

Please sign in to comment.