can we have loop in ui.R

I understand we can write loop in server.R, but are we able to have loop in ui.R?

My need for loop in ui.R is this:
say if user selects certain input from pull-down menu with value=1, I want the width of my dygraph to be 12, otherwise with pull-down menu with value=0, I want the width of my dygraph to be 6.

If loop is not able to be achieved in ui.R, how can I achieve the above?

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one for a shiny app

highly suggest you read Mastering_Shiny this is what reactivity is all about

A rule of thumb for you is that ui.R is to position static UI elements. If you need dynamic parts, then you place containers for them with uiOutput and then give them renderUI definitions in your server code

1 Like

do you have an example?

shinyApp(
  ui = fluidPage(
    selectInput("state", "Choose a state:",
      list(`East Coast` = list("NY", "NJ", "CT"),
           `West Coast` = list("WA", "OR", "CA"),
           `Midwest` = list("MN", "WI", "IA"))
    ),
    textOutput("result")
  ),
  server = function(input, output) {
    output$result <- renderText({
      paste("You chose", input$state)
    })
  }
)
}

in Shiny, it's not IF a user selects a value, it's coded for WHEN a user selects input. In normal coding you think linearly, but in Shiny, code is only ran WHEN it is needed

https://shiny.rstudio.com/gallery/dynamic-ui.html

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.