gpt4 book ai didi

r - 基于条件 react 逻辑 Shiny 的 flexdashboard

转载 作者:行者123 更新时间:2023-12-04 02:09:26 24 4
gpt4 key购买 nike

我正在尝试根据某些输入连续进行一种渲染( 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返回。
reactive({
if (nchar(urtxt()) > 20){
renderPlot({plot(1:10, 1:10)})
} else {
renderPrint({
urtxt()
})
}
})

我怎样才能有条件 react 逻辑?

最佳答案

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

1) 创建动态输出 uiOutput ,

2) 在 react 环境中 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 })

```

关于r - 基于条件 react 逻辑 Shiny 的 flexdashboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36801666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com