• Aucun résultat trouvé

Shiny

N/A
N/A
Protected

Academic year: 2022

Partager "Shiny"

Copied!
28
0
0

Texte intégral

(1)

I

NTRODUCTION TO

S

HINY

W

EB

A

PPLICATIONS Julie Scholler

M Éc E n

(2)

What is Shiny?

R package

Web application framework for R

interactive web page

interactivity with R

Great for sharing R program with someone not knowing R

Two parts

a web page that shows the app to the user

a computer that powers the app

your own computer

a server

(3)

Shiny Template

library(shiny)

#User Interface ui <- fluidpage()

#Server code (R)

server <- function(input,output){}

shinyApp(ui = ui, server = server) Very Important

name of the file app.R

in its own folder

(4)

Alternate Method

useful when the app is complex and involves lots of code

Two Files in the same folder

UI: ui.R

Server: server.R

(5)

Layout

ui <- fluidPage(

titlePanel("title panel"),

sidebarLayout(# always two arguments sidebarPanel("sidebar panel"), mainPanel("main panel")

) )

(6)

Layout

ui <- fluidPage(

titlePanel("title panel"),

sidebarLayout(

position = "right",# change sidebar position sidebarPanel("sidebar panel"),

mainPanel("main panel") )

)

(7)

Text in Panel

mainPanel(

h1("Big Title"),

p("p creates a paragraph of text."), h3("Third level title"),

code("text similar to computer code") )

(8)

All UI functions are simply HTML wrappers

ui <- fluidPage(

titlePanel("title panel"), sidebarLayout(

sidebarPanel("sidebar panel"), mainPanel("main panel")

) )

print(ui)

## <div class="container-fluid">

## <h2>title panel</h2>

## <div class="row">

## <div class="col-sm-4">

## <form class="well">sidebar panel</form>

## </div>

## <div class="col-sm-8">main panel</div>

## </div>

## </div>

(9)

Reactivity

For reactivity, app needs inputs and outputs Steps

Add input to the UI

Add an R object (output) to the UI

Tell Shiny how to build the object in the server function

Reactivity

The object will be reactive if the code that builds it calls a widget value.

(10)

Add inputs to the UI

(11)

Example with iris

sidebarPanel(

selectInput("var", # name to access the widget’s value label = "Choose a variable to display", choices = list("Sepal Length",

"Sepal Width",

"Petal Length",

"Petal Width"), selected = "Sepal Length") )

(12)

More widgets

List of function

actionButton: action Button

checkboxGroupInput: group of check boxes

checkboxInput: single check box

dateInput: calendar to aid date selection

dateRangeInput: pair of calendars for selecting a date range

fileInput: file upload control wizard

helpText: help text that can be added to an input form

numericInput: field to enter numbers

radioButtons: set of radio buttons

selectInput: box with choices to select from

sliderInput: slider bar

submitButton: submit button

textInput: field to enter text Link to Widget Gallery

(13)

Add an R object (output) to the UI

mainPanel(

textOutput("selected_var") )

List of Output functions

dataTableOutput: DataTable

tableOutput: table

plotOutput: plot

htmlOutput: raw HTML

imageOutput: image

textOutput: text

uiOutput: raw HTML

verbatimTextOutput: text

(14)

Provide R code to build the object

server <- function(input, output) { output$selected_var <- renderText({

paste("You have selected", input$var) })

}

(15)

Provide R code to build the object

server <- function(input, output) { output$selected_var <- renderText({

paste("You have selected", input$var) })

}

(16)

List of Render functions

renderDataTable: DataTable

renderImage: images (saved as a link to a source file)

renderPlot: plots

renderPrint: any printed output

renderTable: data frame, matrix, other table like structures

renderText: character strings

renderUI: a Shiny tag object or HTML

(17)

Add a Plot - UI

ui <- fluidPage(

titlePanel("Final App"), sidebarLayout(

sidebarPanel(

selectInput("var",

label = "Choose a variable to display", choices = list("Sepal Length",

"Sepal Width",

"Petal Length",

"Petal Width"), selected = "Sepal Length") ),

mainPanel(

textOutput("selected_var"), plotOutput("plot_output") )

) )

(18)

Add a Plot - Server

server <- function(input, output) { output$selected_var <- renderText({

paste("Distribution of", input$var, "by Species") })

output$plot_output <- renderPlot({

variable<- switch(input$var,

"Sepal Length" = iris$Sepal.Length,

"Sepal Width" = iris$Sepal.Width,

"Petal Length" = iris$Petal.Length,

"Petal Width" = iris$Petal.Width )

data<-data.frame(variable=variable, Species=iris$Species) boxplot(data$variable~data$Species,las=1) })

}

switch: useful companion to multiple choice Shiny widgets

(19)

Result with a Plot - App

(20)

Summary

In the UI

*Inputfunction: create input

*Output function: place reactive objects in your Shiny app In the server

render* function: tell Shiny how to build the objects

surround R expressions by curly braces, {}, in eachrender*

function

save yourrender\*expressions in the output list, with one entry for each reactive object in your app, and

create reactivity by including an input value in arender\*

expression

(21)

Don’t forget

The directory that app.R appears in will become the working directory of the Shiny app

Shiny will run code placed at the start of app.R, before the server function, only once during the life of the app.

Shiny will run code placed inside server function multiple times, which can slow down the app.

use of reactive function

(22)

Reactivity

input$var is a reactive value

output$selected_var depends on input$var

if input$varchanges, thenoutput$selected_varreacts

All inputs are automatically reactive, so if you use any input inside arender* function, the output will re-render any time input changes

(23)

Reactive contexts

You can define your own reactive variables

Reactive values can only be used inside reactive contexts

Any render*function is a reactive context

Usereactive({...}) to assign a reactive variable

Useobserve({...}) to access a reactive variable Remember

reactive variable means anything that depends on it gets re-executed automatically

(24)

Reactive contexts - Assign a variable

#WRONG

server <- function(input, output) { x <- input$num + 1

}

#Right

server <- function(input, output) { x <- reactive({

input$num + 1 }) }

(25)

Reactive contexts - See a variable

#WRONG

server <- function(input, output) { print(input$num)

}

server <- function(input, output) { observe({

print(input$num) })

}

(26)

Sharing Shiny Apps

as R script

simplest

works only if the users have R on their own computer

as a web page

more user friendly

users only need web browser

how

host the app yourself (see with your IT departement)

use RStudio support: Shinyapps.io, Shiny Server (Pro)

(27)

Around Shiny

Shiny in RMarkdown document Header

---

title: "Shiny Document"

output: html_document runtime: shiny

---

in chunk

numericInput("rows", "How many cars?", 5) renderTable({

head(cars, input$rows) })

ShinyDashboard

(28)

More complex App

Références

Documents relatifs

Our goal is thus to describe input-output decoupling and linearization for nonlinear time- varying delay systems and to understand the problems that may arise when constructing

The Data Read is delayed from Data Write and may be shifted into an external register by using the negative-going edge of the Data Read Shift Clocks or the

With a parallel printer port, three serial ports (one standard and two optional), and a game port, the Magic I/O packs more features and connectors on a true

Ecrit un byte, en fait un int pour qu'il marche avec le read. void write(int b) Ne

public static void main(String[] args) throws IOException { InputStream in=new FileInputStream(args[0]);.. Et avec

It signals the central control or the processor (depending on the Medium Data Processing System) to au- tomatically translate EBCDIC to BCL informa- tion as it is

Input port lines and output port lines are accessed at 16-pin DIP sockets on the card.. A reset line is

The Magnetic Tape Result Descriptor has been extended to include Character Count, Begin- ning-of-Tape and End-of-Tape Flags, and a Flag for spacing over about