3

I want to define a function in Python, and then use that function in R code. I want to use Rmarkdown for this, so let's say my notebook looks like this:

Let's define a Python function:
```{python}
def concat(s1, s2):
    result = s1+s2
    return result
```

Now use it:
```{r}
big = concat('small', 'tiny')
print(big)
```

But when I try to run code like this, I get error Could not find function "concat". Running the Python chunk also doesn't create an object in my Rstudio variable list.

What is the correct way of defining functions in Python chunks so they can be used by R chunks?

Donentolon
  • 1,363
  • 1
  • 10
  • 17

1 Answers1

3

After some googling, I realized I was missing py$:

Let's define a Python function:
```{python}
def concat(s1, s2):
    result = s1+s2
    return result
```

Now use it:
```{r}
big = py$concat('small', 'tiny')
print(big)
```
Donentolon
  • 1,363
  • 1
  • 10
  • 17