Lokesh Mano
NBIS, SciLifeLab
14-Apr-2025
One file format
shiny::runApp("path/to/folder")
R -e "shiny::runApp('~/shinyapp')"
rmarkdown::run()
User Interface (UI)
#| '!! shinylive warning !!': |
#| shinylive does not work in self-contained HTML documents.
#| Please set `embed-resources: false` in your metadata.
#| standalone: true
#| components: [editor, viewer]
shinyApp(
ui=fluidPage(
titlePanel("Title Panel"),
sidebarLayout(
sidebarPanel(
helpText("Sidebar Panel")
),
mainPanel(
"hello"
)
)
),
server=function(input,output) {})
Hypertext Markup Language (HTML)
<div class="col-sm-4">
<form class="well" role="complementary">
<span class="help-block">Sidebar Panel</span>
</form>
</div>
#| '!! shinylive warning !!': |
#| shinylive does not work in self-contained HTML documents.
#| Please set `embed-resources: false` in your metadata.
#| standalone: true
#| components: [editor, viewer]
shinyApp(
ui=fluidPage(
titlePanel("Title Panel"),
sidebarLayout(sidebarPanel(helpText("Sidebar Panel")),
mainPanel(tabsetPanel(
tabPanel("tab1",
fluidRow(
column(6,helpText("Col1")),
column(6,
helpText("Col2"),
fluidRow(
column(4,style="background-color:#b0c6fb",helpText("Col1")),
column(4,style="background-color:#ffa153",helpText("Col2")),
column(4,style="background-color:#b1f6c6",helpText("Col3"))
)
)
)
),
tabPanel("tab2",
inputPanel(helpText("Input Panel"))
),
tabPanel("tab3",
wellPanel(helpText("Well Panel"))
)
)
)
)
),
server=function(input,output) {})
#| '!! shinylive warning !!': |
#| shinylive does not work in self-contained HTML documents.
#| Please set `embed-resources: false` in your metadata.
#| standalone: true
#| components: [editor, viewer]
shinyApp(
ui=fluidPage(
fluidRow(
column(4,
selectInput("select-input",
label="selectInput",
choices=c("A","B","C")),
)
)
),
server=function(input,output) {
})
#| '!! shinylive warning !!': |
#| shinylive does not work in self-contained HTML documents.
#| Please set `embed-resources: false` in your metadata.
#| standalone: true
#| components: [editor, viewer]
shinyApp(
ui=fluidPage(
fluidRow(
column(4,
fileInput("file-input","fileInput:"),
selectInput("select-input",label="selectInput",choices=c("A","B","C")),
sliderInput("slider-input",label="sliderInput",value=5,min=1,max=10),
numericInput("numeric-input",label="numericInput",value=5,min=1,max=10),
textInput("text-input",label="textInput"),
textAreaInput("text-area-input",label="textAreaInput"),
dateInput("date-input",label="dateInput"),
dateRangeInput("date-range-input",label="dateRangeInput"),
radioButtons("radio-button",label="radioButtons",choices=c("A","B","C"),inline=T),
checkboxInput("checkbox","checkboxInput",value=FALSE),
actionButton("action-button","Action"),
hr(),
submitButton()
)
)
),
server=function(input,output) {
})
Function | Description |
---|---|
checkboxInput() |
Checkbox |
checkboxGroupInput() |
Checkbox group |
radioButtons() |
Radio buttons |
dateInput() |
Single date |
dateRangeInput() |
Date range |
fileInput() |
Upload file |
numericInput() |
Input number |
sliderInput() |
Input number |
Function | Description |
---|---|
textInput() |
Single line text input |
textAreaInput() |
Multi-line text input |
passwordInput() |
Password input |
selectInput() |
Dropdown selection |
actionButton() |
Action button |
submitButton() |
Submit button |
tabsetPanel() |
Tabset panel |
navbarPage() |
Page with navbar |
#| '!! shinylive warning !!': |
#| shinylive does not work in self-contained HTML documents.
#| Please set `embed-resources: false` in your metadata.
#| standalone: true
#| components: [editor, viewer]
shinyApp(
ui=fluidPage(
textInput("text_input",label="textInput",value="hello world"),
textOutput("text_output")
),
server=function(input, output) {
output$text_output <- renderText({input$text_input})
})
#| '!! shinylive warning !!': |
#| shinylive does not work in self-contained HTML documents.
#| Please set `embed-resources: false` in your metadata.
#| standalone: true
#| components: [editor, viewer]
shinyApp(
ui=fluidPage(fluidRow(column(5,
textInput("text_input",label="textInput",value="<h3 style='color:red'>Red text</h3>"),
hr(),
htmlOutput("html_output"),
textOutput("text_output"),
verbatimTextOutput("verbatim_text_output"),
tableOutput("table_output"),
plotOutput("plot_output",width="300px",height="300px")
))),
server=function(input, output) {
output$html_output <- renderText({input$text_input})
output$text_output <- renderText({input$text_input})
output$verbatim_text_output <- renderText({input$text_input})
output$table_output <- renderTable({iris[1:3,1:3]})
output$plot_output <- renderPlot({
plot(iris[,1],iris[,2])
})
})
Output | Renderer | Description |
---|---|---|
textOutput() |
renderText() /renderPrint() |
Standard text |
verbatimTextOutput() |
renderText() /renderPrint() |
Monospaced text |
htmlOutput() |
renderText() /renderPrint() |
HTML text output |
plotOutput() |
renderPlot() |
Create and display image |
imageOutput() |
renderImage() |
Display existing image |
tableOutput() |
renderTable() |
Table output |
uiOutput() |
renderUI() |
HTML components |
#| '!! shinylive warning !!': |
#| shinylive does not work in self-contained HTML documents.
#| Please set `embed-resources: false` in your metadata.
#| standalone: true
#| components: [editor, viewer]
shinyApp(
ui = fluidPage(
h3("Temperature Converter"),
numericInput("celsius", "Degrees Celsius:", value = 0),
textOutput("fahrenheit")
),
server = function(input, output) {
output$fahrenheit <- renderText({
paste(input$celsius, "°C is ", (input$celsius * 9/5) + 32, " °F")
})
})
uiOutput()
/renderUI()
shinyApp(
ui=fluidPage(
selectInput("type",label="Select input type", choices=c("Text","Number")),
uiOutput("ui"),
textOutput("text_output"),
),
server=function(input, output) {
output$ui <- renderUI({
if(input$type=="Text") {
textInput("input_text","Enter text")
}else{
sliderInput("input_number", "Select number", value=5, min=1, max=10)
}
})
output$text_output <- renderText({
if(input$type=="Text") {
input$input_text
}else{
input$input_number
}
})
})
conditionalPanel()
, insertUI()
and removeUI()
#| '!! shinylive warning !!': |
#| shinylive does not work in self-contained HTML documents.
#| Please set `embed-resources: false` in your metadata.
#| standalone: true
#| components: [editor, viewer]
shinyApp(
ui=fluidPage(
selectInput("type", label="Select input type", choices=c("Text","Number")),
uiOutput("ui"),
textOutput("text_output"),
),
server=function(input, output) {
output$ui <- renderUI({
if(input$type=="Text") {
textInput("input_text", "Enter text", value="hello")
}else{
sliderInput("input_number", "Select number", value=5, min=1, max=10)
}
})
output$text_output <- renderText({
if(input$type=="Text") {
input$input_text
}else{
input$input_number
}
})
})
#| '!! shinylive warning !!': |
#| shinylive does not work in self-contained HTML documents.
#| Please set `embed-resources: false` in your metadata.
#| standalone: true
#| components: [editor, viewer]
shinyApp(
ui=fluidPage(
selectInput("data",label="Select data",
choices=c("mtcars","faithful","iris")),
tableOutput("table"),
uiOutput("ui")
),
server=function(input, output) {
data <- reactive({ get(input$data, 'package:datasets') })
output$ui <- renderUI({
if(input$data=="iris") plotOutput("plot",width="400px")
})
output$plot <- renderPlot({hist(data()[, 1])})
output$table <- renderTable({head(data())})
})
#| '!! shinylive warning !!': |
#| shinylive does not work in self-contained HTML documents.
#| Please set `embed-resources: false` in your metadata.
#| standalone: true
#| components: [editor, viewer]
shinyApp(
ui=fluidPage(
sliderInput("persons",label="Select number of persons", value=1, min=1,max=4),
uiOutput("ui")
),
server=function(input, output) {
output$ui <- renderUI({
lapply(1:input$persons, function(i) {
div(
textInput(paste0("name",i),"Enter name:",paste0("Person ",i)),
textInput(paste0("tel",i),"Enter phone number:",value = "0773921562"),
hr()
)
})
})
})
Server
is a function that assembles your input
into output
using R based code.Rule 1: Save objects to display to output$
Rule 2: Build objects to display with render*()
{}
inside the render*()
function.Rule 3: Use input values with input$
Slide inspirations: Roy Francis (NBIS, RaukR2024)