Open In App

Create An Interactive Web App Using Shiny Package In R

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:

Building Blocks of an R-Shiny Web App

 

Steps For setup for Shiny App

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






# 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.




#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.




# 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 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.


Article Tags :