In this component subdivision nosotros volition “dig deeper” to give away the amazing capabilities that a Shiny Dasboard provides.
Read the examples below to empathize the logic of what nosotros are going to do in addition to and hence evidence yous skills alongside the do laid nosotros prepared for you. Lets begin!
Read the examples below to empathize the logic of what nosotros are going to do in addition to and hence evidence yous skills alongside the do laid nosotros prepared for you. Lets begin!
Answers to the exercises are available here.
The
#ui.R
dashboardPage
function expects 3 components: a header, sidebar, in addition to body:#ui.R
dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody()
)
For to a greater extent than complicated apps, splitting app into pieces tin larn inward to a greater extent than readable:
header <- dashboardHeader()
sidebar <- dashboardSidebar()
trunk <- dashboardBody()
dashboardPage(header, sidebar, body)
Now we’ll expression at each of the 3 principal components of a
shinydashboard
. HEADER
Influenza A virus subtype H5N1 header tin convey a
title
and dropdown menus. The dropdown menus are generated yesteryear the dropdownMenu
function. There are 3 types of menus – messages, notifications, in addition to tasks – in addition to each i must move populated alongside a corresponding type of item. Message menus
A
#ui.R
messageItem
contained inward a message card needs values for from
and message
. You tin also command the icon
and a notification fourth dimension string. By default, the icon is a silhouette of a person. The fourth dimension string tin move whatever text. For example, it could move a relative date/time similar “5 minutes”, “today”, or “12:30pm yesterday”, or an absolute time, similar “2014-12-01 13:45”.#ui.R
dropdownMenu(type = "messages",
messageItem(
from = "Sales Dept",
message = "Sales are steady this month."
),
messageItem(
from = "New User",
message = "How do I register?",
icon = icon("question"),
time = "13:45"
),
messageItem(
from = "Support",
message = "The novel server is ready.",
icon = icon("life-ring"),
time = "2014-12-01"
)
)
Exercise 1
Create a
dropdownMenu
in your dashboardHeader
as the illustration above. Put date, fourth dimension in addition to to a greater extent than oftentimes than non text of your choice. Dynamic content
In virtually cases, you’ll desire to brand the content dynamic. That agency that the HTML content is generated on the server side in addition to sent to the customer for rendering. In the UI code, you’d use
dropdownMenuOutput
like this:dashboardHeader(dropdownMenuOutput("messageMenu"))
Exercise 2
Replace
dropdownMenu
with dropdownMenuOutput
and the three messageItem
with messageMenu
. The side yesteryear side measurement is to do to a greater extent than or less messages for this example.The code below does this piece of work for us.
# Example message information inward a information frame
# Example message information inward a information frame
messageData <- data.frame(
from = c("Admininstrator", "New User", "Support"),
message = c(
"Sales are steady this month.",
"How do I register?",
"The novel server is ready."
),
stringsAsFactors = FALSE
)
Exercise 3
Put
messageData
inside your server.r
but exterior of the shinyServer
function. And on the server side, you’d generate the entire card inward a
renderMenu
, similar this:output$messageMenu <- renderMenu({
# Code to generate each of the messageItems here, inward a list. messageData
# is a information frame alongside ii columns, 'from' in addition to 'message'.
# Also add together on slider value to the message content, hence that messages update.
msgs <- apply(messageData, 1, function(row) {
messageItem(
from = row[["from"]],
message = paste(row[["message"]], input$slider)
)
})
dropdownMenu(type = "messages", .list = msgs)
})
Exercise 4
Put the code above(
output$messageMenu
) inward the shinyServer
of server.R
. Hopefully you lot convey understood yesteryear forthwith the logic behind the dynamic content of your Menu. Now let’s homecoming to the static i inward gild to depict it a lilliputian chip more. So brand the proper changes to your code inward gild to homecoming just to the indicate nosotros were later do 1.
Notification menus
A
#ui.r
notificationItem
contained inward a notification contains a text notification. You tin also command the icon
and the status
color. The code below gives an example.#ui.r
dropdownMenu(type = "notifications",
notificationItem(
text = "20 novel users today",
icon("users")
),
notificationItem(
text = "14 items delivered",
icon("truck"),
status = "success"
),
notificationItem(
text = "Server charge at 84%",
icon = icon("exclamation-triangle"),
status = "warning"
)
)
Exercise 5
Create a
dropdownMenu
for your notifications similar the example. Use text of your choice. Be careful of the type
and the notificationItem
. Task menus
Task items convey a progress bar in addition to a text label. You tin also specify the color of the bar. Valid colors are listed in
#ui.r
?validColors
. Take a expression at the illustration below.#ui.r
dropdownMenu(type = "tasks", badgeStatus = "success",
taskItem(value = 90, color = "green",
"Documentation"
),
taskItem(value = 17, color = "aqua",
"Project X"
),
taskItem(value = 75, color = "yellow",
"Server deployment"
),
taskItem(value = 80, color = "red",
"Overall project"
)
)
Exercise 6
Create a
dropdownMenu
for your tasks similar the illustration above. Use text of your choice in addition to do every bit many taskItem
as you lot want. Be carefull of the type
and the taskItem
. Disabling the header
If you lot don’t desire to demonstrate a header bar, you lot tin disable it with:
dashboardHeader(disable = TRUE)
Exercise 7
Disable the header.
Now enable it again.
Body
The trunk of a dashboard page tin comprise whatever regular Shiny content. However, if you’re creating a dashboard you’ll probable desire to brand something that’s to a greater extent than structured. The basic edifice block of virtually dashboards is a
box
. Boxes inward plow tin comprise whatever content. Boxes
Boxes are the principal edifice blocks of dashboard pages. Influenza A virus subtype H5N1 basic box tin move created alongside the
Boxes tin convey titles in addition to header bar colors alongside the
box
function, in addition to the contents of the box
can move (most) whatever Shiny UI content. We convey already created to a greater extent than or less boxes in part 8 so lets heighten theis appearance a lilliputian bit.Boxes tin convey titles in addition to header bar colors alongside the
title
and status
options. Look at the examples below.box(title = "Histogram", condition = "primary",solidHeader = TRUE, plotOutput("plot2", pinnacle = 250)),
box(
title = "Inputs", condition = "warning",
"Box content here", br(), "More box content",
sliderInput("slider", "Slider input:", 1, 100, 50),
textInput("text", "Text input:")
)
Exercise 8
Give a championship of your choice to all the
box
you convey created inward your dashboard except of the 3 widgets’ box
. Exercise 9
Change the condition of the starting fourth dimension three
box
to “primary” in addition to the final 3 to “warning”. Exercise 10
Transform the headers of your starting fourth dimension three
box
to corporation headers. _______________________________________________
Below are the solutions to these exercises on Building Shiny App.
#################### # # # Exercise 1 # # # #################### #ui.r library(shiny) library(shinydashboard) dashboardPage( dashboardHeader(title = "Shiny App", dropdownMenu(type = "messages", messageItem( from = "Bob Carter", message = "Excellent dashboard.Keep the skilful job" ), messageItem( from = "New User", message = "How do I register?", icon = icon("question"), time = "23:00" ), messageItem( from = "Support", message = "The novel server is ready.", icon = icon("life-ring"), time = "2017-15-03" ) )), dashboardSidebar( sidebarMenu( menuItem("DATATABLE", tabName = "dt", icon = icon("dashboard")), menuItem("SUMMARY", tabName = "sm", icon = icon("th")), menuItem("K-MEANS", tabName = "km", icon = icon("th")) )), dashboardBody( tabItems( tabItem(tabName = "dt", h2("DATA TABLE"), fluidRow( box(dataTableOutput("Table"),width = 400) )), tabItem(tabName = "sm", h2("SUMMARY"), fluidRow( box(dataTableOutput("Table2"),width = 400) ) ), tabItem(tabName = "km", h2("K-MEANS"), fluidRow( box(plotOutput("plot1",click = "mouse")), box(sliderInput("slider1", label = h4("Clusters"), min = 1, max = 9, value = 4), verbatimTextOutput("coord"))), fluidRow( box(checkboxGroupInput("checkGroup", label = h4("Variable X"),names(iris), selected=names(iris)[[2]] )), box(selectInput("select", label = h4("Variable Y"), names(iris),selected=names(iris)[[2]] ))) ) ) ) ) #server.R shinyServer(function(input, output) { output$Table <- renderDataTable( iris,options = list( lengthMenu = list(c(10, 20, 30,-1),c('10','20','30','ALL')), pageLength = 10)) sumiris<-as.data.frame.array(summary(iris)) output$Table2 <- renderDataTable(sumiris) output$plot1 <- renderPlot({ palette(c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3", "#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999")) plot(Data(),main = "K-MEANS", col = Clusters()$cluster, pch = 20, cex = 3, cex.main = 2, font.main= 4, col.main= "blue") }, width = "auto",height = "auto") output$coord <- renderText({ paste0("x=", input$mouse$x, "\ny=", input$mouse$y) }) Data <- reactive({iris[, c(input$select,input$checkGroup)] }) Clusters <- reactive({ kmeans(Data(),input$slider1) }) }) #################### # # # Exercise 2 # # # #################### #ui.r library(shiny) library(shinydashboard) dashboardPage( dashboardHeader(title = "Shiny App", dropdownMenuOutput("messageMenu" )), dashboardSidebar( sidebarMenu( menuItem("DATATABLE", tabName = "dt", icon = icon("dashboard")), menuItem("SUMMARY", tabName = "sm", icon = icon("th")), menuItem("K-MEANS", tabName = "km", icon = icon("th")) )), dashboardBody( tabItems( tabItem(tabName = "dt", h2("DATA TABLE"), fluidRow( box(dataTableOutput("Table"),width = 400) )), tabItem(tabName = "sm", h2("SUMMARY"), fluidRow( box(dataTableOutput("Table2"),width = 400) ) ), tabItem(tabName = "km", h2("K-MEANS"), fluidRow( box(plotOutput("plot1",click = "mouse")), box(sliderInput("slider1", label = h4("Clusters"), min = 1, max = 9, value = 4), verbatimTextOutput("coord"))), fluidRow( box(checkboxGroupInput("checkGroup", label = h4("Variable X"),names(iris), selected=names(iris)[[2]] )), box(selectInput("select", label = h4("Variable Y"), names(iris),selected=names(iris)[[2]] ))) ) ) ) ) #server.R shinyServer(function(input, output) { output$Table <- renderDataTable( iris,options = list( lengthMenu = list(c(10, 20, 30,-1),c('10','20','30','ALL')), pageLength = 10)) sumiris<-as.data.frame.array(summary(iris)) output$Table2 <- renderDataTable(sumiris) output$plot1 <- renderPlot({ palette(c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3", "#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999")) plot(Data(),main = "K-MEANS", col = Clusters()$cluster, pch = 20, cex = 3, cex.main = 2, font.main= 4, col.main= "blue") }, width = "auto",height = "auto") output$coord <- renderText({ paste0("x=", input$mouse$x, "\ny=", input$mouse$y) }) Data <- reactive({iris[, c(input$select,input$checkGroup)] }) Clusters <- reactive({ kmeans(Data(),input$slider1) }) }) #################### # # # Exercise 3 # # # #################### #ui.r library(shiny) library(shinydashboard) dashboardPage( dashboardHeader(title = "Shiny App", dropdownMenuOutput("messageMenu" )), dashboardSidebar( sidebarMenu( menuItem("DATATABLE", tabName = "dt", icon = icon("dashboard")), menuItem("SUMMARY", tabName = "sm", icon = icon("th")), menuItem("K-MEANS", tabName = "km", icon = icon("th")) )), dashboardBody( tabItems( tabItem(tabName = "dt", h2("DATA TABLE"), fluidRow( box(dataTableOutput("Table"),width = 400) )), tabItem(tabName = "sm", h2("SUMMARY"), fluidRow( box(dataTableOutput("Table2"),width = 400) ) ), tabItem(tabName = "km", h2("K-MEANS"), fluidRow( box(plotOutput("plot1",click = "mouse")), box(sliderInput("slider1", label = h4("Clusters"), min = 1, max = 9, value = 4), verbatimTextOutput("coord"))), fluidRow( box(checkboxGroupInput("checkGroup", label = h4("Variable X"),names(iris), selected=names(iris)[[2]] )), box(selectInput("select", label = h4("Variable Y"), names(iris),selected=names(iris)[[2]] ))) ) ) ) ) #server.R messageData <- data.frame( from = c("Admininstrator", "New User", "Support"), message = c( "Sales are steady this month.", "How do I register?", "The novel server is ready." ), stringsAsFactors = FALSE ) shinyServer(function(input, output) { output$Table <- renderDataTable( iris,options = list( lengthMenu = list(c(10, 20, 30,-1),c('10','20','30','ALL')), pageLength = 10)) sumiris<-as.data.frame.array(summary(iris)) output$Table2 <- renderDataTable(sumiris) output$plot1 <- renderPlot({ palette(c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3", "#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999")) plot(Data(),main = "K-MEANS", col = Clusters()$cluster, pch = 20, cex = 3, cex.main = 2, font.main= 4, col.main= "blue") }, width = "auto",height = "auto") output$coord <- renderText({ paste0("x=", input$mouse$x, "\ny=", input$mouse$y) }) Data <- reactive({iris[, c(input$select,input$checkGroup)] }) Clusters <- reactive({ kmeans(Data(),input$slider1) }) }) #################### # # # Exercise iv # # # #################### #ui.r library(shiny) library(shinydashboard) dashboardPage( dashboardHeader(title = "Shiny App", dropdownMenuOutput("messageMenu" )), dashboardSidebar( sidebarMenu( menuItem("DATATABLE", tabName = "dt", icon = icon("dashboard")), menuItem("SUMMARY", tabName = "sm", icon = icon("th")), menuItem("K-MEANS", tabName = "km", icon = icon("th")) )), dashboardBody( tabItems( tabItem(tabName = "dt", h2("DATA TABLE"), fluidRow( box(dataTableOutput("Table"),width = 400) )), tabItem(tabName = "sm", h2("SUMMARY"), fluidRow( box(dataTableOutput("Table2"),width = 400) ) ), tabItem(tabName = "km", h2("K-MEANS"), fluidRow( box(plotOutput("plot1",click = "mouse")), box(sliderInput("slider1", label = h4("Clusters"), min = 1, max = 9, value = 4), verbatimTextOutput("coord"))), fluidRow( box(checkboxGroupInput("checkGroup", label = h4("Variable X"),names(iris), selected=names(iris)[[2]] )), box(selectInput("select", label = h4("Variable Y"), names(iris),selected=names(iris)[[2]] ))) ) ) ) ) #server.R messageData <- data.frame( from = c("Admininstrator", "New User", "Support"), message = c( "Sales are steady this month.", "How do I register?", "The novel server is ready." ), stringsAsFactors = FALSE ) shinyServer(function(input, output) { output$Table <- renderDataTable( iris,options = list( lengthMenu = list(c(10, 20, 30,-1),c('10','20','30','ALL')), pageLength = 10)) sumiris<-as.data.frame.array(summary(iris)) output$Table2 <- renderDataTable(sumiris) output$plot1 <- renderPlot({ palette(c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3", "#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999")) plot(Data(),main = "K-MEANS", col = Clusters()$cluster, pch = 20, cex = 3, cex.main = 2, font.main= 4, col.main= "blue") }, width = "auto",height = "auto") output$coord <- renderText({ paste0("x=", input$mouse$x, "\ny=", input$mouse$y) }) Data <- reactive({iris[, c(input$select,input$checkGroup)] }) Clusters <- reactive({ kmeans(Data(),input$slider1) }) output$messageMenu <- renderMenu({ # Code to generate each of the messageItems here, inward a list. messageData # is a information frame alongside ii columns, 'from' in addition to 'message'. # Also add together on slider value to the message content, hence that messages update. msgs <- apply(messageData, 1, function(row) { messageItem( from = row[["from"]], message = paste(row[["message"]], input$slider) ) }) dropdownMenu(type = "messages", .list = msgs) }) }) #################### # # # Exercise v # # # #################### #ui.r library(shiny) library(shinydashboard) dashboardPage( dashboardHeader(title = "Shiny App", dropdownMenu(type = "messages", messageItem( from = "Bob Carter", message = "Excellent dashboard.Keep the skilful job" ), messageItem( from = "New User", message = "How do I register?", icon = icon("question"), time = "23:00" ), messageItem( from = "Support", message = "The novel server is ready.", icon = icon("life-ring"), time = "2017-15-03" ) ), dropdownMenu(type = "notifications", notificationItem( text = "5 novel users today", icon("users") ), notificationItem( text = "12 items delivered", icon("truck"), status = "success" ), notificationItem( text = "Server charge at 86%", icon = icon("exclamation-triangle"), status = "warning" ) )), dashboardSidebar( sidebarMenu( menuItem("DATATABLE", tabName = "dt", icon = icon("dashboard")), menuItem("SUMMARY", tabName = "sm", icon = icon("th")), menuItem("K-MEANS", tabName = "km", icon = icon("th")) )), dashboardBody( tabItems( tabItem(tabName = "dt", h2("DATA TABLE"), fluidRow( box(dataTableOutput("Table"),width = 400) )), tabItem(tabName = "sm", h2("SUMMARY"), fluidRow( box(dataTableOutput("Table2"),width = 400) ) ), tabItem(tabName = "km", h2("K-MEANS"), fluidRow( box(plotOutput("plot1",click = "mouse")), box(sliderInput("slider1", label = h4("Clusters"), min = 1, max = 9, value = 4), verbatimTextOutput("coord"))), fluidRow( box(checkboxGroupInput("checkGroup", label = h4("Variable X"),names(iris), selected=names(iris)[[2]] )), box(selectInput("select", label = h4("Variable Y"), names(iris),selected=names(iris)[[2]] ))) ) ) ) ) #server.R shinyServer(function(input, output) { output$Table <- renderDataTable( iris,options = list( lengthMenu = list(c(10, 20, 30,-1),c('10','20','30','ALL')), pageLength = 10)) sumiris<-as.data.frame.array(summary(iris)) output$Table2 <- renderDataTable(sumiris) output$plot1 <- renderPlot({ palette(c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3", "#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999")) plot(Data(),main = "K-MEANS", col = Clusters()$cluster, pch = 20, cex = 3, cex.main = 2, font.main= 4, col.main= "blue") }, width = "auto",height = "auto") output$coord <- renderText({ paste0("x=", input$mouse$x, "\ny=", input$mouse$y) }) Data <- reactive({iris[, c(input$select,input$checkGroup)] }) Clusters <- reactive({ kmeans(Data(),input$slider1) }) }) #################### # # # Exercise half-dozen # # # #################### #ui.r library(shiny) library(shinydashboard) dashboardPage( dashboardHeader(title = "Shiny App", dropdownMenu(type = "messages", messageItem( from = "Bob Carter", message = "Excellent dashboard.Keep the skilful job" ), messageItem( from = "New User", message = "How do I register?", icon = icon("question"), time = "23:00" ), messageItem( from = "Support", message = "The novel server is ready.", icon = icon("life-ring"), time = "2017-15-03" ) ), dropdownMenu(type = "notifications", notificationItem( text = "5 novel users today", icon("users") ), notificationItem( text = "12 items delivered", icon("truck"), status = "success" ), notificationItem( text = "Server charge at 86%", icon = icon("exclamation-triangle"), status = "warning" ) ), dropdownMenu(type = "tasks", badgeStatus = "success", taskItem(value = 90, color = "green", "Documentation" ), taskItem(value = 17, color = "aqua", "Project Iris" ), taskItem(value = 75, color = "yellow", "Server deployment" ), taskItem(value = 80, color = "red", "Overall project" ) )), dashboardSidebar( sidebarMenu( menuItem("DATATABLE", tabName = "dt", icon = icon("dashboard")), menuItem("SUMMARY", tabName = "sm", icon = icon("th")), menuItem("K-MEANS", tabName = "km", icon = icon("th")) )), dashboardBody( tabItems( tabItem(tabName = "dt", h2("DATA TABLE"), fluidRow( box(dataTableOutput("Table"),width = 400) )), tabItem(tabName = "sm", h2("SUMMARY"), fluidRow( box(dataTableOutput("Table2"),width = 400) ) ), tabItem(tabName = "km", h2("K-MEANS"), fluidRow( box(plotOutput("plot1",click = "mouse")), box(sliderInput("slider1", label = h4("Clusters"), min = 1, max = 9, value = 4), verbatimTextOutput("coord"))), fluidRow( box(checkboxGroupInput("checkGroup", label = h4("Variable X"),names(iris), selected=names(iris)[[2]] )), box(selectInput("select", label = h4("Variable Y"), names(iris),selected=names(iris)[[2]] ))) ) ) ) ) #server.R shinyServer(function(input, output) { output$Table <- renderDataTable( iris,options = list( lengthMenu = list(c(10, 20, 30,-1),c('10','20','30','ALL')), pageLength = 10)) sumiris<-as.data.frame.array(summary(iris)) output$Table2 <- renderDataTable(sumiris) output$plot1 <- renderPlot({ palette(c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3", "#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999")) plot(Data(),main = "K-MEANS", col = Clusters()$cluster, pch = 20, cex = 3, cex.main = 2, font.main= 4, col.main= "blue") }, width = "auto",height = "auto") output$coord <- renderText({ paste0("x=", input$mouse$x, "\ny=", input$mouse$y) }) Data <- reactive({iris[, c(input$select,input$checkGroup)] }) Clusters <- reactive({ kmeans(Data(),input$slider1) }) }) #################### # # # Exercise seven # # # #################### #ui.r library(shiny) library(shinydashboard) dashboardPage( dashboardHeader(disable=TRUE,title = "Shiny App", dropdownMenu(type = "messages", messageItem( from = "Bob Carter", message = "Excellent dashboard.Keep the skilful job" ), messageItem( from = "New User", message = "How do I register?", icon = icon("question"), time = "23:00" ), messageItem( from = "Support", message = "The novel server is ready.", icon = icon("life-ring"), time = "2017-15-03" ) ), dropdownMenu(type = "notifications", notificationItem( text = "5 novel users today", icon("users") ), notificationItem( text = "12 items delivered", icon("truck"), status = "success" ), notificationItem( text = "Server charge at 86%", icon = icon("exclamation-triangle"), status = "warning" ) ), dropdownMenu(type = "tasks", badgeStatus = "success", taskItem(value = 90, color = "green", "Documentation" ), taskItem(value = 17, color = "aqua", "Project Iris" ), taskItem(value = 75, color = "yellow", "Server deployment" ), taskItem(value = 80, color = "red", "Overall project" ) )), dashboardSidebar( sidebarMenu( menuItem("DATATABLE", tabName = "dt", icon = icon("dashboard")), menuItem("SUMMARY", tabName = "sm", icon = icon("th")), menuItem("K-MEANS", tabName = "km", icon = icon("th")) )), dashboardBody( tabItems( tabItem(tabName = "dt", h2("DATA TABLE"), fluidRow( box(dataTableOutput("Table"),width = 400) )), tabItem(tabName = "sm", h2("SUMMARY"), fluidRow( box(dataTableOutput("Table2"),width = 400) ) ), tabItem(tabName = "km", h2("K-MEANS"), fluidRow( box(plotOutput("plot1",click = "mouse")), box(sliderInput("slider1", label = h4("Clusters"), min = 1, max = 9, value = 4), verbatimTextOutput("coord"))), fluidRow( box(checkboxGroupInput("checkGroup", label = h4("Variable X"),names(iris), selected=names(iris)[[2]] )), box(selectInput("select", label = h4("Variable Y"), names(iris),selected=names(iris)[[2]] ))) ) ) ) ) #server.R shinyServer(function(input, output) { output$Table <- renderDataTable( iris,options = list( lengthMenu = list(c(10, 20, 30,-1),c('10','20','30','ALL')), pageLength = 10)) sumiris<-as.data.frame.array(summary(iris)) output$Table2 <- renderDataTable(sumiris) output$plot1 <- renderPlot({ palette(c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3", "#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999")) plot(Data(),main = "K-MEANS", col = Clusters()$cluster, pch = 20, cex = 3, cex.main = 2, font.main= 4, col.main= "blue") }, width = "auto",height = "auto") output$coord <- renderText({ paste0("x=", input$mouse$x, "\ny=", input$mouse$y) }) Data <- reactive({iris[, c(input$select,input$checkGroup)] }) Clusters <- reactive({ kmeans(Data(),input$slider1) }) }) #################### # # # Exercise 8 # # # #################### #ui.r library(shiny) library(shinydashboard) dashboardPage( dashboardHeader(title = "Shiny App", dropdownMenu(type = "messages", messageItem( from = "Bob Carter", message = "Excellent dashboard.Keep the skilful job" ), messageItem( from = "New User", message = "How do I register?", icon = icon("question"), time = "23:00" ), messageItem( from = "Support", message = "The novel server is ready.", icon = icon("life-ring"), time = "2017-15-03" ) ), dropdownMenu(type = "notifications", notificationItem( text = "5 novel users today", icon("users") ), notificationItem( text = "12 items delivered", icon("truck"), status = "success" ), notificationItem( text = "Server charge at 86%", icon = icon("exclamation-triangle"), status = "warning" ) ), dropdownMenu(type = "tasks", badgeStatus = "success", taskItem(value = 90, color = "green", "Documentation" ), taskItem(value = 17, color = "aqua", "Project Iris" ), taskItem(value = 75, color = "yellow", "Server deployment" ), taskItem(value = 80, color = "red", "Overall project" ) )), dashboardSidebar( sidebarMenu( menuItem("DATATABLE", tabName = "dt", icon = icon("dashboard")), menuItem("SUMMARY", tabName = "sm", icon = icon("th")), menuItem("K-MEANS", tabName = "km", icon = icon("th")) )), dashboardBody( tabItems( tabItem(tabName = "dt", h2("DATA TABLE"), fluidRow( box(title="DATA TABLE",dataTableOutput("Table"),width = 400) )), tabItem(tabName = "sm", h2("SUMMARY"), fluidRow( box(title="SUMMARY DATA TABLE",dataTableOutput("Table2"),width = 400) ) ), tabItem(tabName = "km", fluidRow( box(title="K-MEANS",plotOutput("plot1",click = "mouse")), box(sliderInput("slider1", label = h4("Clusters"), min = 1, max = 9, value = 4), verbatimTextOutput("coord"))), fluidRow( box(checkboxGroupInput("checkGroup", label = h4("Variable X"),names(iris), selected=names(iris)[[2]] )), box(selectInput("select", label = h4("Variable Y"), names(iris),selected=names(iris)[[2]] ))) ) ) ) ) #server.R shinyServer(function(input, output) { output$Table <- renderDataTable( iris,options = list( lengthMenu = list(c(10, 20, 30,-1),c('10','20','30','ALL')), pageLength = 10)) sumiris<-as.data.frame.array(summary(iris)) output$Table2 <- renderDataTable(sumiris) output$plot1 <- renderPlot({ palette(c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3", "#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999")) plot(Data(), col = Clusters()$cluster, pch = 20, cex = 3, cex.main = 2, font.main= 4, col.main= "blue") }, width = "auto",height = "auto") output$coord <- renderText({ paste0("x=", input$mouse$x, "\ny=", input$mouse$y) }) Data <- reactive({iris[, c(input$select,input$checkGroup)] }) Clusters <- reactive({ kmeans(Data(),input$slider1) }) }) #################### # # # Exercise ix # # # #################### #ui.r library(shiny) library(shinydashboard) dashboardPage( dashboardHeader(title = "Shiny App", dropdownMenu(type = "messages", messageItem( from = "Bob Carter", message = "Excellent dashboard.Keep the skilful job" ), messageItem( from = "New User", message = "How do I register?", icon = icon("question"), time = "23:00" ), messageItem( from = "Support", message = "The novel server is ready.", icon = icon("life-ring"), time = "2017-15-03" ) ), dropdownMenu(type = "notifications", notificationItem( text = "5 novel users today", icon("users") ), notificationItem( text = "12 items delivered", icon("truck"), status = "success" ), notificationItem( text = "Server charge at 86%", icon = icon("exclamation-triangle"), status = "warning" ) ), dropdownMenu(type = "tasks", badgeStatus = "success", taskItem(value = 90, color = "green", "Documentation" ), taskItem(value = 17, color = "aqua", "Project Iris" ), taskItem(value = 75, color = "yellow", "Server deployment" ), taskItem(value = 80, color = "red", "Overall project" ) )), dashboardSidebar( sidebarMenu( menuItem("DATATABLE", tabName = "dt", icon = icon("dashboard")), menuItem("SUMMARY", tabName = "sm", icon = icon("th")), menuItem("K-MEANS", tabName = "km", icon = icon("th")) )), dashboardBody( tabItems( tabItem(tabName = "dt", h2("DATA TABLE"), fluidRow( box(title="DATA TABLE",status="primary",dataTableOutput("Table"),width = 400) )), tabItem(tabName = "sm", h2("SUMMARY"), fluidRow( box(title="SUMMARY DATA TABLE",status="primary",dataTableOutput("Table2"),width = 400) ) ), tabItem(tabName = "km", fluidRow( box(title="K-MEANS",status="primary",plotOutput("plot1",click = "mouse")), box(status="warning",sliderInput("slider1", label = h4("Clusters"), min = 1, max = 9, value = 4), verbatimTextOutput("coord"))), fluidRow( box(status="warning",checkboxGroupInput("checkGroup", label = h4("Variable X"),names(iris), selected=names(iris)[[2]] )), box(status="warning",selectInput("select", label = h4("Variable Y"), names(iris),selected=names(iris)[[2]] ))) ) ) ) ) #server.R shinyServer(function(input, output) { output$Table <- renderDataTable( iris,options = list( lengthMenu = list(c(10, 20, 30,-1),c('10','20','30','ALL')), pageLength = 10)) sumiris<-as.data.frame.array(summary(iris)) output$Table2 <- renderDataTable(sumiris) output$plot1 <- renderPlot({ palette(c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3", "#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999")) plot(Data(), col = Clusters()$cluster, pch = 20, cex = 3, cex.main = 2, font.main= 4, col.main= "blue") }, width = "auto",height = "auto") output$coord <- renderText({ paste0("x=", input$mouse$x, "\ny=", input$mouse$y) }) Data <- reactive({iris[, c(input$select,input$checkGroup)] }) Clusters <- reactive({ kmeans(Data(),input$slider1) }) }) #################### # # # Exercise 10 # # # #################### #ui.r library(shiny) library(shinydashboard) dashboardPage( dashboardHeader(title = "Shiny App", dropdownMenu(type = "messages", messageItem( from = "Bob Carter", message = "Excellent dashboard.Keep the skilful job" ), messageItem( from = "New User", message = "How do I register?", icon = icon("question"), time = "23:00" ), messageItem( from = "Support", message = "The novel server is ready.", icon = icon("life-ring"), time = "2017-15-03" ) ), dropdownMenu(type = "notifications", notificationItem( text = "5 novel users today", icon("users") ), notificationItem( text = "12 items delivered", icon("truck"), status = "success" ), notificationItem( text = "Server charge at 86%", icon = icon("exclamation-triangle"), status = "warning" ) ), dropdownMenu(type = "tasks", badgeStatus = "success", taskItem(value = 90, color = "green", "Documentation" ), taskItem(value = 17, color = "aqua", "Project Iris" ), taskItem(value = 75, color = "yellow", "Server deployment" ), taskItem(value = 80, color = "red", "Overall project" ) )), dashboardSidebar( sidebarMenu( menuItem("DATATABLE", tabName = "dt", icon = icon("dashboard")), menuItem("SUMMARY", tabName = "sm", icon = icon("th")), menuItem("K-MEANS", tabName = "km", icon = icon("th")) )), dashboardBody( tabItems( tabItem(tabName = "dt", h2("DATA TABLE"), fluidRow( box(title="DATA TABLE",solidHeader = TRUE,status="primary",dataTableOutput("Table"),width = 400) )), tabItem(tabName = "sm", h2("SUMMARY"), fluidRow( box(title="SUMMARY DATA TABLE",solidHeader = TRUE,status="primary",dataTableOutput("Table2"),width = 400) ) ), tabItem(tabName = "km", fluidRow( box(title="K-MEANS",solidHeader = TRUE,status="primary",plotOutput("plot1",click = "mouse")), box(status="warning",sliderInput("slider1", label = h4("Clusters"), min = 1, max = 9, value = 4), verbatimTextOutput("coord"))), fluidRow( box(status="warning",checkboxGroupInput("checkGroup", label = h4("Variable X"),names(iris), selected=names(iris)[[2]] )), box(status="warning",selectInput("select", label = h4("Variable Y"), names(iris),selected=names(iris)[[2]] ))) ) ) ) ) #server shinyServer(function(input, output) { output$Table <- renderDataTable( iris,options = list( lengthMenu = list(c(10, 20, 30,-1),c('10','20','30','ALL')), pageLength = 10)) sumiris<-as.data.frame.array(summary(iris)) output$Table2 <- renderDataTable(sumiris) output$plot1 <- renderPlot({ palette(c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3", "#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999")) plot(Data(), col = Clusters()$cluster, pch = 20, cex = 3, cex.main = 2, font.main= 4, col.main= "blue") }, width = "auto",height = "auto") output$coord <- renderText({ paste0("x=", input$mouse$x, "\ny=", input$mouse$y) }) Data <- reactive({iris[, c(input$select,input$checkGroup)] }) Clusters <- reactive({ kmeans(Data(),input$slider1) }) })
http://www.r-exercises.com/2017/04/01/building-shiny-app-solutions-part-9/
http://www.r-exercises.com/2017/04/01/building-shiny-app-exercises-part-9/