Shiny helps you make web apps that users can interact with easily. It turns your R code into a friendly app with buttons, sliders, and more.
0
0
Shiny basics for interactive apps in R Programming
Introduction
You want to share your data analysis with others who don't know R.
You want users to explore data by changing inputs like dates or categories.
You want to build a simple tool that updates results instantly when inputs change.
Syntax
R Programming
library(shiny) ui <- fluidPage( # UI elements go here ) server <- function(input, output, session) { # Server logic goes here } shinyApp(ui, server)
ui defines what the user sees and interacts with.
server defines how the app reacts to user inputs and creates outputs.
Examples
This example shows a slider and text that updates when you move the slider.
R Programming
ui <- fluidPage( sliderInput("num", "Choose a number:", 1, 100, 50), textOutput("result") ) server <- function(input, output, session) { output$result <- renderText({ paste("You chose", input$num) }) }
This example counts how many times a button is clicked and shows the count.
R Programming
ui <- fluidPage( actionButton("go", "Click me"), textOutput("count") ) server <- function(input, output, session) { counter <- reactiveVal(0) observeEvent(input$go, { counter(counter() + 1) }) output$count <- renderText({ paste("Button clicked", counter(), "times") }) }
Sample Program
This app lets the user pick a number with a slider. It shows double that number instantly.
R Programming
library(shiny) ui <- fluidPage( titlePanel("Simple Shiny App"), sidebarLayout( sidebarPanel( sliderInput("num", "Pick a number:", 1, 10, 5) ), mainPanel( textOutput("double") ) ) ) server <- function(input, output, session) { output$double <- renderText({ paste("Double of your number is", input$num * 2) }) } shinyApp(ui, server)
OutputSuccess
Important Notes
Use renderText() to create text outputs that update automatically.
Use input$ to get values from UI controls like sliders or buttons.
Always call shinyApp(ui, server) at the end to run your app.
Summary
Shiny apps have two parts: ui for what users see, and server for how the app works.
Use input controls to get user choices and output functions to show results.
Shiny updates outputs automatically when inputs change, making apps interactive.