gpt4 book ai didi

在 Shiny 中重复无功输出

转载 作者:行者123 更新时间:2023-12-05 04:12:37 25 4
gpt4 key购买 nike

我想在 R Shiny 应用程序的多个位置显示相同的响应式文本输出。这是一个简单的示例(在单个 R markdown 文档中):

---
output: html_document
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo=FALSE)
```

```{r}
output$text1 <- renderText({
paste("Selected letter:", input$var)
})

fluidPage(
fluidRow(
selectInput("var", label="Choose a letter", choices=letters[1:4], selected="b")
),
fluidRow(
textOutput("text1")
),
fluidRow(
textOutput("text1")
)
)
```

如果我删除中间的 fluidRow(textOutput("text1")) 并且应用显示初始文本Selected letter:/strong> 和 react 输入 b 一次。但是,如果我保留那段代码,它不会像我预期的那样两次显示相同的文本。相反,该应用仅显示一次初始文本Selected letter:,而响应式输入b根本不显示。


为了完整起见,我添加了提供解决方法的示例代码(针对第二 block )。

n <- 2
for(i in seq(n)) {
output[[paste0("text",i )]] <- renderText(paste("Selected letter:", input$var))
}

fluidPage(
fluidRow(
selectInput("var", label = "Choose a letter",
choices = letters[1:4], selected = "b")
),
fluidRow(
textOutput("text1")
),
fluidRow(
textOutput("text2")
)
)

最佳答案

Shiny 的输入/输出必须有唯一的 ID。引自 http://shiny.rstudio.com/articles/modules.html

Input and output IDs in Shiny apps share a global namespace, meaning, each ID must be unique across the entire app. If you’re using functions to generate UI, and those functions generate inputs and outputs, then you need to ensure that none of the IDs collide.

Shiny 的模块和命名空间提供了一种组织代码和避免名称冲突的方法。

关于在 Shiny 中重复无功输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40141735/

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