Building Dashboards with shinydashboard
Png Kee Seng
Researcher
header <- dashboardHeader(...) sidebar <- dashboardSidebar(...) body <- dashboardBody(...)
ui <- dashboardPage(header, sidebar, body)
server <- function(input, output) { ... }
shinyApp(ui, server)
soccer %>%
ggplot(aes(x=`1_passes_compeletd`,
y=`1_goal_prevented`)) +
geom_hex(bins=10) + theme_classic()
server <- function(input, output){
output$plot1 <- renderPlot({
fifa %>%
ggplot(aes(x=`1_passes_compeletd`,
y=`1_goal_prevented`)) +
geom_hex(bins=10) + theme_classic()
})
}
server <- function(input, output) { output$plot1 <- renderPlot({ if (input$team == 1){
fifa %>% ggplot(aes(x=`1_passes_compeletd`, y=`1_goal_prevented`)) + geom_hex(bins=10) + theme_classic()
} else if (input$team == 2){
fifa %>% ggplot(aes(x=`2_passes_compeletd`, y=`2_goal_prevented`)) + geom_hex(bins=10) + theme_classic()
} }) }
plot_hex <- function(team){
if (team == 1){
soccer %>% ggplot(aes(x=`1_passes_compeletd`,
y=`1_goal_prevented`)) +
geom_hex(bins=10) + theme_classic()
}
else if (team == 2){
soccer %>% ggplot(aes(x=`2_passes_compeletd`,
y=`2_goal_prevented`)) +
geom_hex(bins=10) + theme_classic()
}
}
With helper function
server <- function(input, output) {
output$plot1 <- renderPlot(plot_hex(input$team))
}
Without helper function
server <- function(input, output) {
output$plot1 <- renderPlot({
if (input$team == 1){
soccer %>% ggplot(aes(x=`1_passes_compeletd`,
y=`1_goal_prevented`)) +
geom_hex(bins=10) + theme_classic()
}
else if (input$team == 2){
soccer %>% ggplot(aes(x=`2_passes_compeletd`,
y=`2_goal_prevented`)) +
geom_hex(bins=10) + theme_classic()
}
})
}
# A complete shinydashboard
header <- dashboardHeader(title = "Soccer tournament")
sidebar <- dashboardSidebar(sidebarMenu(menuItem("Passes/goals", tabName = "passtab"), menuItem("Empty", tabName = "emptytab")))
body <- dashboardBody(tabItems( tabItem("passtab", fluidRow(box(selectInput("team", "Team number", choices = c(1,2))), box(plotOutput("plot1"))) ), tabItem("emptytab")) )
ui <- dashboardPage(header, sidebar, body)
server <- function(input, output) { output$plot1 <- renderPlot(plot_hex(input$team)) }
shinyApp(ui, server)
Building Dashboards with shinydashboard