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 5 # # # #################### #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 6 # # # #################### #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 7 # # # #################### #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 9 # # # #################### #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) }) })
Leave a Reply