Connect widgets & plots
In the 7th constituent of our journeying nosotros are cook to connect to a greater extent than of the widgets nosotros created earlier amongst our k-means plot inwards gild to totally command its output. Of cousre nosotros volition also reform the plot itself properly inwards gild to acquire far a existent k-means plot.
Read the examples below to empathise the logic of what nosotros are going to do as well as hence examine yous skills amongst the practice laid nosotros prepared for you. Lets begin!
Read the examples below to empathise the logic of what nosotros are going to do as well as hence examine yous skills amongst the practice laid nosotros prepared for you. Lets begin!
Answers to the exercises are available here.
If you lot obtained a unlike (correct) response than those listed on the solutions page, delight experience costless to post service your response every bit a comment on that page.
First of all let’s movement the widgets nosotros are going to utilization from the
sidebarPanel
into the mainPanel
and specifically nether our plot
. Exercise 1
Remove the
textInput
from your server.R file. Then house the checkboxGroupInput
and the selectInput
in the same row amongst the sliderInput
. Name them “Variable X” as well as “Variable Y” respectively. HINT: Use fluidrow
and column
. Create a reactive expression
Reactive expressions are expressions that tin read reactive values as well as telephone telephone other reactive expressions. Whenever a reactive value changes, whatsoever reactive expressions that depended on it are marked every bit “invalidated” as well as volition automatically re-execute if necessary. If a reactive aspect is marked every bit invalidated, whatsoever other reactive expressions that lately called it are also marked every bit invalidated. In this way, invalidations ripple through the expressions that depend on each other.
The reactive aspect is activated similar this:
The reactive aspect is activated similar this:
example <- reactive({ })
Exercise 2
Place a reactive aspect inwards server.R, at whatsoever location except inside
output$All
and get upwardly it “Data”. HINT: Use reactive
Connect your dataset’s variables amongst your widgets.
Now let’s connect your
selectInput
with the variables of your dataset every bit inwards the instance below. #ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),
sidebarLayout(
sidebarPanel(h2(“Menu”),
selectInput(‘ycol’, ‘Y Variable’, names(iris)) ),
mainPanel(h1(“Main”)
)
)
))
#server.R
sidebarPanel(h2(“Menu”),
selectInput(‘ycol’, ‘Y Variable’, names(iris)) ),
mainPanel(h1(“Main”)
)
)
))
#server.R
shinyServer(function(input, output) {
example <- reactive({
iris[, c(input$ycol)]
})
})
Exercise 3
Put the variables of the
iris
dataset every bit inputs inwards your selectInput
as “Variable Y” . HINT: Use names
. Exercise 4
Do the same for
checkboxGroupInput
and “Variable X”. HINT: Use names
. Select the 4th variabale every bit default similar the instance below.
#ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),
sidebarLayout(
sidebarPanel(h2(“Menu”),
checkboxGroupInput(“xcol”, “Variable X”,names(iris),
selected=names(iris)[[4]]),
selectInput(“ycol”, “Y Variable”, names(iris),
selected=names(iris)[[4]])
),
mainPanel(h1(“Main”)
)
)
))
#server.R
sidebarPanel(h2(“Menu”),
checkboxGroupInput(“xcol”, “Variable X”,names(iris),
selected=names(iris)[[4]]),
selectInput(“ycol”, “Y Variable”, names(iris),
selected=names(iris)[[4]])
),
mainPanel(h1(“Main”)
)
)
))
#server.R
shinyServer(function(input, output) {
example <- reactive({
iris[, c(input$xcol,input$ycol)
]
})
})
Exercise 5
Make the minute variable the default choise for both widgets. HINT: Use
selected
. Now follow the instance below to do a novel role as well as house in that location the automated role for k agency calculation.
#ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),
sidebarLayout(
sidebarPanel(h2(“Menu”),
checkboxGroupInput(“xcol”, “Variable X”,names(iris),
selected=names(iris)[[4]]),
selectInput(“ycol”, “Y Variable”, names(iris),
selected=names(iris)[[4]])
),
mainPanel(h1(“Main”)
)
)
))
#server.R
sidebarPanel(h2(“Menu”),
checkboxGroupInput(“xcol”, “Variable X”,names(iris),
selected=names(iris)[[4]]),
selectInput(“ycol”, “Y Variable”, names(iris),
selected=names(iris)[[4]])
),
mainPanel(h1(“Main”)
)
)
))
#server.R
shinyServer(function(input, output) {
example <- reactive({
iris[, c(input$xcol,input$ycol)
]
})
example2 <- reactive({
kmeans(example())
})
})
Exercise 6
Create the reactive function
Clusters
and pose inwards in that location the function kmeans
which volition hold upwardly applied on the function Data
. HINT: Use reactive
. Connect your plot amongst the widgets.
It is fourth dimension to connect your plot amongst the widgets.
Exercise 7
Put
Data
inside renderPlot
as outset declaration replacing the information that you lot accept chosen to hold upwardly plotted until now. Moreover delete xlab
and ylab
. Improve your k-means visualiztion.
You gan alter automatically the colours of your clusters yesteryear copying as well as pasting this constituent of code every bit outset declaration of
renderPlot
before the plot
function:palette(c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3",
"#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999"))
We volition direct to accept upwardly to nine clusters hence nosotros direct nine colours.
Exercise 8
Set
min
of your sliderInput
to 1, max
to nine and value
to 4 as well as utilization the palette
function to plough over colours. This is how you lot tin plough over unlike colors to your clusters. To activate these colors pose this constituent of code into your
plot
function.col = Clusters()$cluster,
Exercise 9
Activate the
palette
function. To brand your clusters easily foundable you lot tin fully color them yesteryear adding into
plot
function this:pch
= 20, cex
= 3 Exercise 10
Fully color the
points
of your plot
. __________________________________________________
Below are the solutions to these exercises on Building Shiny App.
#################### # # # Exercise 1 # # # #################### #ui.R library(shiny) shinyUI(fluidPage( titlePanel("Shiny App"), sidebarLayout( sidebarPanel(h2("Menu"), br(), fluidRow( column(6, h4("Help Text"), helpText("Just for help"))), br(), fluidRow( column(6, numericInput("numer", label = h4("Numeric Input"), value = 10))), fluidRow( column(6, h4("Single Checkbox"), checkboxInput("checkbox", label = "Choice A", value = TRUE))), fluidRow( column(6, dateInput("date", label = h4("Date input"), value = "2016-12-01")), column(6 )), fluidRow( column(6, dateRangeInput("dates", label = h4("Date Range"))), column(6, textInput("text", label = h4("Text Input"), value = "Some Text"))), fileInput("file", label = h4("File Input"))), mainPanel(h1("Main"), img(src = "petal.jpg", height = 150, width = 200), br(), br(), p("This famous (Fisher's or Anderson's) ", a("iris",href="http://stat.ethz.ch/R-manual/R-devel/library/datasets/iris.html"), "data laid gives the measurements inwards centimeters of the variables sepal length as well as width as well as petal length as well as width, respectively, for l flowers from each of three species of iris. The species are ",strong( "Iris setosa,"),strong( "versicolor"), "and", strong("virginica.")), br(), h2("Analysis"), tabsetPanel(type="tabs",tabPanel("Data Table",dataTableOutput("Table")), tabPanel("Summary",dataTableOutput("Table2")), tabPanel("K means", plotOutput("plot1",click = "mouse"), verbatimTextOutput("coord"), uiOutput("All"))) ) ) )) #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({ plot(iris$Petal.Length,iris$Petal.Width,main = "K-MEANS",xlab="Petal Length",ylab = "Petal Width", 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) }) output$All <- renderUI({ tagList( fluidRow( column(4, sliderInput("slider1", label = h4("Clusters"), min = 3, max = 10, value = 3)), column(4, checkboxGroupInput("checkGroup", label = h4("Variable X"), choices = list("Choice 1" = 1, "Choice 2" = 2, "Choice 3" = 3), selected = 2)), column(4, selectInput("select", label = h4("Variable Y"), choices = list("Choice 1" = 1, "Choice 2" = 2 ), selected = 1))) ) }) }) #################### # # # Exercise ii # # # #################### #ui.R library(shiny) shinyUI(fluidPage( titlePanel("Shiny App"), sidebarLayout( sidebarPanel(h2("Menu"), br(), fluidRow( column(6, h4("Help Text"), helpText("Just for help"))), br(), fluidRow( column(6, numericInput("numer", label = h4("Numeric Input"), value = 10))), fluidRow( column(6, h4("Single Checkbox"), checkboxInput("checkbox", label = "Choice A", value = TRUE))), fluidRow( column(6, dateInput("date", label = h4("Date input"), value = "2016-12-01")), column(6 )), fluidRow( column(6, dateRangeInput("dates", label = h4("Date Range"))), column(6, textInput("text", label = h4("Text Input"), value = "Some Text"))), fileInput("file", label = h4("File Input"))), mainPanel(h1("Main"), img(src = "petal.jpg", height = 150, width = 200), br(), br(), p("This famous (Fisher's or Anderson's) ", a("iris",href="http://stat.ethz.ch/R-manual/R-devel/library/datasets/html/iris.html"), "data laid gives the measurements inwards centimeters of the variables sepal length as well as width as well as petal length as well as width, respectively, for l flowers from each of three species of iris. The species are ",strong( "Iris setosa,"),strong( "versicolor"), "and", strong("virginica.")), br(), h2("Analysis"), tabsetPanel(type="tabs",tabPanel("Data Table",dataTableOutput("Table")), tabPanel("Summary",dataTableOutput("Table2")), tabPanel("K means", plotOutput("plot1",click = "mouse"), verbatimTextOutput("coord"), uiOutput("All"))) ) ) )) #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({ plot(iris$Petal.Length,iris$Petal.Width,main = "K-MEANS",xlab="Petal Length",ylab = "Petal Width", 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) }) output$All <- renderUI({ tagList( fluidRow( column(4, sliderInput("slider1", label = h4("Clusters"), min = 3, max = 10, value = 3)), column(4, checkboxGroupInput("checkGroup", label = h4("Variable X"), choices = list("Choice 1" = 1, "Choice 2" = 2, "Choice 3" = 3), selected = 2)), column(4, selectInput("select", label = h4("Variable Y"), choices = list("Choice 1" = 1, "Choice 2" = 2 ), selected = 1))) ) }) Data <- reactive({ }) }) #################### # # # Exercise three # # # #################### #ui.R library(shiny) shinyUI(fluidPage( titlePanel("Shiny App"), sidebarLayout( sidebarPanel(h2("Menu"), br(), fluidRow( column(6, h4("Help Text"), helpText("Just for help"))), br(), fluidRow( column(6, numericInput("numer", label = h4("Numeric Input"), value = 10))), fluidRow( column(6, h4("Single Checkbox"), checkboxInput("checkbox", label = "Choice A", value = TRUE))), fluidRow( column(6, dateInput("date", label = h4("Date input"), value = "2016-12-01")), column(6 )), fluidRow( column(6, dateRangeInput("dates", label = h4("Date Range"))), column(6, textInput("text", label = h4("Text Input"), value = "Some Text"))), fileInput("file", label = h4("File Input"))), mainPanel(h1("Main"), img(src = "petal.jpg", height = 150, width = 200), br(), br(), p("This famous (Fisher's or Anderson's) ", a("iris",href="http://stat.ethz.ch/R-manual/R-devel/library/datasets/html/iris.html"), "data laid gives the measurements inwards centimeters of the variables sepal length as well as width as well as petal length as well as width, respectively, for l flowers from each of three species of iris. The species are ",strong( "Iris setosa,"),strong( "versicolor"), "and", strong("virginica.")), br(), h2("Analysis"), tabsetPanel(type="tabs",tabPanel("Data Table",dataTableOutput("Table")), tabPanel("Summary",dataTableOutput("Table2")), tabPanel("K means", plotOutput("plot1",click = "mouse"), verbatimTextOutput("coord"), uiOutput("All"))) ) ) )) #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({ plot(iris$Petal.Length,iris$Petal.Width,main = "K-MEANS",xlab="Petal Length",ylab = "Petal Width", 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) }) output$All <- renderUI({ tagList( fluidRow( column(4, sliderInput("slider1", label = h4("Clusters"), min = 3, max = 10, value = 3)), column(4, checkboxGroupInput("checkGroup", label = h4("Variable X"), choices = list("Choice 1" = 1, "Choice 2" = 2, "Choice 3" = 3), selected = 2)), column(4, selectInput("select", label = h4("Variable Y"), names(iris) ))) ) }) Data <- reactive({iris[, c(input$select)] }) }) #################### # # # Exercise 4 # # # #################### #ui.R library(shiny) shinyUI(fluidPage( titlePanel("Shiny App"), sidebarLayout( sidebarPanel(h2("Menu"), br(), fluidRow( column(6, h4("Help Text"), helpText("Just for help"))), br(), fluidRow( column(6, numericInput("numer", label = h4("Numeric Input"), value = 10))), fluidRow( column(6, h4("Single Checkbox"), checkboxInput("checkbox", label = "Choice A", value = TRUE))), fluidRow( column(6, dateInput("date", label = h4("Date input"), value = "2016-12-01")), column(6 )), fluidRow( column(6, dateRangeInput("dates", label = h4("Date Range"))), column(6, textInput("text", label = h4("Text Input"), value = "Some Text"))), fileInput("file", label = h4("File Input"))), mainPanel(h1("Main"), img(src = "petal.jpg", height = 150, width = 200), br(), br(), p("This famous (Fisher's or Anderson's) ", a("iris",href="http://stat.ethz.ch/R-manual/R-devel/library/datasets/html/iris.html"), "data laid gives the measurements inwards centimeters of the variables sepal length as well as width as well as petal length as well as width, respectively, for l flowers from each of three species of iris. The species are ",strong( "Iris setosa,"),strong( "versicolor"), "and", strong("virginica.")), br(), h2("Analysis"), tabsetPanel(type="tabs",tabPanel("Data Table",dataTableOutput("Table")), tabPanel("Summary",dataTableOutput("Table2")), tabPanel("K means", plotOutput("plot1",click = "mouse"), verbatimTextOutput("coord"), uiOutput("All"))) ) ) )) #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({ plot(iris$Petal.Length,iris$Petal.Width,main = "K-MEANS",xlab="Petal Length",ylab = "Petal Width", 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) }) output$All <- renderUI({ tagList( fluidRow( column(4, sliderInput("slider1", label = h4("Clusters"), min = 3, max = 10, value = 3)), column(4, checkboxGroupInput("checkGroup", label = h4("Variable X"),names(iris) )), column(4, selectInput("select", label = h4("Variable Y"), names(iris) ))) ) }) Data <- reactive({iris[, c(input$select,input$checkGroup)] }) }) #################### # # # Exercise five # # # #################### #ui.R library(shiny) shinyUI(fluidPage( titlePanel("Shiny App"), sidebarLayout( sidebarPanel(h2("Menu"), br(), fluidRow( column(6, h4("Help Text"), helpText("Just for help"))), br(), fluidRow( column(6, numericInput("numer", label = h4("Numeric Input"), value = 10))), fluidRow( column(6, h4("Single Checkbox"), checkboxInput("checkbox", label = "Choice A", value = TRUE))), fluidRow( column(6, dateInput("date", label = h4("Date input"), value = "2016-12-01")), column(6 )), fluidRow( column(6, dateRangeInput("dates", label = h4("Date Range"))), column(6, textInput("text", label = h4("Text Input"), value = "Some Text"))), fileInput("file", label = h4("File Input"))), mainPanel(h1("Main"), img(src = "petal.jpg", height = 150, width = 200), br(), br(), p("This famous (Fisher's or Anderson's) ", a("iris",href="http://stat.ethz.ch/R-manual/R-devel/library/datasets/html/iris.html"), "data laid gives the measurements inwards centimeters of the variables sepal length as well as width as well as petal length as well as width, respectively, for l flowers from each of three species of iris. The species are ",strong( "Iris setosa,"),strong( "versicolor"), "and", strong("virginica.")), br(), h2("Analysis"), tabsetPanel(type="tabs",tabPanel("Data Table",dataTableOutput("Table")), tabPanel("Summary",dataTableOutput("Table2")), tabPanel("K means", plotOutput("plot1",click = "mouse"), verbatimTextOutput("coord"), uiOutput("All"))) ) ) )) #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({ plot(iris$Petal.Length,iris$Petal.Width,main = "K-MEANS",xlab="Petal Length",ylab = "Petal Width", 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) }) output$All <- renderUI({ tagList( fluidRow( column(4, sliderInput("slider1", label = h4("Clusters"), min = 3, max = 10, value = 3)), column(4, checkboxGroupInput("checkGroup", label = h4("Variable X"),names(iris), selected=names(iris)[[2]] )), column(4, selectInput("select", label = h4("Variable Y"), names(iris),selected=names(iris)[[2]] ))) ) }) Data <- reactive({iris[, c(input$select,input$checkGroup)] }) }) #################### # # # Exercise vi # # # #################### #ui.R library(shiny) shinyUI(fluidPage( titlePanel("Shiny App"), sidebarLayout( sidebarPanel(h2("Menu"), br(), fluidRow( column(6, h4("Help Text"), helpText("Just for help"))), br(), fluidRow( column(6, numericInput("numer", label = h4("Numeric Input"), value = 10))), fluidRow( column(6, h4("Single Checkbox"), checkboxInput("checkbox", label = "Choice A", value = TRUE))), fluidRow( column(6, dateInput("date", label = h4("Date input"), value = "2016-12-01")), column(6 )), fluidRow( column(6, dateRangeInput("dates", label = h4("Date Range"))), column(6, textInput("text", label = h4("Text Input"), value = "Some Text"))), fileInput("file", label = h4("File Input"))), mainPanel(h1("Main"), img(src = "petal.jpg", height = 150, width = 200), br(), br(), p("This famous (Fisher's or Anderson's) ", a("iris",href="http://stat.ethz.ch/R-manual/R-devel/library/datasets/html/iris.html"), "data laid gives the measurements inwards centimeters of the variables sepal length as well as width as well as petal length as well as width, respectively, for l flowers from each of three species of iris. The species are ",strong( "Iris setosa,"),strong( "versicolor"), "and", strong("virginica.")), br(), h2("Analysis"), tabsetPanel(type="tabs",tabPanel("Data Table",dataTableOutput("Table")), tabPanel("Summary",dataTableOutput("Table2")), tabPanel("K means", plotOutput("plot1",click = "mouse"), verbatimTextOutput("coord"), uiOutput("All"))) ) ) )) #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({ plot(iris$Petal.Length,iris$Petal.Width,main = "K-MEANS",xlab="Petal Length",ylab = "Petal Width", 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) }) output$All <- renderUI({ tagList( fluidRow( column(4, sliderInput("slider1", label = h4("Clusters"), min = 3, max = 9, value = 3)), column(4, checkboxGroupInput("checkGroup", label = h4("Variable X"),names(iris), selected=names(iris)[[2]] )), column(4, selectInput("select", label = h4("Variable Y"), names(iris),selected=names(iris)[[2]] ))) ) }) Data <- reactive({iris[, c(input$select,input$checkGroup)] }) Clusters <- reactive({ kmeans(Data()) }) }) #################### # # # Exercise 7 # # # #################### #ui.R library(shiny) shinyUI(fluidPage( titlePanel("Shiny App"), sidebarLayout( sidebarPanel(h2("Menu"), br(), fluidRow( column(6, h4("Help Text"), helpText("Just for help"))), br(), fluidRow( column(6, numericInput("numer", label = h4("Numeric Input"), value = 10))), fluidRow( column(6, h4("Single Checkbox"), checkboxInput("checkbox", label = "Choice A", value = TRUE))), fluidRow( column(6, dateInput("date", label = h4("Date input"), value = "2016-12-01")), column(6 )), fluidRow( column(6, dateRangeInput("dates", label = h4("Date Range"))), column(6, textInput("text", label = h4("Text Input"), value = "Some Text"))), fileInput("file", label = h4("File Input"))), mainPanel(h1("Main"), img(src = "petal.jpg", height = 150, width = 200), br(), br(), p("This famous (Fisher's or Anderson's) ", a("iris",href="http://stat.ethz.ch/R-manual/R-devel/library/datasets/html/iris.html"), "data laid gives the measurements inwards centimeters of the variables sepal length as well as width as well as petal length as well as width, respectively, for l flowers from each of three species of iris. The species are ",strong( "Iris setosa,"),strong( "versicolor"), "and", strong("virginica.")), br(), h2("Analysis"), tabsetPanel(type="tabs",tabPanel("Data Table",dataTableOutput("Table")), tabPanel("Summary",dataTableOutput("Table2")), tabPanel("K means", plotOutput("plot1",click = "mouse"), verbatimTextOutput("coord"), uiOutput("All"))) ) ) )) #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({ plot(Data(),main = "K-MEANS", 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) }) output$All <- renderUI({ tagList( fluidRow( column(4, sliderInput("slider1", label = h4("Clusters"), min = 3, max = 10, value = 3)), column(4, checkboxGroupInput("checkGroup", label = h4("Variable X"),names(iris), selected=names(iris)[[2]] )), column(4, selectInput("select", label = h4("Variable Y"), names(iris),selected=names(iris)[[2]] ))) ) }) Data <- reactive({iris[, c(input$select,input$checkGroup)] }) Clusters <- reactive({ kmeans(Data(),input$slider1) }) }) #################### # # # Exercise 8 # # # #################### #ui.R library(shiny) shinyUI(fluidPage( titlePanel("Shiny App"), sidebarLayout( sidebarPanel(h2("Menu"), br(), fluidRow( column(6, h4("Help Text"), helpText("Just for help"))), br(), fluidRow( column(6, numericInput("numer", label = h4("Numeric Input"), value = 10))), fluidRow( column(6, h4("Single Checkbox"), checkboxInput("checkbox", label = "Choice A", value = TRUE))), fluidRow( column(6, dateInput("date", label = h4("Date input"), value = "2016-12-01")), column(6 )), fluidRow( column(6, dateRangeInput("dates", label = h4("Date Range"))), column(6, textInput("text", label = h4("Text Input"), value = "Some Text"))), fileInput("file", label = h4("File Input"))), mainPanel(h1("Main"), img(src = "petal.jpg", height = 150, width = 200), br(), br(), p("This famous (Fisher's or Anderson's) ", a("iris",href="http://stat.ethz.ch/R-manual/R-devel/library/datasets/html/iris.html"), "data laid gives the measurements inwards centimeters of the variables sepal length as well as width as well as petal length as well as width, respectively, for l flowers from each of three species of iris. The species are ",strong( "Iris setosa,"),strong( "versicolor"), "and", strong("virginica.")), br(), h2("Analysis"), tabsetPanel(type="tabs",tabPanel("Data Table",dataTableOutput("Table")), tabPanel("Summary",dataTableOutput("Table2")), tabPanel("K means", plotOutput("plot1",click = "mouse"), verbatimTextOutput("coord"), uiOutput("All"))) ) ) )) #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", 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) }) output$All <- renderUI({ tagList( fluidRow( column(4, sliderInput("slider1", label = h4("Clusters"), min = 1, max = 9, value = 4)), column(4, checkboxGroupInput("checkGroup", label = h4("Variable X"),names(iris), selected=names(iris)[[2]] )), column(4, selectInput("select", label = h4("Variable Y"), names(iris),selected=names(iris)[[2]] ))) ) }) Data <- reactive({iris[, c(input$select,input$checkGroup)] }) Clusters <- reactive({ kmeans(Data(),input$slider1) }) }) #################### # # # Exercise nine # # # #################### #ui.R library(shiny) shinyUI(fluidPage( titlePanel("Shiny App"), sidebarLayout( sidebarPanel(h2("Menu"), br(), fluidRow( column(6, h4("Help Text"), helpText("Just for help"))), br(), fluidRow( column(6, numericInput("numer", label = h4("Numeric Input"), value = 10))), fluidRow( column(6, h4("Single Checkbox"), checkboxInput("checkbox", label = "Choice A", value = TRUE))), fluidRow( column(6, dateInput("date", label = h4("Date input"), value = "2016-12-01")), column(6 )), fluidRow( column(6, dateRangeInput("dates", label = h4("Date Range"))), column(6, textInput("text", label = h4("Text Input"), value = "Some Text"))), fileInput("file", label = h4("File Input"))), mainPanel(h1("Main"), img(src = "petal.jpg", height = 150, width = 200), br(), br(), p("This famous (Fisher's or Anderson's) ", a("iris",href="http://stat.ethz.ch/R-manual/R-devel/library/datasets/html/iris.html"), "data laid gives the measurements inwards centimeters of the variables sepal length as well as width as well as petal length as well as width, respectively, for l flowers from each of three species of iris. The species are ",strong( "Iris setosa,"),strong( "versicolor"), "and", strong("virginica.")), br(), h2("Analysis"), tabsetPanel(type="tabs",tabPanel("Data Table",dataTableOutput("Table")), tabPanel("Summary",dataTableOutput("Table2")), tabPanel("K means", plotOutput("plot1",click = "mouse"), verbatimTextOutput("coord"), uiOutput("All"))) ) ) )) #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, 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) }) output$All <- renderUI({ tagList( fluidRow( column(4, sliderInput("slider1", label = h4("Clusters"), min = 1, max = 9, value = 4)), column(4, checkboxGroupInput("checkGroup", label = h4("Variable X"),names(iris), selected=names(iris)[[2]] )), column(4, selectInput("select", label = h4("Variable Y"), names(iris),selected=names(iris)[[2]] ))) ) }) Data <- reactive({iris[, c(input$select,input$checkGroup)] }) Clusters <- reactive({ kmeans(Data(),input$slider1) }) }) #################### # # # Exercise 10 # # # #################### #ui.R library(shiny) shinyUI(fluidPage( titlePanel("Shiny App"), sidebarLayout( sidebarPanel(h2("Menu"), br(), fluidRow( column(6, h4("Help Text"), helpText("Just for help"))), br(), fluidRow( column(6, numericInput("numer", label = h4("Numeric Input"), value = 10))), fluidRow( column(6, h4("Single Checkbox"), checkboxInput("checkbox", label = "Choice A", value = TRUE))), fluidRow( column(6, dateInput("date", label = h4("Date input"), value = "2016-12-01")), column(6 )), fluidRow( column(6, dateRangeInput("dates", label = h4("Date Range"))), column(6, textInput("text", label = h4("Text Input"), value = "Some Text"))), fileInput("file", label = h4("File Input"))), mainPanel(h1("Main"), img(src = "petal.jpg", height = 150, width = 200), br(), br(), p("This famous (Fisher's or Anderson's) ", a("iris",href="http://stat.ethz.ch/R-manual/R-devel/library/datasets/html/iris.html"), "data laid gives the measurements inwards centimeters of the variables sepal length as well as width as well as petal length as well as width, respectively, for l flowers from each of three species of iris. The species are ",strong( "Iris setosa,"),strong( "versicolor"), "and", strong("virginica.")), br(), h2("Analysis"), tabsetPanel(type="tabs",tabPanel("Data Table",dataTableOutput("Table")), tabPanel("Summary",dataTableOutput("Table2")), tabPanel("K means", plotOutput("plot1",click = "mouse"), verbatimTextOutput("coord"), uiOutput("All"))) ) ) )) #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) }) output$All <- renderUI({ tagList( fluidRow( column(4, sliderInput("slider1", label = h4("Clusters"), min = 1, max = 9, value = 4)), column(4, checkboxGroupInput("checkGroup", label = h4("Variable X"),names(iris), selected=names(iris)[[2]] )), column(4, selectInput("select", label = h4("Variable Y"), names(iris),selected=names(iris)[[2]] ))) ) }) Data <- reactive({iris[, c(input$select,input$checkGroup)] }) Clusters <- reactive({ kmeans(Data(),input$slider1) }) })
Sources:
http://www.r-exercises.com/2017/02/19/building-shiny-app-exercises-part-7/
http://www.r-exercises.com/2017/02/19/building-shiny-app-solutions-part-7/