Strange error " /bin/sh: cmdstan: command not found" when using cmdstanr in RMarkdown

I am trying to write an R Markdown document that includes a Cmdstan object. If I knit the whole document it runs, but if I try to execute it chunk by chunk I get the error message " /bin/sh: cmdstan: command not found" when I execute the chunk containing the Stan code.

I am working in:

  • RStudio Version 2023.06.1+524 (2023.06.1+524)

  • R version 4.3.0 (2023-04-21) " Already Tomorrow"

  • cmdstanr version 0.6.0

I do not understand the error message.

Here is my MRE:

title: "MRE"
author: "Jeremy Colman"
date:  "`r Sys.Date()`"
output: pdf_document

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
cmdstanr::check_cmdstan_toolchain(fix = TRUE)
parameters {
  array[2] real y;
model {
  y[1] ~ normal(0, 1);
  y[2] ~ double_exponential(0, 2);

When you call register_knitr_engine() with no arguments, the default behaviour is to only register cmdstanr for processing stan blocks. If you want to use cmdstanr for cmdstan blocks (like in your notebook), you need to instead call:

register_knitr_engine(override = FALSE)

See this doc for a bit more background: R Markdown CmdStan Engine • cmdstanr


Ah! Many thanks. I misread the first paragraph of the section on using rstan and cmdstanr together as implying that the override statement was needed only if both rstan and cmdstanr were being used.


I’ve just had another look at the document you pointed me to. /what misled me was the following near the beginning of the document:

CmdStanR provides a replacement engine, which can be registered as follows:

check_cmdstan_toolchain(fix = TRUE, quiet = TRUE)


By default, this overrides knitr’s built-in stan engine so that all stan chunks are processed with CmdStanR, not RStan.

It might be worth amending the final sentence to include the point about override.

1 Like

@jgcolman Any interest in submitting some edits that would address the confusion? The file is located at If not, no worries at all, we can definitely do it, but we always love to get new contributors!

1 Like

@jonah I’d be glad to do so. Give me a day or two to work something out.

Are you aware of the two rules about dealing with comments on a draft?

Rule 1: no comment, however daft, should be ignored or dismissed. I have spent many years dealing with authors who do not behave in that way.

Rule 2: no specific drafting change put forward by someone outside the team, however convincing it seems to be, should ever be accepted without the most critical analysis. I have spent many years dealing with authors who have accepted specific drafting that makes a nonsense of something else in the document.

So, you have very wisely observed Rule1. I shall be very happy if you treat my suggestions under Rule 2.


Sounds good, thanks for being willing to help out. I was not aware of those rules specifically, but I like them quite a bit. Thanks for sharing.

And no rush if you need more than a few days, I don’t think we’ll do another release for at least a few weeks (although we could still update the website version of the vignette between releases). In the meantime we now at least have a solution on the forum in case anyone else runs into this issue.

I’d like to amend the document in a way that you can track the changes. I don’t know how to do that using Github: I have only ever amended my own documents on Github and I am not a heavy user of it.
Can you point me to simple instructions?

Since you’re only editing that single file this is a bit simpler than it would be if you were going to update many files (you can do this all from within the web browser).

  • Go to There will be an edit button (pencil icon) at the top right of the file (not top right of the whole browser, but of the part displaying the file). Click on the pencil.
  • That should take you to a page where you can click on a button to “fork” the repository (unless you already have a fork, in which case let me know).
  • After that it should let you edit the file and when you’re done click the green “Commit changes” button followed by the “Propose changes” button that comes up after that.

That process will let you submit a “pull request” proposing your edits and we’ll be able to see and comment on the specific changes you made. Let me know if you run into any trouble when trying that.

At this stage you might see something about running the unit tests, but that will happen automatically when one of us reviews it so don’t worry about that.

Thanks. Now that I have started working on this I have a further query about the text. In the section about using both RStan and CmdStanR an example is given in which the CmdStanR chunk is:

```{cmdstan, output.var="model_obj2"}
// Results in a CmdStanModel object from CmdStanR

and that seems logical to me, but earlier in the document we see presented as an example of the CmdStanR option:

```{stan, output.var="model"}
// Stan model code

Is that right? Why stan and not cmdstan as in the later example?

I think this depends on whether you set override to TRUE (the default) or FALSE when calling register_knitr_engine(). If override=TRUE then that makes stan chunks use CmdStanR instead of RStan (this is what you see earlier in the document). If override=FALSE then stan chunks still use RStan and cmdstan chunks use CmdStanR. It’s rare to need to use RStan and CmdStanR in the same document, but this option is provided for those cases when someone wants to do that (e.g. educational materials showing how to use both interfaces to Stan).

OK thanks. That is very clear.

I have just submitted my proposed redraft. I hope it is clear but I think I might still have become confused about when override=TRUE or override=FALSE is to be used.

Can you share the url to where this is? I think something went wrong because it doesn’t show up at Pull requests · stan-dev/cmdstanr · GitHub. Maybe it created the pull request in your own fork of the repository instead of from your fork to our master branch. I thought my instructions should have handled that ok, but I might have overlooked something (sorry if that’s the case!). I should be able to tell based on the url to where the request was created.

It did indeed go to a funny place: Can you work with it from there or do I need to do something myself?

Sorry, but that material doesn’t seem to be my work at all. I’ll carry on searching.

I can see it at…jgcolman:cmdstanr:patch-1

Ok great. At this url do you see a “Create pull request” button? If so go ahead and click that and then potentially you’ll have to click “Create pull request” again on the next page.

By that I meant Comparing stan-dev:master...jgcolman:patch-1 · stan-dev/cmdstanr · GitHub