Search code examples
rknitrneovimrstan

How to prevent opening /tmp files with knitr using nvim-r and rstan


I'm using R in neovim with nvim-r to knit Rmd documents using the \kh or \kp commands. The problem is when I knit an Rmd that fits an Rstan model, it prints the output of the Rstan sampling() call including the progress of the chains in a separate, temporary file at this location:

file:///tmp/RtmpMmXreV/file27067eb3452f_StanProgress.html

that contains this output:

Click the Refresh button to see progress of the chains starting worker pid=17045 on localhost:11515 at 14:31:54.447 starting worker pid=17070 on localhost:11515 at 14:31:54.670

and it opens that html file in my browser. The result is that my browser keeps popping open every time a new model starts sampling. This only happens when I parallelize the chains using options(mc.cores = parallel::detectCores()). The html files do not pop open without that command.

Is it possible to prevent nvim-r from opening these temporary html files or is it possible to silence the chain output from Rstan?

Minimal example:

library(rstan)

options(mc.cores = parallel::detectCores())

data(DNase)

model <- stan_model(model_code = "
data {
    int n;
    vector[n] conc;
    vector[n] density;
}
parameters {
    real b0;
    real b1;
    real<lower=0> sigma;
}
model {
    conc ~ normal(b0 + b1 * (density), sigma);
    b0 ~ normal(0, 10);
    b1 ~ normal(0, 10);
    sigma ~ normal(0, 10);
}")

fit <- sampling(model, data = list(n = nrow(DNase),
                       conc = DNase$conc, 
                       density = DNase$density))

EDIT: I've tried adding results="hide" to the chunk header and refresh = 0 to the sampling() call based on this answer to no avail. refresh = 0 does succeed in removing the starting worker... part of the message, but it still opens an html file that says Click the Refresh button to see progress of the chains.


Solution

  • Adding open_progress = FALSE to sampling() prevents stan from saving the progress of the chains to a log file. The default, open_progress = TRUE only takes effect when cores > 1. From the rstan reference. Example:

    sampling(model, open_progress = FALSE)