3

I want to display the same reactive text output at multiple locations in an R Shiny app. Here is a simple example (in a single R markdown document):

---
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")
  )
)
```

This code works fine if I delete the middle fluidRow(textOutput("text1")), with the app showing the initial text Selected letter: and the reactive input b once. But, if I leave that piece of code in, it doesn't display the same text twice, as I expected it would. Instead, the app shows the initial text Selected letter: only once and the reactive input b not at all.


For completeness, I am adding example code (for the second chunk) that provides a work around.

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")
  )
)
2
  • 2
    You cannot have two outputs with the same id. Commented Oct 20, 2016 at 0:29
  • Thanks for that information. If you provide that as an answer, I will accept it. Can you point to a place where that is stated explicitly? Commented Oct 20, 2016 at 13:23

1 Answer 1

3

Shiny Inputs/Outputs must have unique IDs. Quote from 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 Modules and Namespaces provide a way to organize your code and avoid name collisions.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.