quarto-dev / quarto-cli

Open-source scientific and technical publishing system built on Pandoc.
https://quarto.org
Other
3.62k stars 296 forks source link

Quarto R ojs_define function and OJS in RStudio #1242

Open novotny1akub opened 2 years ago

novotny1akub commented 2 years ago

I tried to use Quarto to do data preprocessing in R and then make the created data available in OJS using RStudio, but it seems to me that the ojs_define function is not available. This article suggests that it might be possible, but it does not seem to be working for me.

{r}
#| output: false
ojs_define(mtcars = mtcars)

In addition, it seems that using ojs from RStudio does not seem to be possible either. For example, following results in an error:

{ojs}
viewof bill_length_min = Inputs.range(
  [32, 50], 
  {value: 35, step: 1, label: "Bill length (min):"}
)

Is there a way to do what I tried above or will it be possible eventually?

jjallaire commented 2 years ago

ojs doesn’t currently work interactively within RStudio (you have to do a complete render). This will be improved in the future!

novotny1akub commented 2 years ago

Thanks a lot. Using some features of OJS (e.g. Inputs.range) is working with the complete render now, but when I try to define data in R, and then use it with OJS, it gives me OJS Runtime Error invalid module. In fact even with OJS defined data, it does not seem to be working. The following code gives OJS Runtime Error invalid module.

d3 = require('d3')

contributors = await d3.json(
  "https://api.github.com/repos/pandas-dev/pandas/stats/contributors"
)

commits = contributors.map(contributor => {
  const author = contributor.author;
  return {
    name: author.login,
    title: author.login,
    group: author.type,
    value: contributor.total
  }
})
cscheid commented 2 years ago

I can't reproduce this locally on the latest Quarto version. Can you make sure you're running an up-to-date version?

image
novotny1akub commented 2 years ago

After updating my version, it seems to be working. Sorry for the confusion and thanks.

novotny1akub commented 2 years ago

I am sorry, but I have to reopen the issue. Below is a reprex. I can successfully define df in R. I can use the df in OJS, but I can't use the Plot module in RStudio. Is there a way to do that?

---
title: "OJS"
format: html
editor: visual
---

```{r}
df <- data.frame(
  year = 1961:1990,
  max = runif(30, -10, 20) |> cumsum()
  )

ojs_define(df = df)
```

```{ojs}
Inputs.table(transpose(df), { sort: "year", reverse: true })
```

```{ojs}
Plot.plot({
  marks: [
    Plot.line(transpose(df), {x: "year", y: "max"})
  ]
})
```
cscheid commented 2 years ago

Unfortunately, like jjallaire said, OJS plots in the "Plots" tab are not currently supported in RStudio.

I will note, though, that you can still render the entire document to get this:

image
novotny1akub commented 2 years ago

I can't render the plot when rendering the whole document. Here is what I see in the Viewer tab.

image

cscheid commented 2 years ago

My guess is you are likely running an older version of RStudio. Try using one of the dailies, perhaps?

novotny1akub commented 2 years ago

I tried to use the preview version of RStudio, but it does not seem to be working. Please note that the version that I am using, which is working, is the most recent one (non-preview).

image
novotny1akub commented 2 years ago

I noticed something that seems slightly odd to me - the plot does not display in RStudio's Viewer panel, but when I click on the "Show in new window" button (a part of the Viewer panel), it opens a browser where the plot is displayed with no issues. In addition, I have done some experiments with d3, and it works with no problems even i RStudio Viewer panel.

cscheid commented 2 years ago

Ok, I see the issue. When using OJS, it's likely that you will run into modules that use modern JS syntax (such as the nullish coalescing operator ??). These are unfortunately not available in the QtWebEngine version of RStudio, which uses a quite-old version of Chromium.

Can you try using the Electron dailies instead? They're at the rightmost column of https://dailies.rstudio.com/

novotny1akub commented 2 years ago

Thanks for the explanation, now I get it. I am afraid neither the Electron version is working.

R Session Startup Failure Report

RStudio Version

RStudio 2022.07.0+547 "Spotted Wakerobin " (ec7dc45b, 2022-06-30) for Windows

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) RStudio/2022.07.0+547 Chrome/102.0.5005.63 Electron/19.0.4 Safari/537.36

Error message

[No error available]

Process Output

The R session exited with code 1.

Error output:

[17564:19308:20220630,235631.501:ERROR crashpad_client_win.cc:491] CreateProcess: The system cannot find the file specified. (2)

Standard output:

[No output emitted]

Logs

C:/Users/novot/AppData/Local/RStudio/log/rsession-novot.log

2022-06-30T21:56:31.502192Z [rsession-novot] ERROR system error 22 (The device does not recognize the command); OCCURRED AT class rstudio::core::Error __cdecl rstudio::core::crash_handler::initialize(enum rstudio::core::crash_handler::ProgramMode) src/cpp/core/CrashHandler.cpp:358; LOGGED FROM: int __cdecl main(int,char *const []) C:\Users\jenkins\workspace\ide\open-source-pipeline\spotted-wakerobin\src\cpp\session\SessionMain.cpp:2007
2022-06-30T21:56:31.507184Z [rsession-novot] ERROR system error 22 (The device does not recognize the command); OCCURRED AT class rstudio::core::Error __cdecl rstudio::core::crash_handler::initialize(enum rstudio::core::crash_handler::ProgramMode) src/cpp/core/CrashHandler.cpp:358; LOGGED FROM: int __cdecl main(int,char *const []) C:\Users\jenkins\workspace\ide\open-source-pipeline\spotted-wakerobin\src\cpp\session\SessionMain.cpp:2057
2022-06-30T21:56:31.507184Z [rsession-novot] ERROR system error 22 (The device does not recognize the command); OCCURRED AT class rstudio::core::Error __cdecl rstudio::core::crash_handler::initialize(enum rstudio::core::crash_handler::ProgramMode) src/cpp/core/CrashHandler.cpp:358; LOGGED FROM: int __cdecl main(int,char *const []) C:\Users\jenkins\workspace\ide\open-source-pipeline\spotted-wakerobin\src\cpp\session\SessionMain.cpp:2057
cscheid commented 2 years ago

I've contacted the IDE devs and will report back.

elbert5770 commented 1 year ago

Yes, I get the exact same behavior with the Quarto penguins example, almost two months later. RStudio 2022.07.1 Build 554, Windows 11. The issue is with the reactive plot in the Viewer with a rendered document. As noted above, it works fine in a browser (Microsoft Edge) after "Show in new window". A fresh install of RStudio on Linux gives the same error as on Windows. However, the viewer in Visual Studio Code works fine with both jupyter and r engines.

A reactive table works perfectly in the RStudio Viewer , so the problem is localized to the reactive plot in RStudio Viewer.

Thus, the penguins example in the Quarto docs will still fail for many users due to this issue. Another problem that is noted in a separate previous issue is that the example uses an altered version of the penguins.csv, but there is no link to download the custom file. This still has not been resolved, and the Quarto docs haven't been updated to reflect these known issues.

cscheid commented 1 year ago

@elbert5770 I'm sorry you hit this issue too. This is a combination of Observable JS source (which we don't control) and RStudio IDE (which has a different release schedule), and as a result it's outside the control of the quarto project. Please report this as an RStudio IDE bug at https://github.com/rstudio/rstudio.

novotny1akub commented 1 year ago

Can Quarto users somehow recommend to the Quarto team what to add to the documentation? Also, I fell like it would be best to close this issue and open a new one since my initial description is chaotic, which may confuse other users that encounter the issues mentioned above.

nvelden commented 1 year ago

I have the same problem. I downloaded the latest version of Rstudio, Quarto and R but I also get the Error in ojs_define(x) : could not find function "ojs_define". Same error occurs in VS code.

---
title: "Untitled"
format: html
editor: visual
---

```{r}
#| echo: false
x = 2 * 2
ojs_define(x)
>Error in ojs_define(x) : could not find function "ojs_define"`

Session.info:

R version 4.2.1 (2022-06-23) Platform: x86_64-apple-darwin17.0 (64-bit) Running under: macOS Monterey 12.5.1

Matrix products: default LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib

locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages: [1] stats graphics grDevices utils datasets methods base

loaded via a namespace (and not attached): [1] compiler_4.2.1 tools_4.2.1 rstudioapi_0.14 knitr_1.40 xfun_0.33

jjallaire commented 1 year ago

That function won't work if you execute it as part of just a cell (as it's only available during a render). This is a limitation we definitely hope to overcome at some point but for now that's the deal.

mcanouil commented 1 year ago

Thank you for using Quarto and reporting an issue!

Unfortunately, this issue is now considered stale because it has been opened since 14 days without providing a "working" reproducible example to help us investigate. If you are still facing the issue, please review the "Bug Reports" guide on how to provide a fully reproducible example as a self-contained Quarto document or a link to a Git repository. Without a reproducible example, it is unlikely that the issue will be addressed and thus will be closed.

You can share a Quarto document using the following syntax, i.e., using more backticks than you have in your document (usually four ````).

````qmd
---
title: "Reproducible Quarto Document"
format: html
---

This is a reproducible Quarto document using `format: html`.
It is written in Markdown and contains embedded R code.
When you run the code, it will produce a plot.

```{r}
plot(cars)

The end.

Remove stale / needs-repro labels or this will be closed in 14 days.

jjallaire commented 1 year ago

@mcanouil this is also not stale as it's a feature request. Let's re-open and remove the stale tag

mcanouil commented 1 year ago

Agreed! This issue should not have the "needs-repro", thus the "stale" label. Removing both now. (Note, that I inadvertently added the stale message/label myself when reviewing all "needs-repro" issues.)

Also adding the "enhancement" label which is the proper label for feature requests.