[Python] Cannot access R or Python objects in R and Python Quarto Document

Discussão em 'Python' iniciado por Stack, Agosto 17, 2025.

  1. Stack

    Stack Membro Participativo

    I am using Quarto and VSCode to try to run Python and R together. I'd like to be able to access R objects in python or python objects in R. If I render the document below, it works as expected. However, if I am running in interactive mode, i.e., running each chunk / developing the code, I cannot access objects from the other language. It seems they do not share the same environment. Is this possible or am I misunderstanding the abilities of Quarto and reticulate?

    ---
    title: "Untitled"
    format: html
    ---

    ```{r}
    library(reticulate)
    reticulate::use_virtualenv("/Users/xxx/.pyenv/versions/xxx")
    ```


    ```{r}
    x <- 123
    ```


    ```{python}
    y = r.x * 2
    print(f"Python y = {y}")
    ```

    ```{r}
    # Access Python variable from R
    cat("R accessing Python y:", py$y, "\n")

    # Create an R data frame
    df_r <- data.frame(
    a = 1:3,
    b = c("x", "y", "z")
    )
    print(df_r)
    ```

    ```{python}
    # Access R data frame from Python
    import pandas as pd
    df_python = r.df_r
    print("Python accessing R data frame:")
    print(df_python)
    print(f"Type: {type(df_python)}")
    ```


    For reference, I am running Quarto in VS Code.

    Continue reading...

Compartilhe esta Página