Rstan_package_skeleton() error


#1

Hi:
I am attempting to place a couple pre-compiled Stan models into a R package. The rstan_package_skeleton() option had a few bugs, so copied and pasted all files into the package working directory. To be clear, I have my .stan files in the the exec folder.

I am now getting this error when running “Build & Reload”:

Error in file(con, “r”) : cannot open the connection:

I also get the following warnings (to 50):
Warning messages:
1: In readLines(file) : line 3 appears to contain an embedded nul
2: In readLines(file) : line 4 appears to contain an embedded nul
3: In readLines(file) : line 5 appears to contain an embedded nul
4: In readLines(file) : line 6 appears to contain an embedded nul
5: In readLines(file) : line 44 appears to contain an embedded nul
6: In readLines(file) : line 547 appears to contain an embedded nul
7: In readLines(file) : line 548 appears to contain an embedded nul
8: In readLines(file) : line 549 appears to contain an embedded nul
9: In readLines(file) : line 550 appears to contain an embedded nul
10: In readLines(file) : line 551 appears to contain an embedded nul
11: In readLines(file) : line 552 appears to contain an embedded nul
12: In readLines(file) : line 553 appears to contain an embedded nul
13: In readLines(file) : line 554 appears to contain an embedded nul
14: In readLines(file) : line 555 appears to contain an embedded nul

Thank you in advance,
Donny


#2

The Build&Reload icon in RStudio doesn’t exactly work in a straighforward fashion with packages such as these. What were the errors you encountered when doing rstantools::rstan_package_skeleton()?


#3

Thank you for the quick response. I just started the package with Rstan_package_skeleton(), and used devtools:document and this worked. I then added my .stan file to the exec folder, and now have the error once again.

error encountered:
Error in file(con, “r”) : cannot open the connection

Here is one of the errors with “rerun wit Debug” (not sure if you want them all):

function (description = “”, open = “”, blocking = TRUE, encoding = getOption(“encoding”),
raw = FALSE, method = getOption(“url.method”, “default”))
{
.Internal(file(description, open, blocking, encoding, method,
raw))
}


#4

Me again. When using the “Build and Reload” I get this unusual error that keeps on running. I have to exit R studio, as the error will not stop.

Warning in readLines(file) :
line 2682 appears to contain an embedded nul
Warning in readLines(file) :
line 2684 apning in readLines(file) :
line 2682 appears to contain an embedded nul
Warning in readLines(file) :
line 2684 ap


#5

Try to build the package once outside RStudio with

R CMD build mypackage/

on the command line.


#6

I spoke with someone, and learned that I cannot precomile the model first. I now have just the text model, but am getting these errors:

  1. Error in is(module, “character”) : object ‘m’ not found
  2. In readLines(file) : incomplete final line found on ‘exec/ridge.stan’

data{
int<lower = 0> N;
int<lower = 0> K;
matrix[N, K] X;

// ridge_prior
real<lower=0> prior_scale; // scale of Gaussian
}

transformed data{
int index[K, K-1];
for(k in 1:K){int displace = 0;
for(j in 1:(K-1)){
if(j == k){displace = 1;}
index[k,j] = j + displace;}}
}

parameters {
vector[K-1] b[K];
vector[K] b_int;
vector<lower = 0> [K] sigma;
}

model{
for(k in 1:K){

// Likelihood
X[,k] ~ normal(b_int[k] + X[,index[k]]*b[k], sigma[k]);

//priors
sigma[k] ~ cauchy(0, 2);
b_int[k] ~ cauchy(0, 2);

//regularizing prior

b[k] ~ normal(0, prior_scale);
}
}


#7

You can (and should) write packages with precompiled Stan models. My guess is that in your Roxygen you don’t have the line

#' @useDynLib mypackage, .registration = TRUE

Or if you are not using Roxygen, then the NAMESPACE file should have the line

useDynLib(mypackage, .registration = TRUE)

It may be faster if you can post a link to your package on GitHub and then I can make a PR to get it up and running initially.


#8

I was under the impression that the rstan skeleton tools provide the necessary boilerplate to compile .stan files into SO/DLLs, and that you can’t just compile an object and stick it into your package tree? Has that changed? Otherwise, wouldn’t you have to precompile each stan model for each OS and OS version? Or are you referring to something else?


#9

Hi:
Thank you for the assistance. I just started package, and here is the github:


#10

I made a PR. To summarize,

  1. You can’t call rstantools::rstan_package_skeleton("anRpackage") and then rename the resulting directory to bnets without having to go back and change a bunch of stuff inside that is tied to the package name.
  2. Read the read-and-delete-me file in the root of bnets before deleting it, particularly the part about the NAMESPACE file and making the cleanup files executable
  3. Don’t try to put dll files into the src/ subdirectory; they have to be created as part of the build process.

#11

Got it thank you! One more question :) I am calling the .stan model inside of a function I created, but not sure what the model is called now. It is saved as ridge.stan, but using ridge within the function does not work.

mod_fit <- sampling(ridge, data = stan_dat,
chains = chains, iter = iter, cores = cores,
control = list(adapt_delta = adapt_delta,
max_treedepth = max_treedepth))


#12

it should be something like

mod_fit <- sampling(stanmodels$ridge, ...)

#13

OK. I tried that and got the following: “model_ridge” not found.

X <- psych::bfi[1:100, 1:5]
fit <- bridge_net(X, prior_scale = 1, chains = 4)

Error in new_CppObject_xp(fields$.module, fields$.pointer, …) :
no valid constructor available for the argument list
In addition: Warning message:
NA values detected and removed
trying deprecated constructor; please alert package maintainer
Show Traceback

Rerun with Debug
Error in checkForRemoteErrors(val) :
4 nodes produced errors; first error: object ‘model_ridge’ not found


#14

It works for me if you call rstan::sampling rather than sampling without importing it. I think you may have unpushed changes that are messing it up.


#15

Hmm. Interesting. I tried it and get the same error.

rstan::sampling(stanmodels$ridge,…

Error in new_CppObject_xp(fields$.module, fields$.pointer, …) :
no valid constructor available for the argument list
trying deprecated constructor; please alert package maintainer
Loading required namespace: rstudioapi
Show Traceback

Rerun with Debug
Error in checkForRemoteErrors(val) :
4 nodes produced errors; first error: object ‘model_ridge’ not found

image


#16

That is Windows stuff. You have to make sure that the right version is installed in the right path to get it to work correctly in parallel for Windows. We can worry about that later. For now, try it with one chain.


#17

Got it! I think it had something to do with parallel. Running with 1 core and many chains works great.

Thank you for the great assistance!


#18

Okay, at least you can keep developing it now. Make sure to uninstall any previous versions of the package in different directories and to start R with a clean session that does not bring in any old objects that are Stan-related.


#19

Almost ;-) I added the final two models, and am now getting a similar error as before. I saw that m is used in a loop, so not sure why it is not found. I pushed the two additional models to github

Updating bnets documentation
Loading bnets
Error in is(module, “character”) : object ‘m’ not found
Calls: suppressPackageStartupMessages … load_all -> run_ns_load_actions -> action -> loadModule -> is
Execution halted

Exited with status 1.


#21

It installs fine for me. When you change or add any of the Stan files, you have to go outside RStudio and do

R CMD INSTALL --preclean bnets

or

R CMD build bnets
R CMD INSTANN bnets_*.tar.gz