Skip to content

Quarto R ojs_define function and OJS in RStudio #1242

Open

Description

@novotny1akub

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?

Activity

  1. jjallaire commented on Jun 29, 2022

    @jjallaire
    Collaborator

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

  2. novotny1akub commented on Jun 29, 2022

    @novotny1akub
    Author

    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
      }
    })
    
  3. cscheid commented on Jun 29, 2022

    @cscheid
    Member

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

    image

  4. self-assigned this
    on Jun 29, 2022
  5. added
    needs-repro
    ojs-engine
    on Jun 29, 2022
  6. novotny1akub commented on Jun 29, 2022

    @novotny1akub
    Author

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

  7. novotny1akub commented on Jun 29, 2022

    @novotny1akub
    Author

    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"})
      ]
    })
    ```
    
  8. cscheid commented on Jun 29, 2022

    @cscheid
    Member

    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

  9. added this to the Future milestone on Jun 29, 2022
  10. 6 remaining items

  11. cscheid commented on Jun 30, 2022

    @cscheid
    Member

    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/

  12. novotny1akub commented on Jun 30, 2022

    @novotny1akub
    Author

    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
    
    
  13. cscheid commented on Jun 30, 2022

    @cscheid
    Member

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

  14. elbert5770 commented on Aug 24, 2022

    @elbert5770

    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.

  15. cscheid commented on Aug 24, 2022

    @cscheid
    Member

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

  16. novotny1akub commented on Aug 24, 2022

    @novotny1akub
    Author

    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.

  17. nvelden commented on Oct 13, 2022

    @nvelden

    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      
    
  18. jjallaire commented on Oct 13, 2022

    @jjallaire
    Collaborator

    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.

  19. mcanouil commented on Jun 12, 2023

    @mcanouil
  20. added
    stale
    on Jun 12, 2023
  21. jjallaire commented on Jun 12, 2023

    @jjallaire
    Collaborator

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

  22. mcanouil commented on Jun 12, 2023

    @mcanouil
    Collaborator

    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.

  23. added
    enhancement
    and removed
    needs-repro
    stale
    on Jun 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementojs-engine

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions