我试图根据某些输入连续地执行一种类型的渲染(renderPlot
)或另一种(renderText
)。这就是我的尝试:
---
title: "Citation Extraction"
output:
flexdashboard::flex_dashboard:
vertical_layout: scroll
orientation: rows
social: menu
source_code: embed
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
```
Sidebar {.sidebar}
=====================================
```{r}
textInput("txt", "What's up?:")
```
Page 1
=====================================
### Chart A
```{r}
urtxt <- reactive({input$txt})
if (nchar(urtxt()) > 20){
renderPlot({plot(1:10, 1:10)})
} else {
renderPrint({
urtxt()
})
}
```
但它说:
所以我尝试在条件周围添加一个反应,导致返回函数reactive
返回。
reactive({
if (nchar(urtxt()) > 20){
renderPlot({plot(1:10, 1:10)})
} else {
renderPrint({
urtxt()
})
}
})
我如何拥有条件反应逻辑?
答案 0 :(得分:10)
要获得不同类型的输出,具体取决于您可以执行的输入字符串的长度:
1)创建动态输出uiOutput
,
2)在被动环境renderUI
中,根据输入,选择输出类型。
3)渲染输出
---
title: "Citation Extraction"
output:
flexdashboard::flex_dashboard:
vertical_layout: scroll
orientation: rows
social: menu
source_code: embed
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
```
Sidebar {.sidebar}
=====================================
```{r, echo = F}
textInput("txt", "What's up?:", value = "")
```
Page 1
=====================================
### Chart A
```{r, echo = F}
uiOutput("dynamic")
output$dynamic <- renderUI({
if (nchar(input$txt) > 20) plotOutput("plot")
else textOutput("text")
})
output$plot <- renderPlot({ plot(1:10, 1:10) })
output$text <- renderText({ input$txt })
```