如何在RShiny中使用循环初始化函数/声明

时间:2018-06-15 15:04:43

标签: r loops shiny

是否可以在R中为RShiny创建带有循环的tabItems和subItems以及fluidRow框?

例如,而不是完成所有这些:

  tabItem(tabName = "table1", 
          textOutput("outputText1"),
          div(style = 'overflow-x: scroll', DT :: dataTableOutput("table1"))
  ),
  tabItem(tabName = "table2", 
          textOutput("outputText2"), 
          div(style = 'overflow-x: scroll', DT :: dataTableOutput("table2"))
  ),
  tabItem(tabName = "table3", 
          textOutput("outputText3"), 
          div(stlye = 'overflow-x: scroll', DT :: dataTableOutput("table3"))
  ),
  tabItem(tabName = "table4", 
          textOutput("outputText4"), 
          div(stlye = 'overflow-x: scroll', DT :: dataTableOutput("table4"))
  ),
  tabItem(tabName = "table5", 
          textOutput("outputText5"), 
          div(stlye = 'overflow-x: scroll', DT :: dataTableOutput("table5"))
  ),
  tabItem(tabName = "table6", 
          textOutput("outputText6"), 
          div(stlye = 'overflow-x: scroll', DT :: dataTableOutput("table6"))
  ),
  tabItem(tabName = "table7", 
          textOutput("outputText7"), 
          div(stlye = 'overflow-x: scroll', DT :: dataTableOutput("table7"))
  ),
  tabItem(tabName = "table8", 
          textOutput("outputText8"), 
          div(stlye = 'overflow-x: scroll', DT :: dataTableOutput("table8"))
  ),

我可以创建循环来创建tabItems吗?

额外的背景信息:在大学里,当我第一次学习R时,我的教授告诉我,使用if和for语句会导致R非常慢。所以我尽量避免使用循环。

1 个答案:

答案 0 :(得分:0)

您将不得不使用lapply并将UI插入到示例中的uiOutput组件中。

UI

library(shiny)

shinyUI(fluidPage(

sidebarLayout(
  sidebarPanel(
     #Just remove this
  ),

  mainPanel(
     uiOutput("dynamicTabset")

  )
)
))

服务器

library(shiny)

shinyServer(function(input, output) {

lapply(1:10, 
       function(x) {
         insertUI(
           selector = '#dynamicTabset',
           ui = tagList(
             tabItem(tabName = paste0("table",x), 
                     textOutput("outputText3"), 
                     div(style = 'overflow-x: scroll', DT :: dataTableOutput("table3"))
             )
         ))
       })
 })