Open In App

Create An Interactive Web App Using Shiny Package In R

Improve
Improve
Like Article
Like
Save
Share
Report

Perhaps a quick introduction to what Shiny is would be helpful before moving on. Creating interactive web applications with R Programming Language is simple thanks to the Shiny package. Shiny’s advantage is that it enables you to extend your R code to the web, which essentially increases the usability of your code for a larger audience.

Building interactive web applications with R is incredibly simple thanks to the R Shiny framework, a package from RStudio. R Shiny is fantastic in that it enables you to produce incredibly effective data reports and visualizations that let the user explore a data set. You can style your application’s content using HTML elements in addition to Shiny elements.

Building Blocks of an R-Shiny Web App

There are two parts to the Shiny web app:

  • User Interface  â€” {ui.R} The UI controls what is being displayed on the application page and how the components are laid out. This may include text and other markdown elements, graphics, widgets that take in user input, or plots. You will also use the UI to define a navigation bar with multiple tabs in this tutorial. The front end that receives user input values is known as the UI.
  • The Server — {server.R} is the backend that processes these input values to create the output results that are ultimately shown on the website. The data that will be shown through the UI is under the server’s control. You will load data into and manage it on the server, then define your outputs using input from the UI.

Building Blocks of an R-Shiny Web App

 

Steps For setup for Shiny App

  • Create a new directory, then inside of it, two R scripts, “ui.R” and “server.R”, to create a new Shiny app. The “server.R” script will specify the app’s logic and functionality, while the “ui.R” script will specify the app’s design and user interface.
  • Use the ‘ui.R’ script’s fluidPage(), sidebarLayout(), and textInput() functions to design the app’s layout and user interface.
  • Use functions like observeEvent() and render*() to build the app’s logic and functionality in the “server.R” script.
  • After completing the aforementioned steps, you can launch the application by running the following command.

You need to install the Shiny, Shinythemes, and ggplot2 packages and then load them as well.

R




# Installing Necessary Packages
install.packages("shiny")
install.packages("shinythemes")
install.packages("ggplot2")
 
# Loading the libraries
library(shiny)
library(shinythemes)
library(ggplot2)


After loading the Library and template, you have to define the UI.

R




#UI Defined
ui <- fluidPage(theme = shinytheme("superhero"),
                navbarPage(
                     
                      # mainPanel
                    "Welcome to GFG",
                   
                      # Tab 1
                    tabPanel("Tab 1",
                             mainPanel(
                                 tags$h3("Register to GFG:"),
                                 textInput("txt1", "Name:", ""),
                                 textInput("txt2", "Surname:", ""),
                                  textInput("txt3", "Email:", ""),
                                  textInput("txt4", "Mobile.No:", ""),
                                 textInput("txt5", "Education:", ""),
                                 textInput("txt6", "College:", ""),
                                 textInput("txt7", "Programming Language:", ""),
                                 textInput("txt8", "Area of Interest:", ""),
                                  
                                 hr(),
                                h4("Here is your Details"),
                               verbatimTextOutput("txtout"),
                             )),
                      # TabPanel
                    tabPanel("Tab 2",
                             h3("Hello !! Here is the Plot Graph,
                                 Slider, Dropdown and Date picker!!!"))),
                             hr(),
                             (
  plotOutput("plot") #PlotGraph
),
                             hr(),
                             sidebarPanel(
      sliderInput("slider", "Select a value:", min = 10,
                  max = 500, value = 125),
      selectInput("dropdown", "Select an option:",
                  c("Python", "JAVA", "C++")),
      dateInput("datepicker", "Select a date:"),
      ),#SidebarPanel
    mainPanel(
      h3("Selected values:"),
      textOutput("selected_value"),
      textOutput("selected_option"),
      textOutput("selected_date"),
    )
 )


After defining UI, You have to define the Server. After defining UI and server, you have to create a shiny object.

R




# Define Server Function
server <- function(input, output) {
   output$selected_value <- renderText({input$slider})
  output$selected_option <- renderText({input$dropdown})
  output$selected_date <- renderText({input$date})
   output$txtout <- renderText({
     paste(input$txt1, input$txt2,
           input$txt3, input$txt4, input$txt5,
           input$txt6, input$txt7, input$txt8,
           sep = " " )
   })
    output$plot <- renderPlot({
    ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
  })
 }
 
 
# to Run App
shinyApp(ui = ui, server = server)


Output:

Shiny App - Tab 1

Shiny App – Tab 1

Shiny App - Tab 2

Shiny App – Tab 2

Summary:

This example demonstrates how to make an interactive web app that accepts user input and displays the outcome using the Shiny package. This app also shows PlotGraph. On the left side of the screen, this app will show a slider, dropdown menu, and date picker. On the right side of the screen, it will show the values that have been selected.  In order to create a more sophisticated and interactive user interface and more intricate server logic, you can use a variety of other Shiny functions and widgets with more apps.



Last Updated : 20 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads