ShinyDashboard动态项目符号点

时间:2018-10-22 22:12:55

标签: r shiny shinydashboard

我希望有一个简单的问题,可以将标记的有序列表传递到Shiny Dashboard中。我想做的是有一个函数,该函数根据已过滤的类别列出项目符号点的有序列表。

这是一个我想对名为nba_teams的数据框进行处理的简单例子。

teams    conference
Bulls    Eastern
Nuggets  Western
Celtics  Eastern
Lakers   Western

现在,如果我编写此功能,它将列出各个会议的列表:

for (row in 1:nrow(nba_teams)){
  teams <- nba_teams[row, "teams"]
  conference <- nba_teams[row,"conference"]

  if(grepl("Western",conference)){
   print(tags$li(teams))
 }
}

  • 掘金
  •    
  • 湖人
  • 我想做的就是在选项卡框中输入以下内容:

    box(
     title = "Western Conference",
     tags$ol(
      for (row in 1:nrow(nba_teams)){
      teams <- nba_teams[row, "teams"]
      conference <- nba_teams[row,"conference"]
    
      if(grepl("Western",conference)){
       print(tags$li(teams))
     }
    })),
    

    但是,这只是将框留为空白,并且不会为每次观察填充一个带有项目符号的框。

    有什么建议吗?谢谢!

    1 个答案:

    答案 0 :(得分:1)

    在这种情况下,我会使用lapply

    library(shiny)  
    library(shinydashboard)
    
    nba_teams <- data.frame(team = c("Bulls", "Nuggest", "Celtics", "Lakers"),
                            conference = c("Eastern", "Western", "Eastern", "Western"))
    
    ui <- dashboardPage(
      dashboardHeader(),
      dashboardSidebar(),
      dashboardBody(
        box(
          title = "Western Conference",
          tags$ol(
            lapply(1:nrow(nba_teams), function(x) {
              if (nba_teams$conference[x]=="Western") {
                return(tags$li(nba_teams$team[x]))
              }
            })
          )
        )
      )
    )
    
    server <- function(input, output, session) {}
    
    shinyApp(ui, server)