Branches of mechanical engineering: Edifice Shiny App Exercises + Solutions - Purpose Iv - R




APPLICATION LAYOUT & REACTIVITY
The 4th constituent of our serial is “separated” into ii “sub-parts”. In the commencement i nosotros volition start edifice the skeleton of our application past times using tabsetPanel. This is how nosotros volition split the sections of our app in addition to also organize its construction better.
In the 2d constituent you lot volition larn hot to charge your dataset inward RStudio in addition to live inward the 3rd i nosotros volition laissez passer on life to your Shiny App! Specifically, you lot are going to get got your commencement contact alongside reactivity in addition to larn how to construct reactive output to display inward your Shiny app inward a shape of a information tabular array initially.
Follow the examples below to empathize the logic of the tools you lot are going to utilization in addition to hence heighten the app you lot started creating in part 1 by practising alongside the do laid nosotros prepared for you. Lets begin!
Answers to the exercises are available here.
If you lot obtained a dissimilar (correct) respond than those listed on the solutions page, delight experience costless to postal service your respond every bit a comment on that page.
Learn more about Shiny inward the online course R Shiny Interactive Web Apps – Next Level Data Visualization. In this course of written report you lot volition larn how to do advanced Shiny spider web apps; embed video, pdfs in addition to images; add together focus in addition to zooming tools; in addition to many other functionalities (30 lectures, 3hrs.).
TABSET PANEL
In the instance below you lot volition meet hot to add together 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 instance below you lot volition meet 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 but added to your Shiny App. Name it “Data Table”.
Exercise 3
Put a second tabPanel next to the commencement one. Name it “Summary”.
LOAD DATASET
Now it is fourth dimension to laissez passer on your app a purpose of existence. This tin move on alongside entirely i way. To add together some information into it! As nosotros told in part 1 we volition do an application based on the famous (Fisher’s or Anderson’s) iris information laid which gives the measurements inward centimeters of the variables sepal length in addition to width in addition to petal length in addition to width, respectively, for fifty flowers from each of three species of iris. The species are Iris setosa, versicolor, in addition to virginica.
This is a “built in” dataset of RStudio that volition assistance us do our commencement application.
But usually you lot desire to analyze your ain datasets. The commencement matter you lot should do inward fellowship to perform whatsoever variety of analysis on them is to charge them properly. We volition directly meet how to charge a dataset inward txt shape from a local file using RStudio. Let’s go!
The procedure is quite simple. First of all you lot get got to house the txt file that contains your dataset into the same directory that you lot are working, secondly press the “Import Dataset” push clit inward RStudio in addition to hence “From Local File…”. Find the txt file inward your calculator in addition to click “Open”, hence press “Import”. That’s all! Your dataset is properly loaded inward your directory in addition to directly you lot tin piece of employment alongside it.
Please banknote that the purpose of this serial is non to learn how to shape your dataset earlier you lot charge it nor how to “clean” it. You tin gain to a greater extent than information almost this dependent area from here. Our purpose is to learn you lot construct your commencement Shiny App.
Exercise 4
Load the dataset you lot desire to analyze (“something.txt”) from your calculator to your directory alongside RStudio buttons.
INTRODUCTION TO REACTIVITY
From this signal you lot are going to move inward in the “Kingdom of Reactivity”. Reactive output automatically responds when your user interacts alongside a widget. You tin do reactive output past times next ii steps. Firstly, add together an R object to your ui.R. in addition to hence tell Shiny how to construct the object inward server.R.
1): Add an R object to the UI
Shiny provides a diversity of functions that transform R objects into output for your UI every bit you lot tin meet below:
: raw HTML
imageOutput: image
plotOutput: plot
tableOutput: table
textOutput: text
uiOutput: raw HTML
verbatimTextOutput: text
To add together output to the UI house the output function inside sidebarPanel or mainPanel in the ui.R script.
For example, the ui.R file below uses tableOutput to add together a reactive trouble 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 grapheme string “dt1”. Each of the *Output functions require a grapheme string that Shiny volition utilization every bit the cite of your reactive element. Users cannot meet it in addition to you lot volition empathize its role later.
Exercise 5
Add a datatableOutput to “Data Table”, cite its declaration “Table”.
2): Provide R code to construct the object.
The code should live placed inward the role that appears inside shinyServer in your server.R script.
This role is of groovy importance every bit it builds a list-like object named output that contains all of the code needed to update the R objects inward your app. Be careful, each R object MUST get got its ain entry inward the list.
You tin do an entry past times defining a novel chemical constituent for output within the function. The chemical constituent cite should gibe the cite of the reactive chemical constituent that you lot created inward ui.R.
Each entry should comprise the output of i of Shiny’s render* functions. Each render* role corresponds to a specific type of reactive object. You tin detect them below:
renderImage: images (saved every bit a link to a source file)
renderPlot: plots
renderPrint: whatsoever printed output
renderTable: information frame, matrix, other tabular array similar structures
renderText: grapheme strings
renderUI: a Shiny tag object or HTML
Each render* role takes a unmarried argument: an R aspect which tin either live i uncomplicated trouble of text, or it tin involve many lines of code.
In the instance below “dt1” is attached to the output expression inward server.R in addition to gives us the Data Table of “iris” dataset within “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* role to server.R inward fellowship to do the Data tabular array of the “iris” dataset. Hint: Use the output expression.
The dataTableOutput is your commencement contact alongside reactivity, inward the side past times side parts of our serial you lot volition utilization the residue of the Output functions that Shiny provides. But for directly let’s experiment a piddling fleck on this.
As you lot tin meet at that spot is a text filter inward your Data Table. You tin deactivate it past times setting searching to live “FALSE” every bit the instance 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 optionslist and searching.
With the same logic you lot tin disable the pagination that is displayed inward your Data Table, every bit inward the instance 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 optionslistpaging.
Now you lot tin meet how to display an exact reveal of rows (15) in addition to 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 i time to a greater extent than in addition to laid the exact reveal of rows that are displayed to 10. Hint: Use optionslistpageLength
We tin also do a Length Menu inward fellowship to command totally the choices of the numbers of rows nosotros desire to live displayed. In the instance below nosotros assign every reveal to a card label. v -> ‘5’, 10 -> ’10’, xv -> ’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 alongside values (10,20,30,-1) in addition to assign each i ot the values to the appropriate card label. Hint: Use optionslistlengthMenu,pageLength.


__________________________________________________________

Below are the solutions to part 1 by practising alongside the do laid nosotros prepared for you. Lets begin!
Answers to the exercises are available here.
If you lot obtained a dissimilar (correct) respond than those listed on the solutions page, delight experience costless to postal service your respond every bit a comment on that page.
Learn more about Shiny inward the online course R Shiny Interactive Web Apps – Next Level Data Visualization. In this course of written report you lot volition larn how to do advanced Shiny spider web apps; embed video, pdfs in addition to images; add together focus in addition to zooming tools; in addition to many other functionalities (30 lectures, 3hrs.).
TABSET PANEL
In the instance below you lot volition meet hot to add together 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 instance below you lot volition meet 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 but added to your Shiny App. Name it “Data Table”.
Exercise 3
Put a second tabPanel next to the commencement one. Name it “Summary”.
LOAD DATASET
Now it is fourth dimension to laissez passer on your app a purpose of existence. This tin move on alongside entirely i way. To add together some information into it! As nosotros told in part 1 we volition do an application based on the famous (Fisher’s or Anderson’s) iris information laid which gives the measurements inward centimeters of the variables sepal length in addition to width in addition to petal length in addition to width, respectively, for fifty flowers from each of three species of iris. The species are Iris setosa, versicolor, in addition to virginica.
This is a “built in” dataset of RStudio that volition assistance us do our commencement application.
But usually you lot desire to analyze your ain datasets. The commencement matter you lot should do inward fellowship to perform whatsoever variety of analysis on them is to charge them properly. We volition directly meet how to charge a dataset inward txt shape from a local file using RStudio. Let’s go!
The procedure is quite simple. First of all you lot get got to house the txt file that contains your dataset into the same directory that you lot are working, secondly press the “Import Dataset” push clit inward RStudio in addition to hence “From Local File…”. Find the txt file inward your calculator in addition to click “Open”, hence press “Import”. That’s all! Your dataset is properly loaded inward your directory in addition to directly you lot tin piece of employment alongside it.
Please banknote that the purpose of this serial is non to learn how to shape your dataset earlier you lot charge it nor how to “clean” it. You tin gain to a greater extent than information almost this dependent area from here. Our purpose is to learn you lot construct your commencement Shiny App.
Exercise 4
Load the dataset you lot desire to analyze (“something.txt”) from your calculator to your directory alongside RStudio buttons.
INTRODUCTION TO REACTIVITY
From this signal you lot are going to move inward in the “Kingdom of Reactivity”. Reactive output automatically responds when your user interacts alongside a widget. You tin do reactive output past times next ii steps. Firstly, add together an R object to your ui.R. in addition to hence tell Shiny how to construct the object inward server.R.
1): Add an R object to the UI
Shiny provides a diversity of functions that transform R objects into output for your UI every bit you lot tin meet below:
), "data laid gives the measurements inward centimeters of the variables sepal length in addition to width in addition to petal length in addition to 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"), tabsetPanel(type="tabs") ) ) )) #################### # # # Exercise 2 # # # #################### #ui.R library(shiny) shinyUI(fluidPage( titlePanel("Shiny App"), sidebarLayout( sidebarPanel(h2("Menu"), 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, radioButtons("radio", label = h4("Radio Buttons"), 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 laid gives the measurements inward centimeters of the variables sepal length in addition to width in addition to petal length in addition to 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"), tabsetPanel(type="tabs",tabPanel("Data Table")) ) ) )) #################### # # # Exercise three # # # #################### #ui.R library(shiny) shinyUI(fluidPage( titlePanel("Shiny App"), sidebarLayout( sidebarPanel(h2("Menu"), 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, radioButtons("radio", label = h4("Radio Buttons"), 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 laid gives the measurements inward centimeters of the variables sepal length in addition to width in addition to petal length in addition to 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"), tabsetPanel(type="tabs",tabPanel("Data Table"),tabPanel("Summary")) ) ) )) #################### # # # Exercise 4 # # # #################### #Place your .txt file inward the same directory that you lot are working -> "Import Dataset" -> "From Local File..." -> "Open" -> "Import" #################### # # # Exercise v # # # #################### #ui.R library(shiny) shinyUI(fluidPage( titlePanel("Shiny App"), sidebarLayout( sidebarPanel(h2("Menu"), 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, radioButtons("radio", label = h4("Radio Buttons"), 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 laid gives the measurements inward centimeters of the variables sepal length in addition to width in addition to petal length in addition to 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"), tabsetPanel(type="tabs",tabPanel("Data Table",dataTableOutput("Table")), tabPanel("Summary")) ) ) )) #################### # # # Exercise six # # # #################### #server.R shinyServer(function(input, output) { output$Table <- renderDataTable( iris) }) #################### # # # Exercise seven # # # #################### #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 ix # # # #################### #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( lengthMenu = list(c(10, 20, 30,-1),c('10','20','30','ALL')), pageLength = 10)) })

http://www.r-exercises.com/2017/01/01/building-shiny-app-solutions-part-4/
http://www.r-exercises.com/2017/01/01/building-shiny-app-exercises-part-4/

Sumber http://engdashboard.blogspot.com/

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

Bali Attractions

BACA JUGA LAINNYA:

Bali Attractions