R Shiny
last modified November 20, 2026
Shiny turns an R script into an interactive web application. An app has two
parts: a user interface that describes the page, and a server function that
reacts to inputs and produces outputs. Shiny runs a small web server, renders the
interface in a browser, and sends the results back when an input changes. This
article builds apps from a text greeting to a table and plot explorer, and tests
each server without opening a browser with testServer. Each app is
a complete app.R file; keep the versions in separate directories if
you want to run them side by side. The examples use R 4.5.2 and Shiny 1.14.0.
The examples need the shiny package, which is not part of base R. Install it
once with install.packages("shiny"); the
installing packages section of the
introduction explains the details.
A minimal app
An app is usually a single app.R file. fluidPage
builds a page that adapts to the window width, textInput creates a
labeled text box, and textOutput reserves a place for text. The
server function receives input, output, and
session; renderText fills an output slot, and
input$name reads the current value. The file ends with
shinyApp(ui, server), which returns the app object.
library(shiny)
ui <- fluidPage(
textInput("name", "Your name", "world"),
textOutput("greeting")
)
server <- function(input, output, session) {
output$greeting <- renderText({
paste("Hello,", input$name)
})
}
shinyApp(ui, server)
The interface has one input named name with the initial value
world, and one output named greeting. The server
renders the greeting whenever input$name changes.
renderText returns a character string; textOutput
inserts it into the page. The names on both sides must match:
textOutput("greeting") is filled by output$greeting.
Save the file and call shiny::runApp() from its directory to start
the app.
Testing the server without a browser
testServer runs the server function in a mock session.
session$setInputs sets input values, and
output$greeting evaluates the corresponding render function. This
catches typos and logic errors without starting a server. The test script is an
ordinary R script, so it can run under Rscript in a build or a
check.
library(shiny)
testServer("app.R", {
session$setInputs(name = "R")
print(output$greeting)
})
The test sets name to R and prints the rendered
greeting. The output is the string Hello, R, which is exactly what
the browser would show. The app file is passed by path; when the working
directory is the app directory, testServer("app.R", ...) is enough.
The expression inside the braces runs in the server's environment, so
input, output, and session are available.
$ Rscript test.R [1] "Hello, R"
Inputs and outputs
numericInput stores a number, with a minimum, a maximum, and a
step. The server can use it in any expression, and renderText
converts the result to text. The next app doubles a number. The test sets two
different values and prints the output after each change.
library(shiny)
ui <- fluidPage(
numericInput("n", "Number", 5, min = 1, max = 20),
textOutput("double")
)
server <- function(input, output, session) {
output$double <- renderText({
input$n * 2
})
}
shinyApp(ui, server)
The input starts at 5, so the initial output is 10. The test sets 7 and gets 14,
then sets 12 and gets 24. Shiny invalidates output$double when
input$n changes and calls the render function again. Nothing else
in the server needs to run.
library(shiny)
testServer("app.R", {
session$setInputs(n = 7)
print(output$double)
session$setInputs(n = 12)
print(output$double)
})
The test sets the input twice and prints the rendered value after each
change. testServer evaluates output$double when it is
read, so the printed value reflects the current input$n.
$ Rscript test.R [1] "14" [1] "24"
Reactive expressions
When two outputs depend on the same calculation, a reactive
expression computes it once and caches the result until its inputs change. The
expression is a function: squared() returns the value. Both the
square and the cube outputs call it, but the square is calculated only once per
change of input$n.
library(shiny)
ui <- fluidPage(
numericInput("n", "Number", 4, min = 1, max = 20),
textOutput("square"),
textOutput("cube")
)
server <- function(input, output, session) {
squared <- reactive(input$n ^ 2)
output$square <- renderText(squared())
output$cube <- renderText(squared() * input$n)
}
shinyApp(ui, server)
The test sets n to 5. output$square is 25 and
output$cube is 125. The cube output calls squared()
and multiplies by input$n; it does not recompute the square. A
reactive expression is lazy: it runs the first time an output asks for it, and
then returns the cached value until an input it depends on changes.
library(shiny)
testServer("app.R", {
session$setInputs(n = 5)
print(output$square)
print(output$cube)
})
The test sets n once and reads both outputs. The two render
functions share the same reactive expression, so the square is computed before
the cube asks for it.
$ Rscript test.R [1] "25" [1] "125"
Layout and printed output
sidebarLayout puts controls in a sidebar and results in the main
panel. selectInput creates a drop-down list, and
verbatimTextOutput with renderPrint shows text exactly
as print would write it. The next app selects an iris species and
prints the first three rows of the chosen group.
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("species", "Species", choices = levels(iris$Species))
),
mainPanel(
verbatimTextOutput("print")
)
)
)
server <- function(input, output, session) {
output$print <- renderPrint({
rows <- subset(iris, Species == input$species)
print(rows[1:3, c("Sepal.Length", "Sepal.Width")])
})
}
shinyApp(ui, server)
The selected species is virginica, so subset returns
the 50 rows of that species. The first three rows have sepal lengths 6.3, 5.8,
and 7.1. renderPrint captures the printed data frame, including its
row names, while renderText would capture only a character vector.
The row names 101, 102, and 103 come from the position of these rows in the
original data frame.
library(shiny)
testServer("app.R", {
session$setInputs(species = "virginica")
cat(output$print, "\n")
})
The test selects a species and prints the captured data frame. cat
writes the string without the [1] and quotes that
print would add.
$ Rscript test.R
Sepal.Length Sepal.Width
101 6.3 3.3
102 5.8 2.7
103 7.1 3.0
Buttons and reactive values
actionButton creates a button that reports how many times it has
been clicked. observeEvent runs an expression when that input
changes, and reactiveVal stores a value that can be updated from an
observer. The next app adds ten to a total on every click. The button is not a
value that the user types; it is an event source.
library(shiny)
ui <- fluidPage(
actionButton("add", "Add 10"),
textOutput("total")
)
server <- function(input, output, session) {
total <- reactiveVal(0)
observeEvent(input$add, {
total(total() + 10)
})
output$total <- renderText(total())
}
shinyApp(ui, server)
The test sets the button input to 1, which triggers the observer and updates
total to 10. Setting it to 2 triggers the observer again and updates
the total to 20. reactiveVal(0) creates the initial value;
total() reads it and total(new_value) writes it. The
observer is the only place that changes the value, so the display and the state
cannot drift apart.
library(shiny)
testServer("app.R", {
session$setInputs(add = 1)
print(output$total)
session$setInputs(add = 2)
print(output$total)
})
The test sets the button counter to 1 and then to 2. Each change runs the observer once and updates the reactive value.
$ Rscript test.R [1] "10" [1] "20"
Plots
plotOutput reserves space for a plot and renderPlot
draws it. The render function runs when an input it uses changes, so a slider can
control the number of bins in a histogram. The plot is rendered as an image and
sent to the browser; the R object behind it is not part of the page.
library(shiny)
ui <- fluidPage(
sliderInput("bins", "Bins", 5, 30, 10),
plotOutput("hist")
)
server <- function(input, output, session) {
output$hist <- renderPlot({
hist(iris$Sepal.Length, breaks = input$bins,
main = "Sepal length", xlab = "cm")
})
}
shinyApp(ui, server)
The test sets the slider to 12 and evaluates output$hist. The
result is a list with src, width, height,
alt, and coordmap; the src field holds the
image data that the browser displays. The histogram uses the built-in
iris data and the base graphics hist function. A
ggplot2 object would work in the same slot with renderPlot.
library(shiny)
testServer("app.R", {
session$setInputs(bins = 12)
print(names(output$hist))
})
The test sets the slider and inspects the list returned by the plot output. The names are enough to confirm that the render function produced an image.
$ Rscript test.R [1] "src" "width" "height" "alt" "coordmap"
A complete app
The final app combines a reactive data selection, a text summary, a table, and a
slider. The species selector chooses all rows or one species; the slider controls
how many rows the table shows. The summary always describes the whole selection,
while the table shows only the first rows. head with a reactive
count is enough because the data is already in memory.
library(shiny)
ui <- fluidPage(
titlePanel("Iris explorer"),
sidebarLayout(
sidebarPanel(
selectInput("species", "Species",
choices = c("all", levels(iris$Species))),
sliderInput("rows", "Rows to show", 1, 10, 5)
),
mainPanel(
textOutput("summary"),
tableOutput("table")
)
)
)
server <- function(input, output, session) {
selected <- reactive({
if (input$species == "all") iris else subset(iris, Species == input$species)
})
output$summary <- renderText({
paste("records:", nrow(selected()),
"showing:", min(input$rows, nrow(selected())),
"mean sepal length:", round(mean(selected()$Sepal.Length), 2))
})
output$table <- renderTable({
head(selected(), input$rows)
})
}
shinyApp(ui, server)
The test selects virginica and three rows. The summary reports 50
records and a mean sepal length of 6.59, and the table output is a non-empty HTML
string. The selected reactive is shared by both outputs, so the
subset is computed once per input change. The slider changes the
showing count in the summary and the number of rows in the table;
the record count and the mean describe the whole selection.
library(shiny)
testServer("app.R", {
session$setInputs(species = "virginica", rows = 3)
cat(output$summary, "\n")
cat("table rendered:", nchar(output$table) > 0, "\n")
})
The test selects a species and a row count, then reads the summary and the table. The table is an HTML string, so the test checks that it is not empty.
$ Rscript test.R records: 50 showing: 3 mean sepal length: 6.59 table rendered: TRUE
Running the app
runApp starts the built-in web server and opens the app in a
browser. The first argument is a directory or an app.R file;
port chooses the TCP port, host chooses the interface,
and launch.browser = FALSE prevents the browser from opening on a
server. The call blocks until the app stops, usually with Ctrl+C. In RStudio, the
Run App button calls the same function.
library(shiny)
runApp("app.R", port = 3838, launch.browser = FALSE)
The script starts the app on port 3838 and prints a line such as
Listening on http://127.0.0.1:3838 when the server is ready. Other
machines can reach it only if host is set to 0.0.0.0
and the firewall allows the port. The app object can also be printed at the
console; shinyApp(ui, server) run at top level starts the same
server. Stop it with Ctrl+C in the terminal, or with the stop button in RStudio.
Next steps
The R Markdown and Quarto articles show how to put a static report on the web; Shiny adds a server that recomputes the report on demand. The HTTP article shows how to call an external API, which a Shiny app can do inside a reactive expression. The debugging article shows how to inspect a failing function, and the performance article shows how to profile a reactive expression that has become slow.
Source
Shiny: Server function testing with Shiny
In this article we built a Shiny app from a user interface and a server function,
then tested the server with testServer before opening a browser. A
text input filled a text output, a numeric input drove a calculation, and a
reactive expression shared that calculation between two outputs. A sidebar
layout, a select input, a printed data frame, an action button with a
reactiveVal, and a histogram covered the common output types. The
final app combined a reactive selection, a text summary, and a table, and
runApp started it on a local port. The
R Markdown and
Quarto articles cover the static reports that Shiny
apps often replace.
Author
List all R Language tutorials.