While Loop inside Modal funtion

Hi all, Not sure what wrong is going on in my code. Basically I whatever col_name has. Those should be in the pop up modal box. I know we can do it manually writing all the element. But I am trying those in while loop so that what ever col_name has, we need to have those in pop up modal. Refer expected output

library(shiny)

ui <- fluidPage(actionButton("show", "show"))
shinyApp(
  ui,
  server = function(input, output) {
    col_name <- c("A", "B", "C", "D")
    i <- 1
    
    observeEvent(input$show,
                 showModal(modalDialog(title = "Edit",
                                       
                                       while (i < length(col_name)){
                                         fluidRow(column(width = 4,
                                                         print(col_name[i])
                                                         i = i + 1))
                                                                    }
                                       )
                           )
                 )
  }
)

Expected output

image

library(shiny)
library(purrr)
col_name <- c("A", "B", "C", "D")
i <- 1

list_of_rows<- purrr::map(col_name,
          ~fluidRow(column(width = 4,.)))

fluidPage(
  list_of_rows
)

Thanks. I tried something like this since I need inside modal box. But did not get. Can we not do this using while loop?

library(shiny)

ui <- fluidPage(actionButton("show", "show"))
shinyApp(
  ui,
  server = function(input, output) {
    col_name <- c("A", "B", "C", "D")
    i <- 1
    
    observeEvent(input$show,
                 showModal(modalDialog(title = "Edit",
                                       list_of_rows<- purrr::map(col_name,
                                                                 ~fluidRow(column(width = 4,.)))
                                       
                                       fluidPage(
                                         list_of_rows
                                       )
                 )
                 )
    )
  }
)

Got it. thanks. Below is solution of urs

library(shiny)
library(purrr)

ui <- fluidPage(actionButton("show", "show"))
shinyApp(
  ui,
  server = function(input, output) {
    col_name <- c("A", "B", "C", "D")
    i <- 1
    list_of_rows<- purrr::map(col_name,
                                     ~fluidRow(column(width = 4,.)))
    observeEvent(input$show,
                 showModal(modalDialog(title = "Edit",
                                       fluidPage(
                                         list_of_rows
                                       )              
                 )
                 )
    )
  }
)

Hi Nir,

I just changed my code as per my requirement. A small

library(shiny)
library(purrr)

ui <- fluidPage(actionButton("show", "show"))
shinyApp(
  ui,
  server = function(input, output) {
    col_name <- c("A", "B", "C", "DE")
    i <- 1
    list_of_rows<- purrr::map(col_name,
                                     ~fluidRow(column(width = 4,.,textInput("a","",value = "b"),)))
    observeEvent(input$show,
                 showModal(modalDialog(title = "Edit",
                                       fluidPage(
                                         list_of_rows
                                       )              
                 )
                 )
    )
  }
)

It is all working. But if you look carefully, the id = "a" is for all textInputs. Can we not make it dynamic so id is a for A,
b for B,
c for C and
d_e for DE

    list_of_rows<- purrr::map(col_name,
                              ~fluidRow(column(width = 4,.,
                               textInput(tolower(.),"",
                                 value = tolower(.)),)))

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