ADD CONTROL WIDGETS
In the instant component of our serial you lot volition come across how to add together command widgets inwards your Shiny app. Widget is a spider web chemical ingredient that your users tin interact with. The widgets provided past times Shiny are:
FUNCTIONS
actionButton
: Action ButtoncheckboxGroupInput
: Influenza A virus subtype H5N1 grouping of cheque boxescheckboxInput
: Influenza A virus subtype H5N1 unmarried cheque boxdateInput
: Influenza A virus subtype H5N1 calendar for appointment selectiondateRangeInput
: Influenza A virus subtype H5N1 span of calendars for selecting a appointment rangefileInput
: Influenza A virus subtype H5N1 file upload command wizardhelpText
: Help text that tin hold upward added to an input formnumericInput
: Influenza A virus subtype H5N1 champaign to acquire inwards numbersradioButtons
: Influenza A virus subtype H5N1 laid of radio buttonsselectInput
: Influenza A virus subtype H5N1 box amongst choices to direct fromsliderInput
: Influenza A virus subtype H5N1 slider barsubmitButton
: Influenza A virus subtype H5N1 submit buttontextInput
: Influenza A virus subtype H5N1 champaign to acquire inwards text ADDING WIDGETS
You tin add together widgets to your spider web page inwards the same agency that you lot added other types of HTML content in part 1.
To add together a widget to your app, house a widget business office in
The showtime ii arguments for each widget are a raise for the widget which volition hold upward a graphic symbol string that the user volition non see, but you lot tin work it to alter the widget’s value too a label which volition seem amongst the widget inwards your app too it should hold upward a graphic symbol string too.
The residue of the arguments vary from widget to widget, depending on what the widget needs to hold upward functional. They may hold upward initial values, ranges, too increments.
Follow the examples below to empathise the logic behind the widgets’ functions too and hence elevate the app you lot created in part 1 by practising amongst the practice laid nosotros prepared for you.
Firstly, nosotros volition add together all the kinds of the widgets to our app, for educational reasons too after nosotros volition determine which of them is practical to keep.
Note that nosotros volition but add together the buttons inwards this part. Reactivity volition hold upward added to them inwards a few chapters. Lets begin!
You tin add together widgets to your spider web page inwards the same agency that you lot added other types of HTML content in part 1.
To add together a widget to your app, house a widget business office in
sidebarPanel
or in mainPanel
in your ui.R file.The showtime ii arguments for each widget are a raise for the widget which volition hold upward a graphic symbol string that the user volition non see, but you lot tin work it to alter the widget’s value too a label which volition seem amongst the widget inwards your app too it should hold upward a graphic symbol string too.
The residue of the arguments vary from widget to widget, depending on what the widget needs to hold upward functional. They may hold upward initial values, ranges, too increments.
Follow the examples below to empathise the logic behind the widgets’ functions too and hence elevate the app you lot created in part 1 by practising amongst the practice laid nosotros prepared for you.
Firstly, nosotros volition add together all the kinds of the widgets to our app, for educational reasons too after nosotros volition determine which of them is practical to keep.
Note that nosotros volition but add together the buttons inwards this part. Reactivity volition hold upward added to them inwards a few chapters. Lets begin!
Answers to the exercises are available here.
If you lot obtained a unlike (correct) answer than those listed on the solutions page, delight experience costless to postal service your answer every bit a comment on that page.
Exercise 1
Open the app you lot created in part 1 and motion the icon from
sidebarPanel
to mainPanel
, acquire out ii rows nether the title
“Main”, position the icon in that location too alter its dimensions to: height
= 150 and width
= 200. HINT: Use br
. BUTTONS
In the instance below nosotros do a UI amongst a
# ui.R
shinyUI(fluidPage(
titlePanel(“Widgets”),
In the instance below nosotros do a UI amongst a
submitButton
and an actionButton
. Please banknote that nosotros work the function fluidrow
to brand certain that all the elements nosotros are going to work volition hold upward inwards the same trouble every bit nosotros are going to bespeak this inwards the adjacent parts:# ui.R
shinyUI(fluidPage(
titlePanel(“Widgets”),
fluidRow(h3(“Buttons”),
actionButton(“action”, label = “Action”),
br(),
br(),
submitButton(“Submit”))))
actionButton(“action”, label = “Action”),
br(),
br(),
submitButton(“Submit”))))
# server.R
shinyServer(function(input, output) {
})
shinyServer(function(input, output) {
})
Exercise 2
Leave a row too house an
actionButton
under the championship “Menu” in sidebarPanel
, hand it the championship “Actionbutton”, name
= “per” and label
= “Perform”. HINT: Use br
and h4
. Exercise 3
Leave a row from the
actionButton
you but placed too add together a submitButton
with title
= “Submitbutton” and name
= “Submit”. HINT: Use br
and h4
. SINGLE CHECKBOX
In the instance below nosotros do a UI amongst a unmarried Checkbox:
# ui.R
shinyUI(fluidPage(
titlePanel(“Widgets”),
In the instance below nosotros do a UI amongst a unmarried Checkbox:
# ui.R
shinyUI(fluidPage(
titlePanel(“Widgets”),
fluidRow(h3(“Single checkbox”),
checkboxInput(“checkbox”, label = “Choice A”, value = TRUE))))
checkboxInput(“checkbox”, label = “Choice A”, value = TRUE))))
#server.R
shinyServer(function(input, output) {
})
shinyServer(function(input, output) {
})
Exercise 4
Add a
checkboxInput
in the sidebarPanel
under the submitButton
, position every bit championship “Single Checkbox”, raise it “checbox”, raise the label
“Choice A” too laid the value
to “TRUE”. HINT: Use h4
. Exercise 5
Change the value to “FALSE” to empathise the difference.
CHECKBOX GROUP
In the instance below nosotros do a UI amongst a Checkbox Group:
#ui.R
shinyUI(fluidPage(
checkboxGroupInput(“checkGroup”,
label = h3(“Checkbox group”),
choices = list(“Choice 1” = 1,
“Choice 2” = 2, “Choice 3” = 3),
selected = 2)
))
In the instance below nosotros do a UI amongst a Checkbox Group:
#ui.R
shinyUI(fluidPage(
checkboxGroupInput(“checkGroup”,
label = h3(“Checkbox group”),
choices = list(“Choice 1” = 1,
“Choice 2” = 2, “Choice 3” = 3),
selected = 2)
))
#server.R
shinyServer(function(input, output) {
})
shinyServer(function(input, output) {
})
Exercise 6
Place a
checkboxGroupInput
under the checkboxInput
, hand it championship “Checkbox Group”, raise it “checkGroup”, raise the label
“Checkbox Group” too hand it 3 choices
. HINT: Use h4
Exercise 7
Make the instant of the
choices
the default one. DATE INPUT
In the instance below nosotros do a UI amongst a Date Input:
#ui.R
shinyUI(fluidPage(
dateInput(“date”,
label = h3(“Date input”),
value = “2016-12-07”)
))
In the instance below nosotros do a UI amongst a Date Input:
#ui.R
shinyUI(fluidPage(
dateInput(“date”,
label = h3(“Date input”),
value = “2016-12-07”)
))
#server.R
shinyServer(function(input, output) {
})
shinyServer(function(input, output) {
})
Exercise 8
Under the c
heckboxGroupInput
add a dateInput
with name
= “date”, label
= “Date Input” and value
= “2016-12-01”. DATE RANGE
In the instance below nosotros do a UI amongst a Date Range Input:
#ui.R
shinyUI(fluidPage(
dateRangeInput(“dates”, label = h3(“Date range”))
))
In the instance below nosotros do a UI amongst a Date Range Input:
#ui.R
shinyUI(fluidPage(
dateRangeInput(“dates”, label = h3(“Date range”))
))
#server.R
shinyServer(function(input, output) {
})
shinyServer(function(input, output) {
})
Exercise 9
Under the
dateInput
place a dateRangeInput
with name
= “dates” and label
= “Date Range”. HINT: Use h4
. FILE INPUT
In the instance below nosotros do a UI amongst a File Input.
#ui.R
shinyUI(fluidPage(
fileInput(“file”, label = h3(“File input”))
))
In the instance below nosotros do a UI amongst a File Input.
#ui.R
shinyUI(fluidPage(
fileInput(“file”, label = h3(“File input”))
))
#server.R
shinyServer(function(input, output) {
})
shinyServer(function(input, output) {
})
Exercise 10
Under the
dateRangeInput
place a fileInput
. Name it “file” too hand it the label
“File Input”. _____________________________________
Below are the solutions to these exercises on Building Shiny App.
#################### # # # Exercise 1 # # # #################### #ui.R library(shiny) shinyUI(fluidPage( titlePanel("Shiny App"), sidebarLayout( sidebarPanel(h2("Menu") ), mainPanel(h1("Main"), br(), br(), img(src = "petal.jpg", height = 150, width = 200), p("This famous (Fisher's or Anderson's) ", a("iris",href="http://stat.ethz.ch/R-manual/R-devel/library/datasets/iris.html"), "data laid gives the measurements inwards centimeters of the variables sepal length too width too petal length too width, respectively, for fifty flowers from each of three 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( sidebarPanel(h2("Menu"), br(), h4("Actionbutton"), actionButton("per", label = "Perform") ), mainPanel(h1("Main"), img(src = "petal.jpg", height = 150, width = 200), p("This famous (Fisher's or Anderson's) ", a("iris",href="http://stat.ethz.ch/R-manual/R-devel/library/datasets/html/iris.html"), "data laid gives the measurements inwards centimeters of the variables sepal length too width too petal length too width, respectively, for fifty flowers from each of three species of iris. The species are ",strong( "Iris setosa,"),strong( "versicolor"), "and", strong("virginica.")), br(), h2("Analysis") ) ))) #################### # # # Exercise three # # # #################### #ui.R library(shiny) shinyUI(fluidPage( titlePanel("Shiny App"), sidebarLayout( sidebarPanel(h2("Menu"), br(), h4("Actionbutton"), actionButton("per", label = "Perform"), br(), h4("Submitbutton"), submitButton("Submit") ), mainPanel(h1("Main"), img(src = "petal.jpg", height = 150, width = 200), br(), br(), p("This famous (Fisher's or Anderson's) ", a("iris",href="http://stat.ethz.ch/R-manual/R-devel/library/datasets/html/iris.html"), "data laid gives the measurements inwards centimeters of the variables sepal length too width too petal length too width, respectively, for fifty flowers from each of three species of iris. The species are ",strong( "Iris setosa,"),strong( "versicolor"), "and", strong("virginica.")), br(), h2("Analysis") ) ))) #################### # # # Exercise iv # # # #################### #ui.R library(shiny) shinyUI(fluidPage( titlePanel("Shiny App"), sidebarLayout( sidebarPanel(h2("Menu"), br(), h4("Actionbutton"), actionButton("per", label = "Perform"), br(), h4("Submitbutton"), submitButton("Submit"), h4("Single Checkbox"), checkboxInput("checkbox", label = "Choice A", value = TRUE) ), mainPanel(h1("Main"), img(src = "petal.jpg", height = 150, width = 200), br(), br(), p("This famous (Fisher's or Anderson's) ", a("iris",href="http://stat.ethz.ch/R-manual/R-devel/library/datasets/html/iris.html"), "data laid gives the measurements inwards centimeters of the variables sepal length too width too petal length too width, respectively, for fifty flowers from each of three species of iris. The species are ",strong( "Iris setosa,"),strong( "versicolor"), "and", strong("virginica.")), br(), h2("Analysis") ) ))) #################### # # # Exercise v # # # #################### #ui.R library(shiny) shinyUI(fluidPage( titlePanel("Shiny App"), sidebarLayout( sidebarPanel(h2("Menu"), br(), h4("Actionbutton"), actionButton("per", label = "Perform"), br(), h4("Submitbutton"), submitButton("Submit"), h4("Single Checkbox"), checkboxInput("checkbox", label = "Choice A", value = FALSE) ), mainPanel(h1("Main"), img(src = "petal.jpg", height = 150, width = 200), br(), br(), p("This famous (Fisher's or Anderson's) ", a("iris",href="http://stat.ethz.ch/R-manual/R-devel/library/datasets/html/iris.html"), "data laid gives the measurements inwards centimeters of the variables sepal length too width too petal length too width, respectively, for fifty flowers from each of three species of iris. The species are ",strong( "Iris setosa,"),strong( "versicolor"), "and", strong("virginica.")), br(), h2("Analysis") ) ))) #################### # # # Exercise vi # # # #################### #ui.R library(shiny) shinyUI(fluidPage( titlePanel("Shiny App"), sidebarLayout( sidebarPanel(h2("Menu"), br(), 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) )), mainPanel(h1("Main"), img(src = "petal.jpg", height = 150, width = 200), br(), br(), p("This famous (Fisher's or Anderson's) ", a("iris",href="http://stat.ethz.ch/R-manual/R-devel/library/datasets/html/iris.html"), "data laid gives the measurements inwards centimeters of the variables sepal length too width too petal length too width, respectively, for fifty flowers from each of three species of iris. The species are ",strong( "Iris setosa,"),strong( "versicolor"), "and", strong("virginica.")), br(), h2("Analysis") ) ))) #################### # # # Exercise seven # # # #################### #ui.R library(shiny) shinyUI(fluidPage( titlePanel("Shiny App"), sidebarLayout( sidebarPanel(h2("Menu"), br(), 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)), mainPanel(h1("Main"), img(src = "petal.jpg", height = 150, width = 200), br(), br(), p("This famous (Fisher's or Anderson's) ", a("iris",href="http://stat.ethz.ch/R-manual/R-devel/library/datasets/html/iris.html"), "data laid gives the measurements inwards centimeters of the variables sepal length too width too petal length too width, respectively, for fifty flowers from each of three 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( sidebarPanel(h2("Menu"), br(), 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")), mainPanel(h1("Main"), img(src = "petal.jpg", height = 150, width = 200), br(), br(), p("This famous (Fisher's or Anderson's) ", a("iris",href="http://stat.ethz.ch/R-manual/R-devel/library/datasets/html/iris.html"), "data laid gives the measurements inwards centimeters of the variables sepal length too width too petal length too width, respectively, for fifty flowers from each of three species of iris. The species are ",strong( "Iris setosa,"),strong( "versicolor"), "and", strong("virginica.")), br(), h2("Analysis") ) ))) #################### # # # Exercise ix # # # #################### #ui.R library(shiny) shinyUI(fluidPage( titlePanel("Shiny App"), sidebarLayout( sidebarPanel(h2("Menu"), br(), 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"))), mainPanel(h1("Main"), img(src = "petal.jpg", height = 150, width = 200), br(), br(), p("This famous (Fisher's or Anderson's) ", a("iris",href="http://stat.ethz.ch/R-manual/R-devel/library/datasets/html/iris.html"), "data laid gives the measurements inwards centimeters of the variables sepal length too width too petal length too width, respectively, for fifty flowers from each of three 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( sidebarPanel(h2("Menu"), br(), 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 laid gives the measurements inwards centimeters of the variables sepal length too width too petal length too width, respectively, for fifty flowers from each of three species of iris. The species are ",strong( "Iris setosa,"),strong( "versicolor"), "and", strong("virginica.")), br(), h2("Analysis") ) )))
Source:
http://www.r-exercises.com/2016/12/18/building-shiny-app-solutions-part-2/
http://www.r-exercises.com/2016/12/18/building-shiny-app-exercises-part-2/