More about shinny App (Inputs, Outputs, Layouts)

In the previous post, we had Build a Hello world shiny App With R.
In this post, we will cover some points regarding shiny Package. During this journey, we will go through these points :


#Design Layout
#Shiny Inputs
#Shiny Outputs

Design Layout

For designing layout, we use sidebarLayout() function. As we know that all the functions we write inside UI parts. As we know all the functions that we use for designing we write these functions in the UI part.


ui <- fluidPage( sidebarLayout()
)

sidebarLayout()

sidebarLayout() functions contains two different panels.

  • sidebarPanel()
  • mainPanel()

So, Now our code will look like this


#sidebarPanel syntax
ui <- fluidPage(
        sidebarLayout({
             sidebarPanel(),
             mainPanel()
})
)

Some more points about sidebarLayout() function

  • sidebarPanel() : In this panel input is stored.
  • mainPanel() : In this panel outPut is stored.

#load library
library(shiny)
library(ggplot2)

# create a html ui with html function
ui <- fluidPage(
  sidebarLayout(
     sidebarPanel(
       #taking user input
       textInput("name","Enter your Name"style="border: 2px solid;")
     ),
    mainPanel(
      plotOutput('trend')
    )
  )
)
#create a server
server <- function(input, output, session) {
  output$trend <- renderPlot({
    ggplot()
  })
}
#Run App
shinyApp(ui = ui, server = server)

OutPut:

shiny Inputs

shiny provides variety of input elements.

textInput


#load library
library(shiny)
# create a html ui with html function
ui<- fluidPage(
  textInput('name','Enter your name'),
  textOutput('name')
)
#create a server
server <- function(input, output,session){
  output$name <- renderText({
    paste('Hi ', input$name)
  })
}
#Run App
shinyApp(ui,server)

OutPut:

selectInput


#load library
library(shiny)
# create a html ui with html function
ui<- fluidPage(
  selectInput('hobby','Enter your Hobby',
              choices = c("Reading","Playing","Dancing","Swimming")),
  textOutput('result')
),
#create a server
server <- function(input, output,session){
  output$result <- renderText({
    paste('You are found of ', input$hobby)
  })
}
#Run App
shinyApp(ui,server)

OutPut:

sliderInput


#load library
library(shiny)
# create a html ui with html function
ui <- fluidPage(
  sliderInput('salary','Select your salary',
              value=30000,min=10000,max=500000),
  plotOutput('salaryResult')
)
#create a server
server <- function(input,output,session){
  output$salaryResult <- renderPlot(
       hist(rnorm(input$salary))
  )
}
#Run App
shinyApp(ui, server)

OutPut:

Leave a Reply

Your email address will not be published. Required fields are marked *