Building Shiny App exercises part 7

Connect widgets & plots

In the seventh part of our journey we are ready to connect more of the widgets we created before with our k-means plot in order to totally control its output. Of cousre we will also reform the plot itself properly in order to make it a real k-means plot.
Read the examples below to understand the logic of what we are going to do and then test yous skills with the exercise set we prepared for you. Lets begin!

Answers to the exercises are available here.

If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.

First of all let’s move the widgets we are going to use from the `sidebarPanel` into the `mainPanel` and specifically under our `plot`.

Learn more about Shiny in the online course R Shiny Interactive Web Apps – Next Level Data Visualization. In this course you will learn how to create advanced Shiny web apps; embed video, pdfs and images; add focus and zooming tools; and many other functionalities (30 lectures, 3hrs.).

Exercise 1

Remove the `textInput` from your server.R file. Then place the `checkboxGroupInput` and the `selectInput` in the same row with the `sliderInput`. Name them “Variable X” and “Variable Y” respectively. HINT: Use `fluidrow` and `column`.

Create a reactive expression

Reactive expressions are expressions that can read reactive values and call other reactive expressions. Whenever a reactive value changes, any reactive expressions that depended on it are marked as “invalidated” and will automatically re-execute if necessary. If a reactive expression is marked as invalidated, any other reactive expressions that recently called it are also marked as invalidated. In this way, invalidations ripple through the expressions that depend on each other.
The reactive expression is activated like this: `example <- reactive({ })`

Exercise 2

Place a reactive expression in server.R, at any spot except inside `output\$All` and name it “Data”. HINT: Use `reactive`

Now let’s connect your `selectInput` with the variables of your dataset as in the example below.

#ui.R
`library(shiny) shinyUI(fluidPage( titlePanel("Shiny App"),`

sidebarLayout(
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 as inputs in your `selectInput` as “Variable Y” . HINT: Use` names`.

Exercise 4

Do the same for `checkboxGroupInput` and “Variable X”. HINT: Use` names`.

Select the fourth variabale as default like the example below.

#ui.R
`library(shiny) shinyUI(fluidPage( titlePanel("Shiny App"),`

sidebarLayout(
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 second variable the default choise for both widgets. HINT: Use `selected`.

Now follow the example below to create a new function and place there the automated function for k means calculation.

#ui.R
`library(shiny) shinyUI(fluidPage( titlePanel("Shiny App"),`

sidebarLayout(
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 put in there the function ` kmeans` which will be applied on the function `Data`. HINT: Use `reactive`.

Connect your plot with the widgets.

It is time to connect your plot with the widgets.

Exercise 7

Put `Data `inside `renderPlot` as first argument replacing the data that you have chosen to be plotted until now. Moreover delete `xlab` and `ylab`.

You gan change automatically the colours of your clusters by copying and pasting this part of code as first argument of `renderPlot `before the `plot` function:

`palette(c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3", "#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999"))`

We will choose to have up to nine clusters so we choose nine colours.

Exercise 8

Set `min` of your `sliderInput` to 1, `max` to 9 and `value` to 4 and use the `palette` function to give colours.

This is how you can give different colors to your clusters. To activate these colors put this part of code into your `plot` function.

`col = Clusters()\$cluster,`

Exercise 9

Activate the ` palette` function.

To make your clusters easily foundable you can fully color them by adding into `plot` function this:
`pch` = 20,` cex` = 3

Exercise 10

Fully color the `points` of your `plot`.

Building Shiny app solutions part 7

Below are the solutions to these exercises on Building Shiny App.

Learn more about Shiny in the online course R Shiny Interactive Web Apps – Next Level Data Visualization. In this course you will learn how to create advanced Shiny web apps; embed video, pdfs and images; add focus and zooming tools; and many other functionalities (30 lectures, 3hrs.).

```####################
#                  #
#    Exercise 1    #
#                  #
####################

#ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 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(
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 2    #
#                  #
####################

#ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 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(
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 3    #
#                  #
####################

#ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 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(
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(
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 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(
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 5    #
#                  #
####################

#ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 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(
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 6    #
#                  #
####################

#ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 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(
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(
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 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(
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(
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 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(
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 9    #
#                  #
####################

#ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 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(
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(
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 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(
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)
})
})
```

Building Shiny App exercises part 6

RENDER FUNCTIONS

In the sixth part of our series we will talk about the `renderPlot` and the `renderUI` function and then we will be ready to create our first visualization. (Find part 1-5 here).
We are going to create a simple interactive scatterplot that will help us see the clusters that are created when we run the k-means algorithm on our dataset. Read the examples below to understand how to activate a `renderPlot` function and the test yous skills with the exercise set we prepared for you. Lets begin!

Answers to the exercises are available here.

If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.

DESCRIPTIVE STATISTICS

As in every statistical application it is wise to apply descriptive statistics on your dataset and also provide this information to user in an easy-readable way. So, first of all we will place a Data Table inside the “SUMMARY” `tabPanel`. The example below can be your guide.

#ui.R
`library(shiny) shinyUI(fluidPage( sidebarLayout( sidebarPanel( ), mainPanel( dataTableOutput("Table") ) )))`
#server.R
`shinyServer(function(input, output, session) { sum<-as.data.frame.array(summary(iris)) output\$Table <- renderDataTable(sum) })`

Learn more about Shiny in the online course R Shiny Interactive Web Apps – Next Level Data Visualization. In this course you will learn how to create advanced Shiny web apps; embed video, pdfs and images; add focus and zooming tools; and many other functionalities (30 lectures, 3hrs.).

Exercise 1

Create a Data Table(“Table2”) with the descriptive statistics of your dataset. HINT: Use `summary`, `as.data.frame.array` and `renderDataTable`.

renderPlot

The `renderPlot` function enders a reactive plot that is suitable for assigning to an output slot. The general form of the function that generates the plot is below:

`renderPlot(expr, width = "auto", height = "auto", res = 72, ..., env = parent.frame(), quoted = FALSE, execOnResize = FALSE, outputArgs = list())`

The example below shows you how to create a simple scatterplot between two variables of the iris dataset(“Sepal Length” and “Sepal Width”).

# ui.R
`library(shiny) shinyUI(fluidPage( sidebarLayout( sidebarPanel( ), mainPanel( plotOutput("plot1") ) )))`
#server.R
`shinyServer(function(input, output, session) { output\$plot1 <- renderPlot({ plot(iris\$Sepal.Length,iris\$Sepal.Width) }) })`

Initially remove `renderImage` and `radioButtons` from the `tabPanel` “K means”.

Exercise 2

Add a scatterplot inside the `tabPanel` “K Means” between two variables of the iris dataset.

INTERACTIVE PLOTS

Shiny has built-in support for interacting with static plots generated by R’s base graphics functions,this makes it easy to add features like selecting points and regions, as well as zooming in and out of images.
To get the position of the mouse when a plot is clicked, you simply need to use the click option with the `plotOutput`. For example, this app will print out the x and y coordinate position of the mouse cursor when a click occurs.

#ui.R
`library(shiny) shinyUI(fluidPage( sidebarLayout( sidebarPanel(), mainPanel( plotOutput("plot1", click = "plot_click"), verbatimTextOutput("info") ) )))`
#server.R
`shinyServer(function(input, output, session) { output\$plot1 <- renderPlot({ plot(iris\$Sepal.Length,iris\$Sepal.Width) }) output\$info <- renderText({ paste0("x=", input\$plot_click\$x, "\ny=", input\$plot_click\$y) }) })`

Exercise 3

Add click inside the `plotOutput` you just created. Name it “mouse”.

Exercise 4

Add a `verbatimTextOutput` inside the “K Means” `tabPanel`,under the `plotOutput` you created before. Name it “coord”.

Exercise 5

Make “x” and “y” coordinates appear in the pre-tag you just created. HINT : Use `renderText` and `paste0` and do not forget to activate it with the `submitButton`.

Exercise 6

Set `height` = “auto” and `width` = “auto”.

PLOT ANNOTATION

This function can be used to add labels to a plot. Its first four principal arguments can also be used as arguments in most high-level plotting functions. They must be of type character or expression. In the latter case, quite a bit of mathematical notation is available such as sub- and superscripts, greek letters, fraction, etc.
`title(main = NULL, sub = NULL, xlab = NULL, ylab = NULL, line = NA, outer = FALSE, ...)`
Look at the example below:
# ui.R
`library(shiny) shinyUI(fluidPage( sidebarLayout( sidebarPanel(), mainPanel( plotOutput("plot1", click = "plot_click"), verbatimTextOutput("info") ) ))) `
#server.R
`shinyServer(function(input, output, session) { output\$plot1 <- renderPlot({ plot(iris\$Sepal.Length,iris\$Sepal.Width,main = "SCATTER PLOT",sub = "K Means",xlab="Sepal Length",ylab = "Sepal Width") }) output\$info <- renderText({ paste0("x=", input\$plot_click\$x, "\ny=", input\$plot_click\$y) }) })`

Exercise 7

Set scatterplot title to “K-Means”, the X-axis label to “Petal Length” and the Y-axis label to “Petal Width”. HINT: Use `main`,`xlab`,`ylab`.

You can also modify and set other graphical parameters related to the title and subtitle like the example below:

# ui.R
`library(shiny) shinyUI(fluidPage( sidebarLayout( sidebarPanel(), mainPanel( plotOutput("plot1", click = "plot_click"), verbatimTextOutput("info") ) ))) `
#server.R
`shinyServer(function(input, output, session) { output\$plot1 <- renderPlot({ plot(iris\$Sepal.Length,iris\$Sepal.Width,main = "SCATTER PLOT",sub = "K Means",xlab="Sepal Length",ylab = "Sepal Width", cex.main = 3, font.main= 5, col.main= "green", cex.sub = 0.65, font.sub = 4, col.sub = "orange") }) output\$info <- renderText({ paste0("x=", input\$plot_click\$x, "\ny=", input\$plot_click\$y) }) })`

Exercise 8

Give values to the rest of the graphical parameters of the title like the example above and get used to them. HINT: Use `cex.main`, `font.main` and `col.main`.

renderUI

`renderUI(expr, env = parent.frame(), quoted = FALSE, outputArgs = list())`

Makes a reactive version of a function that generates HTML using the Shiny UI library. As you can see in the example below this expression returns a tag object.

# ui.R
`library(shiny) shinyUI(fluidPage( sidebarLayout( sidebarPanel( uiOutput("Controls")), mainPanel( plotOutput("plot1", click = "plot_click"), verbatimTextOutput("info") ) ))) `
#server.R
`shinyServer(function(input, output, session) { output\$plot1 <- renderPlot({ plot(iris\$Sepal.Length,iris\$Sepal.Width,main = "SCATTER PLOT",sub = "K Means",xlab="Sepal Length",ylab = "Sepal Width", cex.main = 2, font.main= 4, col.main= "blue", cex.sub = 0.75, font.sub = 3, col.sub = "red") }) output\$info <- renderText({ paste0("x=", input\$plot_click\$x, "\ny=", input\$plot_click\$y) }) output\$Controls <- renderUI({ tagList( sliderInput("n", "N", 1, 1000, 500), textInput("label", "Label") ) }) })`

Exercise 9

Put a `uiOutput` inside `tabPanel `“K-Means” and name it “All”. Then create its output in server.R with a `tagList` into it. HINT: Use `uiOutput`, `renderUI` and `tagList`.

Exercise 10

Remove the `submitButton` and move the `sliderInput` and the `textOutput` from the ui.R into the `tagList`.

Building Shiny App solutions part 6

Below are the solutions to these exercises on Building Shiny App.

Learn more about Shiny in the online course R Shiny Interactive Web Apps – Next Level Data Visualization. In this course you will learn how to create advanced Shiny web apps; embed video, pdfs and images; add focus and zooming tools; and many other functionalities (30 lectures, 3hrs.).

```####################
#                  #
#    Exercise 1    #
#                  #
####################

library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
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,
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2)),

column(6,
selectInput("select", label = h4("Select Box"),
choices = list("Choice 1" = 1, "Choice 2" = 2
), selected = 1))),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 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",
choices = list("Choice 1" = 1, "Choice 2" = 2),
selected = 1), imageOutput("Image"),
sliderInput("slider1", label = h4("Clusters"),
min = 3, max = 10, value = 3),
textOutput("text1"),
submitButton("Submit")))

)
)
))
#server.R
shinyServer(function(input, output) {
output\$Table <- renderDataTable(
iris,options = list(
pageLength = 10))
output\$Image <- renderImage({
filename <- normalizePath(file.path('./images',
list(src = filename,
width=300,
height=200)

},deleteFile = FALSE)
output\$text1 <- renderText({
paste("You have selected", input\$slider1,"clusters")
})
sumiris<-as.data.frame.array(summary(iris))
output\$Table2 <- renderDataTable(sumiris)})
####################
#                  #
#    Exercise 2    #
#                  #
####################

#ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
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,
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2)),

column(6,
selectInput("select", label = h4("Select Box"),
choices = list("Choice 1" = 1, "Choice 2" = 2
), selected = 1))),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 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"),
sliderInput("slider1", label = h4("Clusters"),
min = 3, max = 10, value = 3),
textOutput("text1"),
submitButton("Submit")))

)
)
))
#server.R
shinyServer(function(input, output) {
output\$Table <- renderDataTable(
iris,options = list(
pageLength = 10))

output\$text1 <- renderText({
paste("You have selected", input\$slider1,"clusters")
})
sumiris<-as.data.frame.array(summary(iris))
output\$Table2 <- renderDataTable(sumiris)
output\$plot1 <- renderPlot({
plot(iris\$Petal.Length,iris\$Petal.Width)
})
})

####################
#                  #
#    Exercise 3    #
#                  #
####################

#ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
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,
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2)),

column(6,
selectInput("select", label = h4("Select Box"),
choices = list("Choice 1" = 1, "Choice 2" = 2
), selected = 1))),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 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"),
sliderInput("slider1", label = h4("Clusters"),
min = 3, max = 10, value = 3),
textOutput("text1"),
submitButton("Submit")))

)
)
))
#server.R
shinyServer(function(input, output) {
output\$Table <- renderDataTable(
iris,options = list(
pageLength = 10))

output\$text1 <- renderText({
paste("You have selected", input\$slider1,"clusters")
})
sumiris<-as.data.frame.array(summary(iris))
output\$Table2 <- renderDataTable(sumiris)
output\$plot1 <- renderPlot({
plot(iris\$Petal.Length,iris\$Petal.Width)
})
})

####################
#                  #
#    Exercise 4    #
#                  #
####################

#ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
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,
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2)),

column(6,
selectInput("select", label = h4("Select Box"),
choices = list("Choice 1" = 1, "Choice 2" = 2
), selected = 1))),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 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"),
sliderInput("slider1", label = h4("Clusters"),
min = 3, max = 10, value = 3),
textOutput("text1"),
submitButton("Submit")))

)
)
))
#server.R
shinyServer(function(input, output) {
output\$Table <- renderDataTable(
iris,options = list(
pageLength = 10))

output\$text1 <- renderText({
paste("You have selected", input\$slider1,"clusters")
})
sumiris<-as.data.frame.array(summary(iris))
output\$Table2 <- renderDataTable(sumiris)
output\$plot1 <- renderPlot({
plot(iris\$Petal.Length,iris\$Petal.Width)
})
})

####################
#                  #
#    Exercise 5    #
#                  #
####################

#ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
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,
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2)),

column(6,
selectInput("select", label = h4("Select Box"),
choices = list("Choice 1" = 1, "Choice 2" = 2
), selected = 1))),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 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"),
sliderInput("slider1", label = h4("Clusters"),
min = 3, max = 10, value = 3),
textOutput("text1"),
submitButton("Submit")))

)
)
))
#server.R
shinyServer(function(input, output) {
output\$Table <- renderDataTable(
iris,options = list(
pageLength = 10))

output\$text1 <- renderText({
paste("You have selected", input\$slider1,"clusters")
})
sumiris<-as.data.frame.array(summary(iris))
output\$Table2 <- renderDataTable(sumiris)
output\$plot1 <- renderPlot({
plot(iris\$Petal.Length,iris\$Petal.Width)
})
output\$coord <- renderText({
paste0("x=", input\$mouse\$x, "\ny=", input\$mouse\$y)
})
})

####################
#                  #
#    Exercise 6    #
#                  #
####################
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
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,
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2)),

column(6,
selectInput("select", label = h4("Select Box"),
choices = list("Choice 1" = 1, "Choice 2" = 2
), selected = 1))),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 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"),
sliderInput("slider1", label = h4("Clusters"),
min = 3, max = 10, value = 3),
textOutput("text1"),
submitButton("Submit")))

)
)
))
#server.R
shinyServer(function(input, output) {
output\$Table <- renderDataTable(
iris,options = list(
pageLength = 10))

output\$text1 <- renderText({
paste("You have selected", input\$slider1,"clusters")
})
sumiris<-as.data.frame.array(summary(iris))
output\$Table2 <- renderDataTable(sumiris)
output\$plot1 <- renderPlot({
plot(iris\$Petal.Length,iris\$Petal.Width)
}, width = "auto",height = "auto")
output\$coord <- renderText({
paste0("x=", input\$mouse\$x, "\ny=", input\$mouse\$y)
})
})

####################
#                  #
#    Exercise 7    #
#                  #
####################

#ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
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,
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2)),

column(6,
selectInput("select", label = h4("Select Box"),
choices = list("Choice 1" = 1, "Choice 2" = 2
), selected = 1))),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 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"),
sliderInput("slider1", label = h4("Clusters"),
min = 3, max = 10, value = 3),
textOutput("text1"),
submitButton("Submit")))

)
)
))
#server.R
shinyServer(function(input, output) {
output\$Table <- renderDataTable(
iris,options = list(
pageLength = 10))

output\$text1 <- renderText({
paste("You have selected", input\$slider1,"clusters")
})
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")
}, width = "auto",height = "auto")
output\$coord <- renderText({
paste0("x=", input\$mouse\$x, "\ny=", input\$mouse\$y)
})
})

####################
#                  #
#    Exercise 8    #
#                  #
####################

#ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
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,
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2)),

column(6,
selectInput("select", label = h4("Select Box"),
choices = list("Choice 1" = 1, "Choice 2" = 2
), selected = 1))),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 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"),
sliderInput("slider1", label = h4("Clusters"),
min = 3, max = 10, value = 3),
textOutput("text1"),
submitButton("Submit")))

)
)
))
#server.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
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,
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2)),

column(6,
selectInput("select", label = h4("Select Box"),
choices = list("Choice 1" = 1, "Choice 2" = 2
), selected = 1))),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 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"),
sliderInput("slider1", label = h4("Clusters"),
min = 3, max = 10, value = 3),
textOutput("text1"),
submitButton("Submit")))

)
)
))

####################
#                  #
#    Exercise 9    #
#                  #
####################

#ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
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,
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2)),

column(6,
selectInput("select", label = h4("Select Box"),
choices = list("Choice 1" = 1, "Choice 2" = 2
), selected = 1))),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 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"),
sliderInput("slider1", label = h4("Clusters"),
min = 3, max = 10, value = 3),
textOutput("text1"),
submitButton("Submit")))

)
)
))
#server.R
shinyServer(function(input, output) {
output\$Table <- renderDataTable(
iris,options = list(
pageLength = 10))

output\$text1 <- renderText({
paste("You have selected", input\$slider1,"clusters")
})
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(

)
})
})

####################
#                  #
#    Exercise 10   #
#                  #
####################

#ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
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,
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2)),

column(6,
selectInput("select", label = h4("Select Box"),
choices = list("Choice 1" = 1, "Choice 2" = 2
), selected = 1))),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 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(
pageLength = 10))

output\$text1 <- renderText({
paste("You have selected", input\$slider1,"clusters")
})
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(
sliderInput("slider1", label = h4("Clusters"),
min = 3, max = 10, value = 3),
textOutput("text1")

)
})
})
```

Building Shiny App Exercises (part 5)

RENDER FUNCTIONS
In the fourth part of our series we just “scratched the surface” of reactivity by analyzing some of the properties of the `renderTable` function.
Now it is time to get deeper and learn how to use the rest of the render functions that shiny provides. As you were told in part 4 these are:

`renderImage`
`renderPlot`
`renderPrint`
`renderText`
`renderUI`

Below you will see the functionality of three of them (`renderImage`, `renderText` and `renderPrint`) and then we will be ready to use those of them that match our needs in the next parts, just like the widgets and give a specific form to our application. As you will probably understand, when reading this part our aim is to perform several statistical analyses on our dataset. We will start by creating a K-Means `tabPanel`.

Follow the examples to understand the logic of the tools you are going to use and then enhance the app you started creating in part 1 by practising with the exercise set we prepared for you. Lets begin!

Answers to the exercises are available here.

If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.

renderImage
Sending pre-rendered images with `renderImage`.
These are images saved as a link to a source file. If your Shiny app has pre-rendered images saved in a subdirectory, you can send them using `renderImage`. Suppose the images are in the subdirectory “www”/, and are named “image1.png”, “image2.png”, and so on. The following code would send the appropriate image, depending on the value of `input\$n`:

# ui.R
`library(shiny) shinyUI(fluidPage( titlePanel("RenderImage"), sidebarLayout( sidebarPanel( radioButtons("n", label = h4("Radio Buttons"), choices = list("Choice 1" = 1, "Choice 2" = 2), selected = 2) ), mainPanel( imageOutput("Image") ) )`

))
#server.R

`shinyServer(function(input, output, session) { # Send a pre-rendered image, and don't delete the image after sending it output\$Image <- renderImage({ # When input\$n is 3, filename is ./images/image3.jpeg filename <- normalizePath(file.path('./www', paste('image', input\$n, '.png', sep='')))`

# Return a list containining the filename
list(src = filename)

}, deleteFile = FALSE)
})

Now let’s break down what the code above exactly does. First of all as we saw in part 1 you should save your images in a subdirectory called “www” inside the directory that you work. Let’s say you save 2 images and you name them “image1” and “image2”. As you can see we use `radioButtons`here to select which one of the two we want to be displayed. The `filename` contains the exact output path of the images while the `list` contains the `filename` along with some other values.
In this example, `deleteFile` is FALSE because we don’t want Shiny to delete an image after sending it.

Learn more about Shiny in the online course R Shiny Interactive Web Apps – Next Level Data Visualization. In this course you will learn how to create advanced Shiny web apps; embed video, pdfs and images; add focus and zooming tools; and many other functionalities (30 lectures, 3hrs.).

Exercise 1

Place a `tabPanel` in the `tabsetPanel` of your Shiny App. Name it “K Means”.

Exercise 2

Move the `radioButtons` from the `sidebarPanel` inside the `tabPanel` “K Means” you just created and name it “Select Image”. Also, move the `submitButton` from the `sidebarPanel` to the `tabPanel` “K Means” without title.

Exercise 3

Place an `imageOutput` inside the `tabPanel` “K Means” with name “Image” (ui.R) and the reactive function of it (server.R). Still nothing happens. HINT: Use `renderImage`.

Create a subdirectory inside the directory you work and name it “images”. Put there two images with names “pic1” and “pic2” respectively and .png ending.

Exercise 4

Now create the `filename`. Follow the example above to create the right path. Do not forget to connect it with the `radioButtons`. Two steps left.

Exercise 5

Now it is time to set `deleteFile` = “FALSE”.

Exercise 6

Create the `list` that contains the `filename`.

Exercise 7

Set` width` = 300 and `height` = 200 into the `list`.

renderText-renderPrint

The example below shows how the ` renderText` works.

#ui.R
`library(shiny) shinyUI(fluidPage( titlePanel("RenderImage"), sidebarLayout( sidebarPanel( sliderInput("slider1", label = h4("Sliders"), min =3 , max = 10, value =3) ), mainPanel( textOutput("text1") ) ) ))`
#server.R
`shinyServer(function(input, output, session) {`

output\$text1 <- renderText({
paste(“You have selected”, input\$slider1,”clusters”)
})
})

The code above takes a numeric value from the `sliderInput` and puts it in the exact place of our sentence in the `mainPanel`.

Before proceeding to the next exercise move the `sliderInput` from the `sidebarPanel` just after the `imageOutput` in the `tabPanel `“K Means”. Then change its name to “Clusters”, its `min` to 3, its `max` to 10 and `value` to 3.

Exercise 8

Put the `textOutpout` named “text1” inside your` tabPanel` exactly after the `sliderInput`, then place its reactive function inside server.R using `renderText`.

Exercise 9

Display the reactive output by putting inside the `renderText` function the sentence “You have selected”,(?),”clusters.” HINT : Use `paste`.

Exercise 10

Follow exactly the same steps but this time instead of `renderText` use `renderPrint` and note the difference.

Building Shiny App solutions part 5

Below are the solutions to these exercises on Building Shiny App.

```####################
#                  #
#    Exercise 1    #
#                  #
####################

#ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
column(6,
h4("Help Text"),
helpText("Just for help"))),
br(),
fluidRow(
column(6,
h4("Submitbutton"),
submitButton("Submit")),
column(6,
numericInput("numer",
label = h4("Numeric Input"),
value = 10))),
fluidRow(
column(6,
h4("Single Checkbox"),
checkboxInput("checkbox", label = "Choice A", value = TRUE)),
column(6,
choices = list("Choice 1" = 1, "Choice 2" = 2),
selected = 2))),
fluidRow(
column(6,
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2)),

column(6,
selectInput("select", label = h4("Select Box"),
choices = list("Choice 1" = 1, "Choice 2" = 2
), selected = 1))),
fluidRow(
column(6,
dateInput("date",
label = h4("Date input"),
value = "2016-12-01")),
column(6,
sliderInput("slider1", label = h4("Sliders"),
min = 0, max = 100, value = c(10,90)))),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 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"),
tabPanel("K means"))

)
)
))
#server.R
shinyServer(function(input, output) {
output\$Table <- renderDataTable(
iris,options = list(
pageLength = 10))
})

####################
#                  #
#    Exercise 2    #
#                  #
####################

#ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
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,
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2)),

column(6,
selectInput("select", label = h4("Select Box"),
choices = list("Choice 1" = 1, "Choice 2" = 2
), selected = 1))),
fluidRow(
column(6,
dateInput("date",
label = h4("Date input"),
value = "2016-12-01")),
column(6,
sliderInput("slider1", label = h4("Sliders"),
min = 0, max = 100, value = c(10,90)))),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 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"),
tabPanel("K means",
choices = list("Choice 1" = 1, "Choice 2" = 2),
selected = 1),submitButton("Submit")))

)
)
))
#server.R
shinyServer(function(input, output) {
output\$Table <- renderDataTable(
iris,options = list(
pageLength = 10))

})

####################
#                  #
#    Exercise 3    #
#                  #
####################

#ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
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,
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2)),

column(6,
selectInput("select", label = h4("Select Box"),
choices = list("Choice 1" = 1, "Choice 2" = 2
), selected = 1))),
fluidRow(
column(6,
dateInput("date",
label = h4("Date input"),
value = "2016-12-01")),
column(6,
sliderInput("slider1", label = h4("Sliders"),
min = 0, max = 100, value = c(10,90)))),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 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"),
tabPanel("K means",
choices = list("Choice 1" = 1, "Choice 2" = 2),
selected = 1), imageOutput("Image"),submitButton("Submit")))

)
)
))
#server.R
shinyServer(function(input, output) {
output\$Table <- renderDataTable(
iris,options = list(
pageLength = 10))
output\$Image <- renderImage({})
})

####################
#                  #
#    Exercise 4    #
#                  #
####################

#ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
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,
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2)),

column(6,
selectInput("select", label = h4("Select Box"),
choices = list("Choice 1" = 1, "Choice 2" = 2
), selected = 1))),
fluidRow(
column(6,
dateInput("date",
label = h4("Date input"),
value = "2016-12-01")),
column(6,
sliderInput("slider1", label = h4("Sliders"),
min = 0, max = 100, value = c(10,90)))),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 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"),
tabPanel("K means",
choices = list("Choice 1" = 1, "Choice 2" = 2),
selected = 1), imageOutput("Image"),submitButton("Submit")))

)
)
))
#server.R
shinyServer(function(input, output) {
output\$Table <- renderDataTable(
iris,options = list(
pageLength = 10))
output\$Image <- renderImage({
filename <- normalizePath(file.path('./images',

})
})
####################
#                  #
#    Exercise 5    #
#                  #
####################

#ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
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,
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2)),

column(6,
selectInput("select", label = h4("Select Box"),
choices = list("Choice 1" = 1, "Choice 2" = 2
), selected = 1))),
fluidRow(
column(6,
dateInput("date",
label = h4("Date input"),
value = "2016-12-01")),
column(6,
sliderInput("slider1", label = h4("Sliders"),
min = 0, max = 100, value = c(10,90)))),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 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"),
tabPanel("K means",
choices = list("Choice 1" = 1, "Choice 2" = 2),
selected = 1), imageOutput("Image"),submitButton("Submit")))

)
)
))
#server.R
shinyServer(function(input, output) {
output\$Table <- renderDataTable(
iris,options = list(
pageLength = 10))
output\$Image <- renderImage({
filename <- normalizePath(file.path('./images',

},deleteFile = FALSE)
})

####################
#                  #
#    Exercise 6    #
#                  #
####################

#ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
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,
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2)),

column(6,
selectInput("select", label = h4("Select Box"),
choices = list("Choice 1" = 1, "Choice 2" = 2
), selected = 1))),
fluidRow(
column(6,
dateInput("date",
label = h4("Date input"),
value = "2016-12-01")),
column(6,
sliderInput("slider1", label = h4("Sliders"),
min = 0, max = 100, value = c(10,90)))),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 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"),
tabPanel("K means",
choices = list("Choice 1" = 1, "Choice 2" = 2),
selected = 1), imageOutput("Image"),submitButton("Submit")))

)
)
))
#server.R
shinyServer(function(input, output) {
output\$Table <- renderDataTable(
iris,options = list(
pageLength = 10))
output\$Image <- renderImage({
filename <- normalizePath(file.path('./images',
list(src = filename)

},deleteFile = FALSE)
})

####################
#                  #
#    Exercise 7    #
#                  #
####################

#ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
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,
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2)),

column(6,
selectInput("select", label = h4("Select Box"),
choices = list("Choice 1" = 1, "Choice 2" = 2
), selected = 1))),
fluidRow(
column(6,
dateInput("date",
label = h4("Date input"),
value = "2016-12-01")),
column(6,
sliderInput("slider1", label = h4("Sliders"),
min = 0, max = 100, value = c(10,90)))),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 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"),
tabPanel("K means",
choices = list("Choice 1" = 1, "Choice 2" = 2),
selected = 1), imageOutput("Image"),submitButton("Submit")))

)
)
))
#server.R
shinyServer(function(input, output) {
output\$Table <- renderDataTable(
iris,options = list(
pageLength = 10))
output\$Image <- renderImage({
filename <- normalizePath(file.path('./images',
list(src = filename,
width=300,
height=200)

},deleteFile = FALSE)
})

####################
#                  #
#    Exercise 8    #
#                  #
####################

ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
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,
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2)),

column(6,
selectInput("select", label = h4("Select Box"),
choices = list("Choice 1" = 1, "Choice 2" = 2
), selected = 1))),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 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"),
tabPanel("K means",
choices = list("Choice 1" = 1, "Choice 2" = 2),
selected = 1), imageOutput("Image"),
sliderInput("slider1", label = h4("Clusters"),
min = 3, max = 10, value = 3),
textOutput("text1"),
submitButton("Submit")))

)
)
))
#server.R
shinyServer(function(input, output) {
output\$Table <- renderDataTable(
iris,options = list(
pageLength = 10))
output\$Image <- renderImage({
filename <- normalizePath(file.path('./images',
list(src = filename,
width=300,
height=200)

},deleteFile = FALSE)
output\$text1 <- renderText({})
})

####################
#                  #
#    Exercise 9    #
#                  #
####################

#ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
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,
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2)),

column(6,
selectInput("select", label = h4("Select Box"),
choices = list("Choice 1" = 1, "Choice 2" = 2
), selected = 1))),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 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"),
tabPanel("K means",
choices = list("Choice 1" = 1, "Choice 2" = 2),
selected = 1), imageOutput("Image"),
sliderInput("slider1", label = h4("Clusters"),
min = 3, max = 10, value = 3),
textOutput("text1"),
submitButton("Submit")))

)
)
))
#server.R
shinyServer(function(input, output) {
output\$Table <- renderDataTable(
iris,options = list(
pageLength = 10))
output\$Image <- renderImage({
filename <- normalizePath(file.path('./images',
list(src = filename,
width=300,
height=200)

},deleteFile = FALSE)
output\$text1 <- renderText({
paste("You have selected", input\$slider1,"clusters")
})
})

####################
#                  #
#    Exercise 10   #
#                  #
####################

#ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
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,
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2)),

column(6,
selectInput("select", label = h4("Select Box"),
choices = list("Choice 1" = 1, "Choice 2" = 2
), selected = 1))),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 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"),
tabPanel("K means",
choices = list("Choice 1" = 1, "Choice 2" = 2),
selected = 1), imageOutput("Image"),
sliderInput("slider1", label = h4("Clusters"),
min = 3, max = 10, value = 3),
textOutput("text1"),
submitButton("Submit")))

)
)
))
#server.R
shinyServer(function(input, output) {
output\$Table <- renderDataTable(
iris,options = list(
pageLength = 10))
output\$Image <- renderImage({
filename <- normalizePath(file.path('./images',
list(src = filename,
width=300,
height=200)

},deleteFile = FALSE)
output\$text1 <- renderPrint({
paste("You have selected", input\$slider1,"clusters")
})
})
```

Building Shiny App exercises part 4

APPLICATION LAYOUT & REACTIVITY
The fourth part of our series is “separated” into two “sub-parts”. In the first one we will start building the skeleton of our application by using `tabsetPanel`. This is how we will separate the sections of our app and also organize its structure better.
In the second part you will learn hot to load your dataset in RStudio and finally in the third one we will give life to your Shiny App! Specifically, you are going to have your first contact with reactivity and learn how to build reactive output to display in your Shiny app in a form of a data table initially.
Follow the examples below to understand the logic of the tools you are going to use and then enhance the app you started creating in part 1 by practising with the exercise set we prepared for you. Lets begin!

Answers to the exercises are available here.

If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.

Learn more about Shiny in the online course R Shiny Interactive Web Apps – Next Level Data Visualization. In this course you will learn how to create advanced Shiny web apps; embed video, pdfs and images; add focus and zooming tools; and many other functionalities (30 lectures, 3hrs.).

TABSET PANEL
In the example below you will see hot to add a `tabsetPanel` in your shiny app.

# ui.R
`library(shiny) fluidPage( titlePanel("TabPanel"), sidebarLayout( sidebarPanel(h3("Menu")), mainPanel(h3("Main Panel"),tabsetPanel(type = "tabs")) ))`

#server.R
`library(shiny) shinyServer(function(input, output) { })`

Exercise 1

Add a `tabsetPanel` to the `mainPanel` of your Shiny App.

TAB PANEL
In the example below you will see how to add `tabPanel` in your `tabsetPanel`.

# ui.R
`library(shiny) fluidPage( titlePanel("TabPanel"), sidebarLayout( sidebarPanel(h3("Menu")), mainPanel(h3("Main Panel"),tabsetPanel(type = "tabs", tabPanel("Tab Panel 1") ))))`

#server.R
`library(shiny) shinyServer(function(input, output) { })`

Exercise 2

Place a `tabPanel` in the `tabsetPanel` you just added to your Shiny App. Name it “Data Table”.

Exercise 3

Put a second `tabPanel` next to the first one. Name it “Summary”.

Now it is time to give your app a purpose of existence. This can happen with only one way. To add some data into it! As we told in part 1 we will create an application based on the famous (Fisher’s or Anderson’s) iris data set which gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are Iris setosa, versicolor, and virginica.
This is a “built in” dataset of RStudio that will help us create our first application.
But normally you want to analyze your own datasets. The first thing you should do in order to perform any kind of analysis on them is to load them properly. We will now see how to load a dataset in txt form from a local file using RStudio. Let’s go!
The process is quite simple. First of all you have to place the txt file that contains your dataset into the same directory that you are working, secondly press the “Import Dataset” button in RStudio and then “From Local File…”. Find the txt file in your computer and click “Open”, then press “Import”. That’s all! Your dataset is properly loaded in your directory and now you can work with it.

Exercise 4

Load the dataset you want to analyze (“something.txt”) from your computer to your directory with RStudio buttons.

INTRODUCTION TO REACTIVITY
From this point you are going to enter in the “Kingdom of Reactivity”. Reactive output automatically responds when your user interacts with a widget. You can create reactive output by following two steps. Firstly, add an R object to your ui.R. and then tell Shiny how to build the object in server.R.

1): Add an R object to the UI

Shiny provides a variety of functions that transform R objects into output for your UI as you can see below:

`htmlOutput`: raw HTML
`imageOutput`: image
`plotOutput`: plot
`tableOutput`: table
`textOutput`: text
`uiOutput`: raw HTML
`verbatimTextOutput`: text

To add output to the UI place the `output` function inside `sidebarPanel` or `mainPanel` in the ui.R script.
For example, the ui.R file below uses `tableOutput` to add a reactive line of text to “Tab Panel 1”. Nothing happens…for the moment!

# ui.R
`library(shiny) fluidPage( titlePanel("TabPanel"), sidebarLayout( sidebarPanel(h3("Menu")), mainPanel(h3("Main Panel"),tabsetPanel(type = "tabs", tabPanel("Tab Panel 1",dataTableOutput("dt1")), tabPanel("Tab Panel 2")) )))`

#server.R
`library(shiny) shinyServer(function(input, output) { })`

Notice that `datatableOutput` takes an argument, the character string “dt1”. Each of the *`Output` functions require a character string that Shiny will use as the name of your reactive element. Users cannot see it and you will understand its role later.

Exercise 5

Add a `datatableOutput` to “Data Table”, name its argument “Table”.

2): Provide R code to build the object.

The code should be placed in the function that appears inside `shinyServer` in your server.R script.
This function is of great importance as it builds a list-like object named output that contains all of the code needed to update the R objects in your app. Be careful, each R object MUST have its own entry in the list.
You can create an entry by defining a new element for output within the function. The element name should match the name of the reactive element that you created in ui.R.
Each entry should contain the output of one of Shiny’s `render`* functions. Each `render`* function corresponds to a specific type of reactive object. You can find them below:

`renderImage`: images (saved as a link to a source file)
`renderPlot`: plots
`renderPrint`: any printed output
`renderTable`: data frame, matrix, other table like structures
`renderText`: character strings
`renderUI`: a Shiny tag object or HTML

Each `render`* function takes a single argument: an R expression which can either be one simple line of text, or it can involve many lines of code.
In the example below “dt1” is attached to the `output` expression in server.R and gives us the Data Table of “iris” dataset inside “Tab Panel 1”.

# ui.R
`library(shiny) fluidPage( titlePanel("TabPanel"), sidebarLayout( sidebarPanel(h3("Menu")), mainPanel(h3("Main Panel"),tabsetPanel(type = "tabs", tabPanel("Tab Panel 1",dataTableOutput("dt1")), tabPanel("Tab Panel 2")) )))`

#server.R
`library(shiny) shinyServer(function(input, output) { output\$dt1 <- renderDataTable( iris) `
})

Exercise 6

Add the appropriate `render`* function to server.R in order to create the Data table of the “iris” dataset. Hint: Use the `output` expression.

The `dataTableOutput` is your first contact with reactivity, in the next parts of our series you will use the rest of the Output functions that Shiny provides. But for now let’s experiment a little bit on this.
As you can see there is a text filter in your Data Table. You can deactivate it by setting `searching` to be “FALSE” as the example below.

# ui.R
`library(shiny) fluidPage( titlePanel("TabPanel"), sidebarLayout( sidebarPanel(h3("Menu")), mainPanel(h3("Main Panel"),tabsetPanel(type = "tabs", tabPanel("Tab Panel 1",dataTableOutput("dt1")), tabPanel("Tab Panel 2")) )))`

#server.R
`library(shiny) shinyServer(function(input, output) { output\$dt1 <- renderDataTable( iris,options = list(searching=FALSE)) })`

Exercise 7

Disable the Text Filter of your Data Table. Hint: Use `options`, `list` and `searching`.

With the same logic you can disable the pagination that is displayed in your Data Table, as in the example below.

# ui.R

`library(shiny) fluidPage( titlePanel("TabPanel"), sidebarLayout( sidebarPanel(h3("Menu")), mainPanel(h3("Main Panel"),tabsetPanel(type = "tabs", tabPanel("Tab Panel 1",dataTableOutput("dt1")), tabPanel("Tab Panel 2")) )))`

#server.R
`library(shiny) shinyServer(function(input, output) { output\$dt1 <- renderDataTable( iris,options = list(searching=FALSE,paging=FALSE)) })`

Exercise 8

Disable the Pagination of your Data Table. Hint: Use `options`, `list`, `paging`.
Now you can see how to display an exact number of rows (15) and enable filtering again.

# ui.R

`library(shiny) fluidPage( titlePanel("TabPanel"), sidebarLayout( sidebarPanel(h3("Menu")), mainPanel(h3("Main Panel"),tabsetPanel(type = "tabs", tabPanel("Tab Panel 1",dataTableOutput("dt1")), tabPanel("Tab Panel 2")) )))`

#server.R
`library(shiny) shinyServer(function(input, output) { output\$dt1 <- renderDataTable( iris,options = list(pageLength=15)) })`

Exercise 9

Enable filtering again and set the exact number of rows that are displayed to 10. Hint: Use `options`, `list`, `pageLength`

We can also create a Length Menu in order to control totally the choices of the numbers of rows we want to be displayed. In the example below we assign every number to a menu label. 5 -> ‘5’, 10 -> ’10’, 15 -> ’15’,-1 -> ‘ALL’.

# ui.R
`library(shiny) fluidPage( titlePanel("TabPanel"), sidebarLayout( sidebarPanel(h3("Menu")), mainPanel(h3("Main Panel"),tabsetPanel(type = "tabs", tabPanel("Tab Panel 1",dataTableOutput("dt1")), tabPanel("Tab Panel 2")) )))`

#server.R
`library(shiny) shinyServer(function(input, output) { output\$dt1 <- renderDataTable( iris,options = list( lengthMenu = list(c(5, 15, 25,-1),c('5','15','25','ALL')), pageLength = 15)) })`

Exercise 10

Create a Length Menu with values (10,20,30,-1) and assign each one ot the values to the appropriate menu label. Hint: Use `options`, `list`, `lengthMenu`,`pageLength`.

Building Shiny App solutions part 4

Below are the solutions to these exercises on Building Shiny App.

Learn more about Shiny in the online course R Shiny Interactive Web Apps – Next Level Data Visualization. In this course you will learn how to create advanced Shiny web apps; embed video, pdfs and images; add focus and zooming tools; and many other functionalities (30 lectures, 3hrs.).

```####################
#                  #
#    Exercise 1    #
#                  #
####################

#ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
column(6,
h4("Help Text"),
helpText("Just for help"))),
br(),
fluidRow(
column(6,
h4("Submitbutton"),
submitButton("Submit")),
column(6,
numericInput("numer",
label = h4("Numeric Input"),
value = 10))),
fluidRow(
column(6,
h4("Single Checkbox"),
checkboxInput("checkbox", label = "Choice A", value = TRUE)),
column(6,
choices = list("Choice 1" = 1, "Choice 2" = 2),
selected = 2))),
fluidRow(
column(6,
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2)),

column(6,
selectInput("select", label = h4("Select Box"),
choices = list("Choice 1" = 1, "Choice 2" = 2
), selected = 1))),
fluidRow(
column(6,
dateInput("date",
label = h4("Date input"),
value = "2016-12-01")),
column(6,
sliderInput("slider1", label = h4("Sliders"),
min = 0, max = 100, value = c(10,90)))),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are ",strong( "Iris setosa,"),strong( "versicolor"), "and", strong("virginica.")),
br(),
h2("Analysis"),
tabsetPanel(type="tabs")

)
)
))

####################
#                  #
#    Exercise 2    #
#                  #
####################

#ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
column(6,
h4("Help Text"),
helpText("Just for help"))),
br(),
fluidRow(
column(6,
h4("Submitbutton"),
submitButton("Submit")),
column(6,
numericInput("numer",
label = h4("Numeric Input"),
value = 10))),
fluidRow(
column(6,
h4("Single Checkbox"),
checkboxInput("checkbox", label = "Choice A", value = TRUE)),
column(6,
choices = list("Choice 1" = 1, "Choice 2" = 2),
selected = 2))),
fluidRow(
column(6,
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2)),

column(6,
selectInput("select", label = h4("Select Box"),
choices = list("Choice 1" = 1, "Choice 2" = 2
), selected = 1))),
fluidRow(
column(6,
dateInput("date",
label = h4("Date input"),
value = "2016-12-01")),
column(6,
sliderInput("slider1", label = h4("Sliders"),
min = 0, max = 100, value = c(10,90)))),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are ",strong( "Iris setosa,"),strong( "versicolor"), "and", strong("virginica.")),
br(),
h2("Analysis"),
tabsetPanel(type="tabs",tabPanel("Data Table"))

)
)
))

####################
#                  #
#    Exercise 3    #
#                  #
####################

#ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
column(6,
h4("Help Text"),
helpText("Just for help"))),
br(),
fluidRow(
column(6,
h4("Submitbutton"),
submitButton("Submit")),
column(6,
numericInput("numer",
label = h4("Numeric Input"),
value = 10))),
fluidRow(
column(6,
h4("Single Checkbox"),
checkboxInput("checkbox", label = "Choice A", value = TRUE)),
column(6,
choices = list("Choice 1" = 1, "Choice 2" = 2),
selected = 2))),
fluidRow(
column(6,
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2)),

column(6,
selectInput("select", label = h4("Select Box"),
choices = list("Choice 1" = 1, "Choice 2" = 2
), selected = 1))),
fluidRow(
column(6,
dateInput("date",
label = h4("Date input"),
value = "2016-12-01")),
column(6,
sliderInput("slider1", label = h4("Sliders"),
min = 0, max = 100, value = c(10,90)))),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are ",strong( "Iris setosa,"),strong( "versicolor"), "and", strong("virginica.")),
br(),
h2("Analysis"),
tabsetPanel(type="tabs",tabPanel("Data Table"),tabPanel("Summary"))

)
)
))

####################
#                  #
#    Exercise 4    #
#                  #
####################

#Place your .txt file in the same directory that you are working -> "Import Dataset" -> "From Local File..." -> "Open" -> "Import"

####################
#                  #
#    Exercise 5    #
#                  #
####################

#ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
column(6,
h4("Help Text"),
helpText("Just for help"))),
br(),
fluidRow(
column(6,
h4("Submitbutton"),
submitButton("Submit")),
column(6,
numericInput("numer",
label = h4("Numeric Input"),
value = 10))),
fluidRow(
column(6,
h4("Single Checkbox"),
checkboxInput("checkbox", label = "Choice A", value = TRUE)),
column(6,
choices = list("Choice 1" = 1, "Choice 2" = 2),
selected = 2))),
fluidRow(
column(6,
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2)),

column(6,
selectInput("select", label = h4("Select Box"),
choices = list("Choice 1" = 1, "Choice 2" = 2
), selected = 1))),
fluidRow(
column(6,
dateInput("date",
label = h4("Date input"),
value = "2016-12-01")),
column(6,
sliderInput("slider1", label = h4("Sliders"),
min = 0, max = 100, value = c(10,90)))),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 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"))

)
)
))

####################
#                  #
#    Exercise 6    #
#                  #
####################

#server.R
shinyServer(function(input, output) {
output\$Table <- renderDataTable(
iris)
})

####################
#                  #
#    Exercise 7    #
#                  #
####################

#server.R
shinyServer(function(input, output) {
output\$Table <- renderDataTable(
iris,options = list(searching=FALSE))
})

####################
#                  #
#    Exercise 8    #
#                  #
####################

#server.R
shinyServer(function(input, output) {
output\$Table <- renderDataTable(
iris,options = list(searching=FALSE,paging=FALSE))
})

####################
#                  #
#    Exercise 9    #
#                  #
####################

#server.R
shinyServer(function(input, output) {
output\$Table <- renderDataTable(
iris,options = list(pageLength=10))
})

####################
#                  #
#    Exercise 10   #
#                  #
####################

#server.R
shinyServer(function(input, output) {
output\$Table <- renderDataTable(
iris,options = list(
pageLength = 10))
})
```

Building Shiny App exercises part 3

Welcome to the third part of our series. In this part you will learn how to build and place inside your app the rest of the widgets which were mentioned in part 2.
More specifically we will analyze: 1) `helptext`, 2) `numericInput`, 3) `radioButtons`, 4) `selectInput`, 5) `sliderInput` and 6) `textInput`.
As you already know from part 2 reactivity will be added in the upcoming parts of our series so this is something that you do not have to worry about.
Follow the examples below to understand the logic behind the widgets’ functions and then enhance the app you created in part 1 by practising with the exercise set we prepared for you.
Firstly, we will add all the kinds of the widgets in our app, for educational reasons and later we will decide which of them is practical to keep.Let’s start!

Answers to the exercises are available here.

If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.

To begin with let’s create the space inside our `sidebarPanel` in order to put in there the rest of our widgets.

Learn more about Shiny in the online course R Shiny Interactive Web Apps – Next Level Data Visualization. In this course you will learn how to create advanced Shiny web apps; embed video, pdfs and images; add focus and zooming tools; and many other functionalities (30 lectures, 3hrs.).

Exercise 1

Use the function `fluidrow` to make sure that all the elements we are going to use will be in the same line. To do this put `fluidrow` just under the “Menu” in your sidebarPanel and close its parenthesis just before the `submibutton` (excluding the two` br`).

HELP TEXT

In the example below we create a UI with a `helpText`.
# ui.R
`shinyUI(fluidPage( titlePanel("Widgets"), h3("Help Text"), helpText("Text that is used to provide some extra details to the user.")))`

# server.R
`shinyServer(function(input, output) { })`

Exercise 2

Place a `helpText` exactly under the `actionButton`, name it “Help Text” and as text add:”For help”. Hint: Use `h4`.

Exercise 3

Now use `column` function in order to decide the column width for every row and put the two widgets in the same line. To do this place the `column` function twice. Firstly place it just before the “Actionbutton” title with width = 6 and close its parenthesis exactly after the `label` “Perform”. Do the same for the `helpInput`. Both of the `column` functions must be inside the same `fluidrow`.

NUMERIC INPUT

In the example below we create a UI with a `numericInput`.
# ui.R
`shinyUI(fluidPage( titlePanel("Widgets"), numericInput("num", label = h3("Numeric Input"), value = 1) ))`

# server.R
`shinyServer(function(input, output) { })`

Exercise 4

Put a `numericInput` under `helpText`,in the same row with `submitButton`. Name it “numer”, give it “Numeric Input” as `label` and `value` = 10. Hint: Use `h4`, `fluidrow` and `column`.

In the example below we create a UI with a `radioButtons`.
#ui.R
`shinyUI(fluidPage( titlePanel("Widgets"), radioButtons("radio", label = h3("Radio buttons"), choices = list("Choice 1" = 1, "Choice 2" = 2, "Choice 3" = 3),selected = 1) ))`

#server.R
`shinyServer(function(input, output) { })`

Exercise 5

Add `radioButtons` under `numericInput`, in the same row with `checkBoxInput`. Name it “radiobuttons”, put as `label` “Radio Buttons” and give it two choices with no default. Hint: Use` h4`, `fluidrow`, `column` and `choices`.

Exercise 6

Now put “2” as the default of the choices. Hint: Use `selected`.

SELECT INPUT

In the example below we create a UI with a `selectInput`.
# ui.R
`shinyUI(fluidPage( titlePanel("Widgets"), selectInput("select", label = h3("Select Box"), choices = list("Choice 1" = 1, "Choice 2" = 2, "Choice 3" = 3), selected = 1) ))`

#server.R
`shinyServer(function(input, output) { })`

Exercise 7
Place under `radiobuttons` and in the same row with `checkBoxGroupInput` a `selectinput`. Its `name` should be “select”, its `label` “Select Box” and you should give it two `choices` with the second one as default. Hint: Use `h4`, `fluidrow`, `column`, `choices` and `selected`.

SLIDER INPUT

In the example below we create a UI with two `sliderInput`.
# ui.R
`shinyUI(fluidPage( titlePanel("Widgets"), sliderInput("slider1", label = h3("Sliders"), min = 0, max = 10, value = 5), sliderInput("slider2", "", min = 0, max = 10, value = c(3, 7)) ))`

#server.R
`shinyServer(function(input, output) { })`

Exercise 8

Under the `selectInput` and in the same row with the `dateInput` place a `sliderInput` with `name` = slider1, `label` = “Sliders”, `min` = 0, `max` = 100 and `value` =50. Hint: Use `fluidrow`, `columns` and `h4`.

Exercise 9

Replace the `value` with a default range “10-90” and see the difference.

TEXT INPUT

In the example below we create a UI with a `textInput`.
# ui.R
`shinyUI(fluidPage( titlePanel("Widgets"), textInput("text", label = h3("Text Input"), value = "Text...")) )`

#server.R
`shinyServer(function(input, output) { })`

Exercise 10

Finally put a `textInput` under `sliderInput` and in the same row with the `dateRangeInput`. Name it “text”, put as `label` “Text Input” and as `value` “Some Text”. Hint: Use `fluidrow`, `column` and `h4`.

Building Shiny App solutions part 3

Below are the solutions to these exercises on Building Shiny App.

Learn more about Shiny in the online course R Shiny Interactive Web Apps – Next Level Data Visualization. In this course you will learn how to create advanced Shiny web apps; embed video, pdfs and images; add focus and zooming tools; and many other functionalities (30 lectures, 3hrs.).

```####################
#                  #
#    Exercise 1    #
#                  #
####################

# ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
h4("Actionbutton"),
actionButton("per", label = "Perform")),
br(),
h4("Submitbutton"),
submitButton("Submit"),
h4("Single Checkbox"),
checkboxInput("checkbox", label = "Choice A", value = TRUE),
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2),
dateInput("date",
label = h4("Date input"),
value = "2016-12-01"),
dateRangeInput("dates", label = h4("Date Range")),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are ",strong( "Iris setosa,"),strong( "versicolor"), "and", strong("virginica.")),
br(),
h2("Analysis")

)
)
))

####################
#                  #
#    Exercise 2    #
#                  #
####################

# ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
h4("Actionbutton"),
actionButton("per", label = "Perform"),
h4("Help Text"),
helpText("Just for help")),
br(),
h4("Submitbutton"),
submitButton("Submit"),
h4("Single Checkbox"),
checkboxInput("checkbox", label = "Choice A", value = TRUE),
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2),
dateInput("date",
label = h4("Date input"),
value = "2016-12-01"),
dateRangeInput("dates", label = h4("Date Range")),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are ",strong( "Iris setosa,"),strong( "versicolor"), "and", strong("virginica.")),
br(),
h2("Analysis")

)
)
))

####################
#                  #
#    Exercise 3    #
#                  #
####################

# ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
column(6,
h4("Help Text"),
helpText("Just for help"))),
br(),
h4("Submitbutton"),
submitButton("Submit"),
h4("Single Checkbox"),
checkboxInput("checkbox", label = "Choice A", value = TRUE),
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2),
dateInput("date",
label = h4("Date input"),
value = "2016-12-01"),
dateRangeInput("dates", label = h4("Date Range")),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are ",strong( "Iris setosa,"),strong( "versicolor"), "and", strong("virginica.")),
br(),
h2("Analysis")

)
)
))

####################
#                  #
#    Exercise 4    #
#                  #
####################

# ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
column(6,
h4("Help Text"),
helpText("Just for help"))),
br(),
fluidRow(
column(6,
h4("Submitbutton"),
submitButton("Submit")),
column(6,
numericInput("numer",
label = h4("Numeric Input"),
value = 10))),
h4("Single Checkbox"),
checkboxInput("checkbox", label = "Choice A", value = TRUE),
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2),
dateInput("date",
label = h4("Date input"),
value = "2016-12-01"),
dateRangeInput("dates", label = h4("Date Range")),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are ",strong( "Iris setosa,"),strong( "versicolor"), "and", strong("virginica.")),
br(),
h2("Analysis")

)
)
))

####################
#                  #
#    Exercise 5    #
#                  #
####################

# ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
column(6,
h4("Help Text"),
helpText("Just for help"))),
br(),
fluidRow(
column(6,
h4("Submitbutton"),
submitButton("Submit")),
column(6,
numericInput("numer",
label = h4("Numeric Input"),
value = 10))),
fluidRow(
column(6,
h4("Single Checkbox"),
checkboxInput("checkbox", label = "Choice A", value = TRUE)),
column(6,
choices = list("Choice 1" = 1, "Choice 2" = 2)
))),
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2),
dateInput("date",
label = h4("Date input"),
value = "2016-12-01"),
dateRangeInput("dates", label = h4("Date Range")),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are ",strong( "Iris setosa,"),strong( "versicolor"), "and", strong("virginica.")),
br(),
h2("Analysis")

)
)
))

####################
#                  #
#    Exercise 6    #
#                  #
####################

# ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
column(6,
h4("Help Text"),
helpText("Just for help"))),
br(),
fluidRow(
column(6,
h4("Submitbutton"),
submitButton("Submit")),
column(6,
numericInput("numer",
label = h4("Numeric Input"),
value = 10))),
fluidRow(
column(6,
h4("Single Checkbox"),
checkboxInput("checkbox", label = "Choice A", value = TRUE)),
column(6,
choices = list("Choice 1" = 1, "Choice 2" = 2),
selected = 2))),
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2),
dateInput("date",
label = h4("Date input"),
value = "2016-12-01"),
dateRangeInput("dates", label = h4("Date Range")),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are ",strong( "Iris setosa,"),strong( "versicolor"), "and", strong("virginica.")),
br(),
h2("Analysis")

)
)
))

####################
#                  #
#    Exercise 7    #
#                  #
####################

# ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
column(6,
h4("Help Text"),
helpText("Just for help"))),
br(),
fluidRow(
column(6,
h4("Submitbutton"),
submitButton("Submit")),
column(6,
numericInput("numer",
label = h4("Numeric Input"),
value = 10))),
fluidRow(
column(6,
h4("Single Checkbox"),
checkboxInput("checkbox", label = "Choice A", value = TRUE)),
column(6,
choices = list("Choice 1" = 1, "Choice 2" = 2),
selected = 2))),
fluidRow(
column(6,
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2)),
column(6,
selectInput("select", label = h4("Select Box"),
choices = list("Choice 1" = 1, "Choice 2" = 2
), selected = 1))),
dateInput("date",
label = h4("Date input"),
value = "2016-12-01"),
dateRangeInput("dates", label = h4("Date Range")),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are ",strong( "Iris setosa,"),strong( "versicolor"), "and", strong("virginica.")),
br(),
h2("Analysis")

)
)
))

####################
#                  #
#    Exercise 8    #
#                  #
####################

# ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
column(6,
h4("Help Text"),
helpText("Just for help"))),
br(),
fluidRow(
column(6,
h4("Submitbutton"),
submitButton("Submit")),
column(6,
numericInput("numer",
label = h4("Numeric Input"),
value = 10))),
fluidRow(
column(6,
h4("Single Checkbox"),
checkboxInput("checkbox", label = "Choice A", value = TRUE)),
column(6,
choices = list("Choice 1" = 1, "Choice 2" = 2),
selected = 2))),
fluidRow(
column(6,
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2)),

column(6,
selectInput("select", label = h4("Select Box"),
choices = list("Choice 1" = 1, "Choice 2" = 2
), selected = 1))),
fluidRow(
column(6,
dateInput("date",
label = h4("Date input"),
value = "2016-12-01")),
column(6,
sliderInput("slider1", label = h4("Sliders"),
min = 0, max = 100, value = 50))),
dateRangeInput("dates", label = h4("Date Range")),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are ",strong( "Iris setosa,"),strong( "versicolor"), "and", strong("virginica.")),
br(),
h2("Analysis")

)
)
))

####################
#                  #
#    Exercise 9    #
#                  #
####################

# ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
column(6,
h4("Help Text"),
helpText("Just for help"))),
br(),
fluidRow(
column(6,
h4("Submitbutton"),
submitButton("Submit")),
column(6,
numericInput("numer",
label = h4("Numeric Input"),
value = 10))),
fluidRow(
column(6,
h4("Single Checkbox"),
checkboxInput("checkbox", label = "Choice A", value = TRUE)),
column(6,
choices = list("Choice 1" = 1, "Choice 2" = 2),
selected = 2))),
fluidRow(
column(6,
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2)),

column(6,
selectInput("select", label = h4("Select Box"),
choices = list("Choice 1" = 1, "Choice 2" = 2
), selected = 1))),
fluidRow(
column(6,
dateInput("date",
label = h4("Date input"),
value = "2016-12-01")),
column(6,
sliderInput("slider1", label = h4("Sliders"),
min = 0, max = 100, value = c(10,90)))),
dateRangeInput("dates", label = h4("Date Range")),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are ",strong( "Iris setosa,"),strong( "versicolor"), "and", strong("virginica.")),
br(),
h2("Analysis")

)
)
))

####################
#                  #
#    Exercise 10   #
#                  #
####################

# ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),

sidebarLayout(
br(),
fluidRow(
column(6,
h4("Actionbutton"),
actionButton("per", label = "Perform")),
column(6,
h4("Help Text"),
helpText("Just for help"))),
br(),
fluidRow(
column(6,
h4("Submitbutton"),
submitButton("Submit")),
column(6,
numericInput("numer",
label = h4("Numeric Input"),
value = 10))),
fluidRow(
column(6,
h4("Single Checkbox"),
checkboxInput("checkbox", label = "Choice A", value = TRUE)),
column(6,
choices = list("Choice 1" = 1, "Choice 2" = 2),
selected = 2))),
fluidRow(
column(6,
checkboxGroupInput("checkGroup",
label = h4("Checkbox group"),
choices = list("Choice 1" = 1,
"Choice 2" = 2, "Choice 3" = 3),
selected = 2)),

column(6,
selectInput("select", label = h4("Select Box"),
choices = list("Choice 1" = 1, "Choice 2" = 2
), selected = 1))),
fluidRow(
column(6,
dateInput("date",
label = h4("Date input"),
value = "2016-12-01")),
column(6,
sliderInput("slider1", label = h4("Sliders"),
min = 0, max = 100, value = c(10,90)))),
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 set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are ",strong( "Iris setosa,"),strong( "versicolor"), "and", strong("virginica.")),
br(),
h2("Analysis")

)
)
))
```