Build a Hello world shiny App With R

Introduction to Shiny

Shiny is R Package that allows you to turn your analysis into an interactive and engaging  Web Application using R.

In this post, we will develop a Hello World shiny application. In an upcoming post, we will explore more about the shiny package and develop more applications.

Now without wasting our time, we move for developing shiny App.

For developing a shiny we have to follow these steps :

  • Install Shiny Package
  • Load Shiny Package
  • Create a HTML UI with HTML function
  • Create a Server
  • Run the app

Install Shiny Package


# install shiny package
install.packages("shiny")

Load Shiny Package


# load shiny package
library(shiny)

Create a HTML UI with HTML function


#Create a HTML UI with HTML function
ui <- fluidPage("Hello World")

Create a Server


#create shiny server
server <- function(input,output,session){
}

Run the app


#Run the app
shinyApp(ui=ui,server=server)

Hence for building Hello World app with shiny we have write following lines of code.


# install shiny package
install.packages("shiny")
# load shiny package
library(shiny)
#Create a HTML UI with HTML function
ui <- fluidPage("Hello World")
#create shiny server
server <- function(input,output,session){
}
#Run the app
shinyApp(ui=ui,server=server)

OutPut

Now we are modifying our script and make it a little bit dynamic.
For that, we will add an input textbox When you insert your name inside the textbox. Suppose I have inserted my name inside textbox then it will contact with Hello World and complete text will be Hello World Dheeraj.

So, Now the question rises how to add an input text field for entering your name then for that purpose Shiny has defined some functions.


#taking user input
ui <- fluidPage(textInput("name","Enter your Name"),
                textOutput("q"))

Then after we will concate this input name with Hello World text as shown below.


load library
library(shiny)

# create a html ui with html function
#taking user input
ui <- fluidPage(textInput("name","Enter your Name"),
                textOutput("q"))
#create a server
server <- function(input,output,session){
  output$q <- renderText({
    paste("Hello World ",input$name)
  })
}
#Run an PP
shinyApp(ui, server)

OutPut

Leave a Reply

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