【问题标题】:Conditional reactive logic shiny based flexdashboard基于条件反应逻辑闪亮的 flexdashboard
【发布时间】:2016-08-16 13:08:24
【问题描述】:

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

我怎样才能有条件反应逻辑?

【问题讨论】:

  • 试图理解否决票。似乎有人对我的所有内容都投了反对票,无论问题质量如何。如果有反对的真正原因,请分享,以便我改进。
  • 最近似乎确实有很多匿名投票,但其中大部分是针对毫无头绪的重复,这似乎更加前沿,并且具有从相对较新的引出一个好的答案的额外好处用户。向你们俩致敬。

标签: r shiny flexdashboard


【解决方案1】:

要根据输入字符串的长度获得不同类型的输出,您可以执行以下操作:

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

```

【讨论】:

    猜你喜欢
    • 2021-01-12
    • 2019-08-17
    • 2014-10-22
    • 2023-03-03
    • 2020-03-14
    • 2021-05-25
    • 2017-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多