Ale*_*xis 6 r r-markdown shiny reticulate
我有这个 Rmarkdown,带有一个 python 函数:
---
title: "An hybrid experiment"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(reticulate)
```
```{r}
selectInput("selector",label = "Selector",
choices = list("1" = 1, "2" = 2, "3" = 3),
selected = 1)
```
```{python}
def addTwo(number):
return number + 2
```
Run Code Online (Sandbox Code Playgroud)
我尝试addTwo在响应式上下文中使用该函数,所以我尝试了这个:
```{r}
renderText({
the_number <- py$addTwo(input$selector)
paste0("The text is: ",the_number)
})
```
Run Code Online (Sandbox Code Playgroud)
但我收到了这个错误:
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
Detailed traceback:
File "<string>", line 2, in addTwo
Run Code Online (Sandbox Code Playgroud)
我一定是做错了什么,请你指导我解决这个问题吗?
这reticulate部分很好,错误实际上来自shiny.
以下是关于 的一些重要细节input$selector:
selectInputas.numericreq(input$selector)将避免错误renderText这有效:
---
title: "An hybrid experiment"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(reticulate)
```
```{python}
def addTwo(number):
return number + 2
```
```{r}
selectInput("selector",label = "Selector",
choices = list("choose 1" = 1, "choose 2" = 2, "choose 3" = 3),
selected = 1)
renderText({
the_number <- py$addTwo(as.numeric(input$selector))
paste0("The text is: ",the_number)
})
```
Run Code Online (Sandbox Code Playgroud)