有条件的反应逻辑闪亮的flexdashboard

时间:2016-04-22 19:14:19

标签: r shiny flexdashboard

我试图根据某些输入连续地执行一种类型的渲染(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()   
    })
}
```

但它说:

enter image description here

所以我尝试在条件周围添加一个反应,导致返回函数reactive返回。

reactive({
    if (nchar(urtxt()) > 20){
    renderPlot({plot(1:10, 1:10)})
} else {
    renderPrint({
        urtxt()   
    })
}
})

我如何拥有条件反应逻辑?

1 个答案:

答案 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 })

```