ShinyDashboard Dynamic Bullet Points

你说的曾经没有我的故事 提交于 2019-12-11 06:01:34

问题


I have hopefully a simple problem to pass a tags ordered list to in a Shiny Dashboard. What I'd like to do is have a function that makes an ordered list of bullet points based upon on a filtered category.

Here is a trivial example of what I'd like to be able to do with a data frame called nba_teams

teams    conference
Bulls    Eastern
Nuggets  Western
Celtics  Eastern
Lakers   Western

Now if I write this function it will break out the list for the respective conferences:

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))
 }
}

  • Nuggets
  • Lakers
  • What I'd like to do is to have this in a tab box such that:

    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))
     }
    })),
    

    But this just leaves the box blank and won't populate the box with a bullet point for each observation.

    Any suggestions? Thank you!


    回答1:


    I would use lapply in this case:

    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)
    


    来源:https://stackoverflow.com/questions/52938451/shinydashboard-dynamic-bullet-points

    易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
    该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!