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

**URL:** <https://discourse.mc-stan.org/t/strange-error-bin-sh-cmdstan-command-not-found-when-using-cmdstanr-in-rmarkdown/32625>\
**Category:** Modeling\
**Created:** [September 1, 2023, 10:48am UTC](https://discourse.mc-stan.org/t/strange-error-bin-sh-cmdstan-command-not-found-when-using-cmdstanr-in-rmarkdown/32625 "2023-09-01T10:48:50Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![jgcolman](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jgcolman/32/836_2.png) [@jgcolman](https://discourse.mc-stan.org/u/jgcolman)\
**Post date:** [September 1, 2023, 10:48am UTC](https://discourse.mc-stan.org/t/strange-error-bin-sh-cmdstan-command-not-found-when-using-cmdstanr-in-rmarkdown/32625/1 "2023-09-01T10:48:50Z")

</div>

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:

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

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

````

```plaintext
library(knitr)
library(cmdstanr)

```

```plaintext
 
cmdstanr::check_cmdstan_toolchain(fix = TRUE)
register_knitr_engine()

```

```plaintext
  
parameters {
  array[2] real y;
}
model {
  y[1] ~ normal(0, 1);
  y[2] ~ double_exponential(0, 2);
} 

```

```plaintext

```

---

<div class="post-metadata">

**Author:** ![andrjohns](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/andrjohns/32/15297_2.png) [@andrjohns](https://discourse.mc-stan.org/u/andrjohns)\
**Post date:** [September 1, 2023, 10:59am UTC](https://discourse.mc-stan.org/t/strange-error-bin-sh-cmdstan-command-not-found-when-using-cmdstanr-in-rmarkdown/32625/2 "2023-09-01T10:59:54Z")

</div>

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:

```r
register_knitr_engine(override = FALSE)

```

See this doc for a bit more background: [R Markdown CmdStan Engine • cmdstanr](https://mc-stan.org/cmdstanr/articles/r-markdown.html#using-rstan-and-cmdstanr-engines-side-by-side)

---

<div class="post-metadata">

**Author:** ![jgcolman](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jgcolman/32/836_2.png) [@jgcolman](https://discourse.mc-stan.org/u/jgcolman)\
**Post date:** [September 1, 2023, 11:35am UTC](https://discourse.mc-stan.org/t/strange-error-bin-sh-cmdstan-command-not-found-when-using-cmdstanr-in-rmarkdown/32625/3 "2023-09-01T11:35:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jgcolman](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jgcolman/32/836_2.png) [@jgcolman](https://discourse.mc-stan.org/u/jgcolman)\
**Post date:** [September 1, 2023, 12:06pm UTC](https://discourse.mc-stan.org/t/strange-error-bin-sh-cmdstan-command-not-found-when-using-cmdstanr-in-rmarkdown/32625/4 "2023-09-01T12:06:31Z")

</div>

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:

```no-highlight

```

library(cmdstanr)  
check\_cmdstan\_toolchain(fix = TRUE, quiet = TRUE)

register\_knitr\_engine()

```no-highlight

```

> 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.

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [September 1, 2023, 4:13pm UTC](https://discourse.mc-stan.org/t/strange-error-bin-sh-cmdstan-command-not-found-when-using-cmdstanr-in-rmarkdown/32625/5 "2023-09-01T16:13:00Z")

</div>

@jgcolman Any interest in submitting some edits that would address the confusion? The file is located at [https://github.com/stan-dev/cmdstanr/blob/master/vignettes/r-markdown.Rmd](https://github.com/stan-dev/cmdstanr/blob/master/vignettes/r-markdown.Rmd). If not, no worries at all, we can definitely do it, but we always love to get new contributors!

---

<div class="post-metadata">

**Author:** ![jgcolman](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jgcolman/32/836_2.png) [@jgcolman](https://discourse.mc-stan.org/u/jgcolman)\
**Post date:** [September 1, 2023, 8:35pm UTC](https://discourse.mc-stan.org/t/strange-error-bin-sh-cmdstan-command-not-found-when-using-cmdstanr-in-rmarkdown/32625/6 "2023-09-01T20:35:09Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [September 1, 2023, 9:38pm UTC](https://discourse.mc-stan.org/t/strange-error-bin-sh-cmdstan-command-not-found-when-using-cmdstanr-in-rmarkdown/32625/7 "2023-09-01T21:38:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jgcolman](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jgcolman/32/836_2.png) [@jgcolman](https://discourse.mc-stan.org/u/jgcolman)\
**Post date:** [September 19, 2023, 9:06am UTC](https://discourse.mc-stan.org/t/strange-error-bin-sh-cmdstan-command-not-found-when-using-cmdstanr-in-rmarkdown/32625/8 "2023-09-19T09:06:09Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [September 19, 2023, 3:36pm UTC](https://discourse.mc-stan.org/t/strange-error-bin-sh-cmdstan-command-not-found-when-using-cmdstanr-in-rmarkdown/32625/9 "2023-09-19T15:36:04Z")

</div>

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 [https://github.com/stan-dev/cmdstanr/blob/master/vignettes/r-markdown.Rmd](https://github.com/stan-dev/cmdstanr/blob/master/vignettes/r-markdown.Rmd). 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.

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [September 20, 2023, 12:57am UTC](https://discourse.mc-stan.org/t/strange-error-bin-sh-cmdstan-command-not-found-when-using-cmdstanr-in-rmarkdown/32625/10 "2023-09-20T00:57:28Z")

</div>

> [@jonah](#):
>
> That process will let you submit a “pull request”

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.

---

<div class="post-metadata">

**Author:** ![jgcolman](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jgcolman/32/836_2.png) [@jgcolman](https://discourse.mc-stan.org/u/jgcolman)\
**Post date:** [September 20, 2023, 9:06am UTC](https://discourse.mc-stan.org/t/strange-error-bin-sh-cmdstan-command-not-found-when-using-cmdstanr-in-rmarkdown/32625/11 "2023-09-20T09:06:39Z")

</div>

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:

````plaintext
```{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:

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

````

````plaintext
```{r}
model$sample()
```

````

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

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [September 20, 2023, 2:57pm UTC](https://discourse.mc-stan.org/t/strange-error-bin-sh-cmdstan-command-not-found-when-using-cmdstanr-in-rmarkdown/32625/12 "2023-09-20T14:57:09Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![jgcolman](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jgcolman/32/836_2.png) [@jgcolman](https://discourse.mc-stan.org/u/jgcolman)\
**Post date:** [September 20, 2023, 2:59pm UTC](https://discourse.mc-stan.org/t/strange-error-bin-sh-cmdstan-command-not-found-when-using-cmdstanr-in-rmarkdown/32625/13 "2023-09-20T14:59:16Z")

</div>

OK thanks. That is very clear.

---

<div class="post-metadata">

**Author:** ![jgcolman](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jgcolman/32/836_2.png) [@jgcolman](https://discourse.mc-stan.org/u/jgcolman)\
**Post date:** [September 21, 2023, 12:48pm UTC](https://discourse.mc-stan.org/t/strange-error-bin-sh-cmdstan-command-not-found-when-using-cmdstanr-in-rmarkdown/32625/14 "2023-09-21T12:48:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [September 21, 2023, 2:50pm UTC](https://discourse.mc-stan.org/t/strange-error-bin-sh-cmdstan-command-not-found-when-using-cmdstanr-in-rmarkdown/32625/15 "2023-09-21T14:50:52Z")

</div>

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](https://github.com/stan-dev/cmdstanr/pulls). 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.

---

<div class="post-metadata">

**Author:** ![jgcolman](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jgcolman/32/836_2.png) [@jgcolman](https://discourse.mc-stan.org/u/jgcolman)\
**Post date:** [September 21, 2023, 3:06pm UTC](https://discourse.mc-stan.org/t/strange-error-bin-sh-cmdstan-command-not-found-when-using-cmdstanr-in-rmarkdown/32625/16 "2023-09-21T15:06:42Z")

</div>

It did indeed go to a funny place: [https://github.com/jgcolman/cmdstanr](https://github.com/jgcolman/cmdstanr). Can you work with it from there or do I need to do something myself?

---

<div class="post-metadata">

**Author:** ![jgcolman](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jgcolman/32/836_2.png) [@jgcolman](https://discourse.mc-stan.org/u/jgcolman)\
**Post date:** [September 21, 2023, 3:10pm UTC](https://discourse.mc-stan.org/t/strange-error-bin-sh-cmdstan-command-not-found-when-using-cmdstanr-in-rmarkdown/32625/17 "2023-09-21T15:10:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jgcolman](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jgcolman/32/836_2.png) [@jgcolman](https://discourse.mc-stan.org/u/jgcolman)\
**Post date:** [September 21, 2023, 3:13pm UTC](https://discourse.mc-stan.org/t/strange-error-bin-sh-cmdstan-command-not-found-when-using-cmdstanr-in-rmarkdown/32625/18 "2023-09-21T15:13:53Z")

</div>

I can see it at [https://github.com/stan-dev/cmdstanr/compare/master…jgcolman:cmdstanr:patch-1](https://github.com/stan-dev/cmdstanr/compare/master...jgcolman:cmdstanr:patch-1)

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [September 21, 2023, 3:17pm UTC](https://discourse.mc-stan.org/t/strange-error-bin-sh-cmdstan-command-not-found-when-using-cmdstanr-in-rmarkdown/32625/19 "2023-09-21T15:17:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jonah](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jonah/32/200_2.png) [@jonah](https://discourse.mc-stan.org/u/jonah)\
**Post date:** [September 21, 2023, 3:18pm UTC](https://discourse.mc-stan.org/t/strange-error-bin-sh-cmdstan-command-not-found-when-using-cmdstanr-in-rmarkdown/32625/20 "2023-09-21T15:18:11Z")

</div>

> [@jonah](#):
>
> this url

By that I meant [Comparing stan-dev:master...jgcolman:patch-1 · stan-dev/cmdstanr · GitHub](https://github.com/stan-dev/cmdstanr/compare/master%E2%80%A6jgcolman:cmdstanr:patch-1)

[Next page](https://discourse.mc-stan.org/t/strange-error-bin-sh-cmdstan-command-not-found-when-using-cmdstanr-in-rmarkdown/32625.md?page=2)
