【问题标题】:while loop under fluidrow in Shiny在闪亮的流体行下循环
【发布时间】:2020-08-30 01:06:39
【问题描述】:

我正在执行一个闪亮的应用程序,其中 Modal 函数中有 while 循环。请参阅下文。因此,单击按钮后的预期输出应该是 4 行(A、B、C、D)的弹出窗口。所以基本上当col_name 改变模式框中的行数时。在这种情况下应该有 4 行,因为我们只有 (A, B, C, D)

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)) {
        print(i)
        fluidRow(
          column(width = 4,
                 col_name[i]
                 i = i + 1
          )
        )
      }

      )))
  })



点击按钮后的预期输出

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    这是一个可行的解决方案,如果您出于某种原因必须在循环中执行此操作

    library(shiny)
    
    ui <- fluidPage(
      actionButton("show","show")
    )
    
    server = function(input, output) { 
      col_name <- c("A","B","C","D")
      observeEvent(input$show,{
                  text <- ""
                   i <- 1
                   while (i <= length(col_name)) {
                     print(i)
                     text <- paste(text,col_name[i],"<br/>")
                     i = i+1
                   }
                   text <- HTML(text)
                   showModal(modalDialog(
                     title = "Edit", 
                     text
                   ))
      })
    }
    shinyApp(ui,server)
    

    但是,如果您不需要 while 循环,这里有一个更简洁的解决方案。

    library(shiny)
    
    ui <- fluidPage(
      actionButton("show","show")
    )
    
    server = function(input, output) { 
      col_name <- c("A","B","C","D")
      observeEvent(input$show,{
                   showModal(modalDialog(
                     title = "Edit", 
                     HTML(paste(col_name, collapse = "<br/>"))
                   ))
      })
    }
    shinyApp(ui,server)
    

    【讨论】:

      猜你喜欢
      • 2017-02-15
      • 1970-01-01
      • 2016-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多