Model Calibration using Stan

I have been trying to use Stan for model calibration problems. I fit an emulator to the computer model and then try to use Bayesian model calibration. However, I am facing issues while calling an external R/Python function from within the Stan block. I need to invoke the prediction function from the emulator during each iteration of the Monte Carlo setup and this needs a way to call an R function outside the Stan block from within the Stan block. Usually we pass all the necessary data to Stan in the data block, which is not the case here. Does anyone have any idea how to go about this issue ?

Thanks.

Calling external functions in Python or R isn’t possible in the Stan language.

It might be possible to program an interface to Python or R through a C++ interface, because you can add custom C++ headers to Stan & implement functions that way. But I’ve never seen it done.

For example, could I add a C++ header that used pybind11 to call Python code?