Expose_stan_functions not working after updating rstan and R

I have recently updated rstan (version 2.26.4) and R (version 4.1.2). Running expose_stan_functions now throws the following error:
Error: $ operator is invalid for atomic vectors

An example:

model_code <-
  '
  functions {
    real standard_normal_rng() {
      return normal_rng(0,1);
   }
  }
'
expose_stan_functions(stanc(model_code = model_code))
# Error: $ operator is invalid for atomic vectors

Thanks

1 Like

The error comes from the underlying call to rstan::expose_stan_functions which brms::expose_functions is a wrapper for. It can be caused by a few things, but the main culprits are usually a version of withr > 2.2.0 or an out of date installation of RcppParallel.

First try:

remotes::install_version("withr", version="2.2.0")

And see if that fixes the issue. If it doesn’t try reinstalling the latest development version of RcppParallel.

remotes::install_github("RcppCore/RcppParallel")

You may need to reinstall rstan and StanHeaders after RcppParallel.

Hi @ajnafa

Many thanks for the reply. Unfortunately the error persists even after installing withr version 2.2.0 followed by updating RcppParallel and reinstalling rstan and StanHeaders

remotes::install_version("withr", version="2.2.0")

remotes::install_github("RcppCore/RcppParallel")

remove.packages(c("rstan", "StanHeaders"))
install.packages("rstan", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))

Package versions:
withr : 2.2.0
RcppParallel: 5.1.4.9000
rstan: 2.26.4
StanHeaders: 2.26.4

1 Like

I believe the culprit here might be the new StanHeaders/rstan itself.

@hsbadr I can replicate the error on a fresh install of 2.26.4 using the example code:

model_code <-
    '
  functions {
    real standard_normal_rng() {
      return normal_rng(0,1);
   }
  }
'
expose_stan_functions(stanc(model_code = model_code))

Could you test this?

No change, unfortunately

1 Like

v2.26.5 is ready for testing.

remove.packages(c("rstan", "StanHeaders"))
install.packages("rstan", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))

Same error is present, but I believe it’s separate from the TBB. I stepped through the expose_stan_functions source and it turns out that the Rcpp::sourceCpp call was returning an error:

In file included from C:/Users/Andrew Johnson/Documents/R/win-library/4.1/StanHeaders/include/src/stan/model/model_header.hpp:7,
                 from file3bfc5caa636.cpp:14:
C:/Users/Andrew Johnson/Documents/R/win-library/4.1/StanHeaders/include/src/stan/io/dump.hpp: In member function 'virtual void stan::io::dump::validate_dims(const string&, const string&, const string&, const std::vector<long long unsigned int>&) const':
C:/Users/Andrew Johnson/Documents/R/win-library/4.1/StanHeaders/include/src/stan/io/dump.hpp:778:15: error: 'validate_dims' is not a member of 'stan::io'
     stan::io::validate_dims(*this, stage, name, base_type, dims_declared);
               ^~~~~~~~~~~~~
C:/Users/Andrew Johnson/Documents/R/win-library/4.1/StanHeaders/include/src/stan/io/dump.hpp:778:15: note: suggested alternative: 'validate_zero_buf'
     stan::io::validate_dims(*this, stage, name, base_type, dims_declared);
               ^~~~~~~~~~~~~
               validate_zero_buf

Even though USE_STANC3 was defined. Because the call is wrapped in a try statement in expose_stan_functions it was failing silently. It looks like the generated c++ just needed to have the rstan includes added like with the main stan_model calls:

model_cppcode <- paste("#ifndef MODELS_HPP",
                       "#define MODELS_HPP",
                       "#define STAN__SERVICES__COMMAND_HPP",
                       "#include <rstan/rstaninc.hpp>",
                       model_cppcode,
                       "#endif",
                       sep = '\n')

I’ll open a quick PR now

3 Likes

I see. Thanks!

To confirm that this is related to TBB or not, could you test v2.26.6?

remove.packages(c("rstan", "StanHeaders"))
install.packages("StanHeaders", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
install.packages("rstan", \repos = c("https://mc-stan.org/r-packages/", getOption("repos")))

@andrjohns Have you tested this? I’d liked to confirm that this is (not) related to static TBB? Thanks!

This looks like a USE_STANC3 issue where the headers are incompatible (v2.21 vs v2.26). I have no problem running expose_stan_functions() on my system. The error message can be improved, but any failed compilation will lead to this error right now.

I am getting the same error on Mac OS 12.0.1. I have removed/reinstalled RStan according to the above instructions, but to no avail. Has there been a solution for this issue?

No solution yet. I tried after reinstalling v2.26.6 rstan and StanHeaders but no success.

You can test the experimental version

remove.packages(c("rstan", "StanHeaders"))
remotes::install_git("https://github.com/stan-dev/rstan", subdir = "StanHeaders", ref = "experimental") 
remotes::install_git("https://github.com/stan-dev/rstan", subdir = "rstan/rstan", ref = "experimental")

It worked for me running Mac OS 12.0.1

> version
               _                           
platform       aarch64-apple-darwin20      
arch           aarch64                     
os             darwin20                    
system         aarch64, darwin20           
status                                     
major          4                           
minor          1.2                         
year           2021                        
month          11                          
day            01                          
svn rev        81115                       
language       R                           
version.string R version 4.1.2 (2021-11-01)
nickname       Bird Hippie   

Thanks @spinkney

I tried installing the experimental version but could only install the StanHeaders. Installation of rstan fails with the following error:

Warning in i.p(...) :
  installation of package ‘C:/Users/Sandhu/AppData/Local/Temp/RtmpaKrc6K/file304c4e66448d/rstan_2.28.2.9000.tar.gz’ had non-zero exit status

Install the binary packages for Windows from the latest artifacts (currently, at Update stan submodule · stan-dev/rstan@fbc0eef · GitHub): assuming that you’re on R-release, scroll down to the packages, download R-release_windows-latest.zip, and unzip it. Then, install the packages (from your current working directory) using:

install.packages("StanHeaders_2.28.2.9000.zip", repos = NULL)
install.packages("rstan_2.28.2.9000.zip", repos = NULL)

Note that this’s the bleeding-edge branch. It isn’t available in our repos because v2.26 is the planned update, not v2.28+ yet.

Thanks @hsbadr

I could see the artifacts including R-release_windows-latest but there is no option to download it. I am using the Google Chrome.

You may need to log into GitHub and just click on the artifact you want to download.

Many thanks @hsbadr

I have successfully installed StanHeaders and rstan (v 2.28.2.9000) and can confirm that expose_stan_functions now works fine. I have tested it using the following example.

model_code <-
  '
  functions {
    real standard_normal_rng() {
      return normal_rng(0,1);
   }
  }
'
expose_stan_functions(stanc(model_code = model_code))

standard_normal_rng()

Thanks once again