Hi everyone,
I am developing an R package that uses cpp11 to interface C++ functions with R. One of the C++ functions uses JSON for Modern C++ to parse a JSON file, which is a single header file placed in src/include/json.hpp
and then included in the C++ file with #include "include/json.hpp"
.
My issue is that devtools::check()
warns about pragma(s) suppressing important diagnostics:
── R CMD check results ──────────────────── cppheader 0.0.0.9000 ────
Duration: 8.1s
❯ checking pragmas in C/C++ headers and code ... WARNING
File which contains pragma(s) suppressing important diagnostics
‘src/include/json.hpp’
I don't want to mess with json.hpp
. From what I can gather from Section 8 in R Internals, this is controlled by the environment variable _R_CHECK_PRAGMAS_
, which defaults to FALSE
for non-CRAN checks but TRUE
for CRAN checks.
I get the warning even though NOT_CRAN
is true
when testing locally. I also get the warning even when I explicitly set this environment variable to FALSE
, and also set NOT_CRAN
to true
(since it seems to disappear when specifying env_vars
), like so:
devtools::check(
document = TRUE,
env_vars = c(
"_R_CHECK_PRAGMAS_" = "FALSE",
"NOT_CRAN" = "true"
)
)
The environment variables are picked up, but I still get the warning:
══ Checking════════════════════════════════════════════════
Setting env vars:
• _R_CHECK_CRAN_INCOMING_REMOTE_ : FALSE
• _R_CHECK_CRAN_INCOMING_ : FALSE
• _R_CHECK_FORCE_SUGGESTS_ : FALSE
• _R_CHECK_PACKAGES_USED_IGNORE_UNUSED_IMPORTS_: FALSE
• _R_CHECK_PRAGMAS_ : FALSE
• NOT_CRAN : true
...
── R CMD check results ──────────────────── cppheader 0.0.0.9000 ────
Duration: 8.3s
❯ checking pragmas in C/C++ headers and code ... WARNING
File which contains pragma(s) suppressing important diagnostics
‘src/include/json.hpp’
I've tested this in a toy package, here's the essentials:
get_hello.cpp
#include <cpp11.hpp>
#include "include/json.hpp"
#include <fstream>
using json = nlohmann::json;
[[cpp11::register]]
cpp11::writable::list get_hello_(const std::string& json_path) {
using namespace cpp11::literals;
// read json file
std::ifstream file(json_path);
// parse json file
json j;
file >> j;
// extract "hello" element
std::string hello = j["hello"];
// create and return cpp11 list
cpp11::writable::list results = cpp11::writable::list({
"hello"_nm = hello
});
return results;
}
get_hello.R
#' Read "hello" from JSON
#'
#' This function reads the "hello" key from a JSON file.
#'
#' @param json_path Path to the JSON file
#' @return A list containing the value of the "hello" key
#' @export
get_hello <- function(json_path) {
get_hello_(json_path)
}
hello.json
{
"hello": "world"
}
DESCRIPTION
Package: cppheader
Type: Package
Title: A test package for C++ headers
Version: 0.0.0.9000
Maintainer: CRDF (Carl Delfin) <crdf@novonordisk.com>
Author: CRDF (Carl Delfin) <crdf@novonordisk.com>
Description: A test package for external C++ headers.
License: MPL (>= 2.0)
Encoding: UTF-8
RoxygenNote: 7.3.2
Suggests:
testthat (>= 3.0.0),
devtools
Config/testthat/edition: 3
Config/testthat/parallel: TRUE
LinkingTo:
cpp11
And output works as expected:
> get_hello("inst/extdata/hello.json")
$hello
[1] "world"
So far the only workaround seems to be to allow warnings, which is not ideal. Am I missing something obvious here? Is there some other environment variable that controls this?
Thanks!