Branches of mechanical engineering: Edifice Shiny App Exercises + Solutions - Business Office Eight - R


Transform your App into Dashboard
Now that nosotros covered the basic staff that yous involve to know inward gild to ready your App it is fourth dimension to heighten its appearance in addition to its functionality. The interface is real of import fot the user equally it must non alone locomote friendly but also tardily to use.
At this component subdivision nosotros volition transform your Shiny App into a beautiful Shiny Dashboard. Firstly nosotros volition do the interface in addition to and thence measuring yesteryear measuring nosotros volition “move” the App yous built inward the previous parts into this. In component subdivision 8 nosotros volition movement the app measuring yesteryear measuring into your dashboard in addition to inward the final ii parts nosotros volition heighten its appearance fifty-fifty to a greater extent than in addition to of course of teaching deploy it.
Read the examples below to empathize the logic of what nosotros are going to do in addition to and thence essay out yous skills amongst the practise laid nosotros prepared for you. Lets begin!
Answers to the exercises are available here.
INSTALLATION
The packages that nosotros are going to work is shinydashboard and shiny . To install, run:
install.packages("shinydashboard")
install.packages("shiny")
Learn more about Shiny inward the online course R Shiny Interactive Web Apps – Next Level Data Visualization. In this course of teaching yous volition acquire how to do advanced Shiny spider web apps; embed video, pdfs in addition to images; add together focus in addition to zooming tools; in addition to many other functionalities (30 lectures, 3hrs.).
Exercise 1
Install the package shinydashboard and the package shiny in your working directory.
BASICS
H5N1 dashboard has 3 parts: a header, a sidebar, in addition to a body. Here’s the most minimal possible UI for a dashboard page.
## ui.R ##
library(shinydashboard)
dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody()
)
Exercise 2
Add a dashboardPage and in addition to thence Header, Sidebar in addition to Body into your UI. HINT: Use dashboardPagedashboardHeaderdashboardSidebardashboardBody.
First of all nosotros should rear it with title like below:
## ui.R ##
library(shinydashboard)
dashboardPage(
dashboardHeader(title="branchesofmechanicalengineering: Building Shiny App exercises + Solutions - component subdivision 8 - R"box-sizing: inherit;" />dashboardSidebar(),
dashboardBody()
)
Exercise 3
Name your dashboard “Shiny App”. HINT: Use title.
Next, nosotros tin add together content to the sidebar. For this instance we’ll add together card items that bear similar tabs. These role similarly to Shiny’s tabPanels: when yous click on i card item, it shows a dissimilar laid of content inward the primary body.
There are ii parts that involve to locomote done. First, yous involve to add menuItems to the sidebar, amongst appropriate tabNames.
## Sidebar content
dashboardSidebar(
sidebarMenu(
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
menuItem("Widgets", tabName = "widgets", icon = icon("th"))
)
)
Exercise 4
Create three menuItem, rear them “DATA TABLE”, “SUMMARY” in addition to “K-MEANS” respectively. Make certain to work distict tabName for each i of them. The icon is of your choise. HINT: Use menuItemtabName and icon.
In the body, add tabItems with corresponding values for tabName:
## Body content
dashboardBody(
tabItems(
tabItem(tabName = "dashboard",
h2("Dashboard"),
fluidRow(
box()
)
),
tabItem(tabName = "widgets",
h2("WIDGETS")
),
)
)
Exercise 5
Add tabItems in dashboardBody. Be certain to plough over the same tabName to each i to acquire them linked amongst your menuItem. HINT: Use tabItemstabItemh2.
Obviously, this dashboard isn’t real useful. We’ll involve to add together components that genuinely do something. In the torso nosotros tin add together boxes that convey content.
Firstly let’s do a box for our dataTable in the tabItem with tabName “dt”.
## Body content
dashboardBody(
tabItems(
tabItem(tabName = "dashboard",
h2("Dashboard"),
fluidRow(
box()
)
),
tabItem(tabName = "widgets",
h2("WIDGETS")
),
)
)
Exercise 6
Specify the fluidrow and do a box inside the “DATA TABLE” tabItem. HINT: Use fluidrow and box.
Exercise 7
Do the same for the other two tabItem. Create one fluidrow and one box in the “SUMMARY” in addition to another fluidrow with four boxin the “K-MEANS”.
Now but re-create in addition to glue the code below, which yous used in part 7 to movement your dataTable inside the “DATA TABLE” tabItem.
#ui.R
dataTableOutput("Table"),width = 400
#server.R
output$Table <- renderDataTable(
iris,options = list(
lengthMenu = list(c(10, 20, 30,-1),c('10','20','30','ALL')),
pageLength = 10))
Exercise 8
Place the sample code inward a higher house inward the correct house inward gild to add together the dataTable “Table” within the “DATA TABLE” tabItem.
Now but re-create in addition to glue the code below, which yous used in part 7 to movement the dataTable “Table2” within the “SUMMARY” tabItem.
#ui.R
dataTableOutput("Table2"),width = 400
#server.R
sumiris<-as.data.frame.array(summary(iris))
output$Table2 <- renderDataTable(sumiris)
Exercise 9
Place the sample code inward a higher house inward the correct house inward gild to add together the dataTable “Table2” within the “SUMMARY” tabItem.
Do the same for the final practise equally yous but convey to seat the code from part 7 inside the “K-MEANS” tabItem.
Exercise 10

Place the K-Means plot and the 3 widgets from part 7 inside the four box you created before.
__________________________________________

Below are the solutions to these exercises on Building Shiny App.
#################### #                  # #    Exercise 1    # #                  # ####################  #ui.R install.packages("shinydashboard") install.packages("shiny")  #################### #                  # #    Exercise 2    # #                  # ####################  #ui.r library(shiny) library(shinydashboard)  dashboardPage(   dashboardHeader(),   dashboardSidebar(),   dashboardBody() ) #server.R function(input, output) { }  #################### #                  # #    Exercise 3    # #                  # ####################  #ui.r library(shiny) library(shinydashboard)  dashboardPage(   dashboardHeader(title = "Shiny App"),   dashboardSidebar(),   dashboardBody() ) #server.R function(input, output) { }  #################### #                  # #    Exercise 4    # #                  # ####################  #ui.r library(shiny) library(shinydashboard)  dashboardPage(   dashboardHeader(title = "Shiny App"),   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() ) #server.R function(input, output) { }  #################### #                  # #    Exercise v    # #                  # ####################  #ui.r library(shiny) library(shinydashboard)  dashboardPage(   dashboardHeader(title = "Shiny App"),   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")               ),       tabItem(tabName = "sm",               h2("SUMMARY")       ),       tabItem(tabName = "km",               h2("K-MEANS")       )   ) )) #server.R function(input, output) { }  #################### #                  # #    Exercise half dozen    # #                  # ####################  #ui.r library(shiny) library(shinydashboard)  dashboardPage(   dashboardHeader(title = "Shiny App"),   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()               )),       tabItem(tabName = "sm",               h2("SUMMARY")       ),       tabItem(tabName = "km",               h2("K-MEANS")       )   ) )) #server.R function(input, output) { }  #################### #                  # #    Exercise vii    # #                  # ####################  #ui.r library(shiny) library(shinydashboard)  dashboardPage(   dashboardHeader(title = "Shiny App"),   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()               )),       tabItem(tabName = "sm",               h2("SUMMARY"),               fluidRow(                 box()               )       ),       tabItem(tabName = "km",               h2("K-MEANS"),               fluidRow(                 box(),                 box(),                 box(),                 box()               )       )   ) )) #server.R function(input, output) { }  #################### #                  # #    Exercise 8    # #                  # ####################  #ui.r library(shiny) library(shinydashboard)  dashboardPage(   dashboardHeader(title = "Shiny App"),   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()               )       ),       tabItem(tabName = "km",               h2("K-MEANS"),               fluidRow(                 box(),                 box(),                 box(),                 box()               )       )   ) )) #server.R function(input, output) {   output$Table <- renderDataTable(     iris,options = list(       lengthMenu = list(c(10, 20, 30,-1),c('10','20','30','ALL')),       pageLength = 10))}  #################### #                  # #    Exercise ix    # #                  # ####################  #ui.r library(shiny) library(shinydashboard)  dashboardPage(   dashboardHeader(title = "Shiny App"),   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(),                 box(),                 box(),                 box()               )       )   ) )) #server.R 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)}  #################### #                  # #    Exercise 10   # #                  # ####################  #ui.r library(shiny) library(shinydashboard)  dashboardPage(   dashboardHeader(title = "Shiny App"),   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)   }) }) 


Sources:

http://www.r-exercises.com/2017/03/24/building-shiny-app-solutions-part-8/
http://www.r-exercises.com/2017/03/24/building-shiny-app-exercises-part-8/

Sumber http://engdashboard.blogspot.com/

Jangan sampai ketinggalan postingan-postingan terbaik dari Branches of mechanical engineering: Edifice Shiny App Exercises + Solutions - Business Office Eight - R. Berlangganan melalui email sekarang juga:

Bali Attractions

BACA JUGA LAINNYA:

Bali Attractions