shiny Getting started with shiny Simple App


Example

Each shiny app contains two parts: A user interface definition (UI) and a server script (server). This example shows how you can print "Hello world" from UI or from server.

UI.R

In the UI you can place some view objects (div, inputs, buttons, etc).

library(shiny)

# Define UI for application print "Hello world" 
shinyUI(

  # Create bootstrap page 
  fluidPage(
    
    # Paragraph "Hello world"
    p("Hello world"),

    # Create button to print "Hello world" from server
    actionButton(inputId = "Print_Hello", label = "Print_Hello World"),

    # Create position for server side text
    textOutput("Server_Hello")
    
  )
)

Server.R

In the server script you can define methods which manipulate data or listen to actions.

# Define server logic required to print "Hello World" when button is clicked
shinyServer(function(input, output) {
  
  # Create action when actionButton is clicked
  observeEvent(input$Print_Hello,{

    # Change text of Server_Hello
    output$Server_Hello = renderText("Hello world from server side")
  })
  
  
})

How to run?

You can run your app in several ways:

  1. Create two different files and place them into one directory, then use runApp('your dir path')
  2. You can define two variables (ui and server, for example) and then use shinyApp(ui,server) to run your app

Result

In this example you will see some text and a button:

screenshot before click

And after button click the server responds:

screenshot after click