RCall.jl icon indicating copy to clipboard operation
RCall.jl copied to clipboard

problems with complex/non-standard formulas

Open bbolker opened this issue 4 months ago • 3 comments

One-sided formulas, and formulas that deviate from the existing standard for random-effect terms (i.e. containing terms such as cs(f|g), to denote a compound-symmetric covariance matrix), seem to break stuff. (I set the breaking chunks to eval:false so I could render the document ...)

This is with RCall v0.14.9

---
author: Ben Bolker
title: "formula parsing etc."
engine: julia
julia:
  env: [ "LD_LIBRARY_PATH=/usr/local/lib/R/lib"]
---

```{julia}
using RCall
```

```{r}
#| eval: false
form <-  ~ a + b
```

> LoadError: ArgumentError: malformed expression in formula ~(a + b)

```{r}
#| eval: false
form <- y ~ a + b + cs(f | g)
```

> UndefVarError: `cs` not defined in `RCall`

This one is OK:

```{r}
form <- y ~ a + b + (f | g)
```

quarto check

Quarto 1.7.32
[✓] Checking environment information...
      Quarto cache location: /home/bolker/.cache/quarto
[✓] Checking versions of quarto binary dependencies...
      Pandoc version 3.6.3: OK
      Dart Sass version 1.85.1: OK
      Deno version 1.46.3: OK
      Typst version 0.13.0: OK
[✓] Checking versions of quarto dependencies......OK
[✓] Checking Quarto installation......OK
      Version: 1.7.32
      Path: /opt/quarto/bin

[✓] Checking tools....................OK
      TinyTeX: (not installed)
      Chromium: (not installed)

[✓] Checking LaTeX....................OK
      Using: Installation From Path
      Path: /usr/local/texlive/2024/bin/x86_64-linux
      Version: 2024

[✓] Checking Chrome Headless....................OK
      Using: Chrome found on system
      Path: /usr/bin/chromium-browser
      Source: PATH

[✓] Checking basic markdown render....OK

[✓] Checking Python 3 installation....OK
      Version: 3.12.3
      Path: /usr/bin/python3
      Jupyter: 5.3.2
      Kernels: python3

[✓] Checking Jupyter engine render....OK

[✓] Checking R installation...........OK
      Version: 4.6.0
      Path: /usr/local/lib/R
      LibPaths:
        - /usr/lib/R/site-library
        - /usr/local/lib/R/library
      knitr: 1.50
      rmarkdown: 2.29

[✓] Checking Knitr engine render......OK

bbolker avatar Aug 30 '25 18:08 bbolker

thanks! For my own notes, the MWE without quarto is:

julia> using RCall

julia> rcopy(R"~ a + b")
ERROR: LoadError: ArgumentError: malformed expression in formula ~(a + b)
Stacktrace:
 [1] var"@formula"(__source__::LineNumberNode, __module__::Module, ex::Any)
   @ StatsModels ~/.julia/packages/StatsModels/YNwJ1/src/formula.jl:62
...

julia> rcopy(R"y ~ f(a)")
ERROR: UndefVarError: `f` not defined in `RCall`
Suggestion: check for spelling errors or missing imports.
Stacktrace:
 [1] top-level scope
   @ ~/.julia/packages/RCall/GLHIY/src/convert/formula.jl:41
...

palday avatar Aug 30 '25 20:08 palday

It occurs to me for the quarto case, there might be an easy workaround. RCall doesn't actually have to copy everything evaluated in R over to Julia, so my guess is that the QuartoNotebookRunner is copying the last value for display purposes. In other words, you might be able to dodge this issue by ending the R cells in a safe value such as NULL or, if you want to display things, in an explicit print(form) call since the return value of print is NULL.

palday avatar Sep 02 '25 11:09 palday

Thanks, that makes sense - I'll try that solution (when I get a chance) and let you know how it goes. (After your first answer I thought it was silly of me to have posted in Quarto format, but now that I see the [potential] workaround I'm glad I did.)

bbolker avatar Sep 02 '25 17:09 bbolker