Using python and R together in Rmarkdown

I am trying to use RMarkdown to have python and R integrated together in one document.
The objects that I created in R are usable in python, and vice versa, when I use the RMarkdown in Rstudio. But when I knit it to PDF, following behaviour is observed:

Python code:

x = ‘hello’

R code:

py$x 

If I run this without knitting, it returns the ‘hello’ in the console. But when I knit it to PDF, I can see the output to be ##NULL

Can anyone please help me with this?

Did you try to use quarto instead of RMarkdown for multilanguage projects? Quarto should be somewhat more versatile if you incorporate Python as well as R in your documents.

1 Like

Do you manually set your python environment using reticulate? It would make it easier to help you if you can provide a complete minimal reprex.

1 Like

Thanks for your reply.
I’m kinda new to this. Can you please tell me what to you mean by provide ‘complete reprex’?
So I’ll be able to post it.

If you've never heard of a reprex before, you might want to start by reading this FAQ:

In the context of an Rmd document, you should provide a minimal document that incluyes the necessary code chunks to reproduce your specific issue.

1 Like

Thank you for the help.
As I sat down to create a minimal Reprex for you, the problem got solved.

I was unnecessarily putting extra arguments to the r setup.

Here's how r-setup looks like when used stock:

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

What I was doing:

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

It is highly likely that these settings interfered with each other creating erroneous results.
The reason I modified the settings in the first place was that I wasn't able to call R objects in python (exactly the opposite of my current problem), for which some answers online suggested that setting eval = TRUE and cache = TRUE can do the job.

However, now everything seems to work just fine even with the stock settings.

Thank you again!

1 Like

This happens very often, that is why asking programming related questions with a reprex is such a good idea.

I'm glad you found a solution to your problem. Now to close the loop, if your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.