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