UI.R partial coding

As it was said before, the input widgets are the first part of the application that must be coded. In this case, almost all the widgets are on the side panel except the selectors that are used in the education-descending line chart and the earnings chi-square test that are in their corresponding tabs. Also, it is recommended to generate the application's frontend structure at this stage. So, the UI.R code that we have so far would be as shown in the following section.

UI.R

As it may be noted, the arguments of the widgets builders are determined dynamically. The reason behind this is exactly the same as in the creation of factor.vars in global.R: avoid hardcoding. Of course, there are some cases (for example, the sex variable) where this will be unnecessary, as it is impossible that the categories were changed even with a change in the data source. However, in cases such as age, it is perfectly possible that the minimum and maximum values change. So, this dynamic referencing guarantees that the input's limits are the same as the dataset's:

library(shiny)

# Starting line
shinyUI(fluidPage(
 
 # Application title
 titlePanel("Adult Dataset"),
 
 sidebarLayout(
   
   sidebarPanel(
     h1("Gender"),
     checkboxGroupInput("gender", "Choose the genders",
                        choices = levels(data.adult$sex),
                        selected = levels(data.adult$sex)),
     h1("Age"),
     sliderInput("minage", "Select lower limit", min(data.adult$age), max(data.adult$age), value = min(data.adult$age), step = 1),
     sliderInput("maxage", "Select upper limit", min(data.adult$age), max(data.adult$age), value = max(data.adult$age), step = 1),
     
     h1("Ethnic group"),
     selectInput("ethnic", "Select ethnic groups", choices = levels(data.adult$race),
                 selected = levels(data.adult$race), multiple = T),
     h1("Marital Status"),
     selectInput("marital.stat", "Select marital status", choices = levels(data.adult$marital.status),
                 selected = levels(data.adult$marital.status), multiple = T),
     
     actionButton("submitter","Submit")
   ),
   
   
   mainPanel(
     tabsetPanel(
       tabPanel("Demographics",
                column(6,div("Gender Distribution (in %)", class = "title"),
                       div("Age Distribution (in %) by Gender", class = "title")),
                column(6,div("Ethnicity distribution by gender (in %)", class = "title"),
                       div("Marital Status distribution (in %)", class = "title"))),
                tabPanel("Education",
                         div("Educational Curve", class = "title"),
                         selectInput("edvar","Choose the variable to compare",
                                     choices = c("sex","race", "marital.status"),
                                     selected = "sex")),
                tabPanel("Occupation and Earnings",
                         div("Earning Chi test", class = "title"),
                         selectInput("earnvar","Choose the variable to run the test with",
                                     choices = factor.vars, selected = factor.vars[1]),
                         div("Activity summary", class = "title"))
       )
     )
   )
 ))

There is only one input where its options are hardcoded: that is, edvar. This is because the election of the variables is firstly totally arbitrary (and for this reason, they should be hardcoded somewhere), and secondly because they are not used anywhere else. If the same options were used in another part of the application, it would have been better to store them in a separate object that is coded on global.R.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset