Learn what the heck shiny is and how you use it!
2017-04-07
Learn what the heck shiny is and how you use it!
A shiny application report consists of two functions1
shinyServer()
shinyUI()
One says what to execute and the other states how to present it the results.
defaultdisplay<-list(width="100%", height="75%") shinyApp( ui = fluidPage() , server = function(input, output) {} , options = defaultdisplay )
You typically split into two files:
shinyServer()
shinyUI()
This can then be run with runApp()
You can do a single file example app.R
which contains both functions but this is typically better for very short apps.
Page Types | |
---|---|
basicPage | |
bootstrapPage | |
fillPage | |
fixedPage | |
fluidPage | |
navbarPage |
Input controls p1 | Input controls p2 |
---|---|
checkboxGroupInput | passwordInput |
checkboxInput | restoreInput |
dateInput | selectInput |
dateRangeInput | selectizeInput |
fileInput | sliderInput |
numericInput |
Output controls p1 | Output controls p2 |
---|---|
dataTableOutput | tableOutput |
htmlOutput | textOutput |
imageOutput | uiOutput |
plotOutput | verbatimTextOutput |
shinyApp( ui = fluidPage(dateInput("datePicker" , "Pick a date:" , format="dd/mm/yy")), server = function(input, output) {} ,options = defaultdisplay )
shinyApp( ui = fluidPage(sliderInput("vals" ,"Insert a number:" ,min=0 ,max=50 ,value=15 ,ticks=FALSE)) ,server = function(input, output) {} ,options = defaultdisplay )
shinyApp( ui = fluidPage(tags$textarea(id="charbox" ,rows=3 ,cols=40 ,"Default value")) ,server = function(input, output) {} ,options = defaultdisplay )
shinyApp( ui = fluidPage(selectInput("multiselect" ,"Pick favourites:" ,c("Green","Red","Blue") ,multiple=TRUE)) ,server = function(input, output) {} ,options = defaultdisplay )
shinyApp( ui = fluidPage(textInput("char", "Insert text:") , textOutput("text") ), server = function(input, output) { output$text <- renderText(input$char) } ,options = defaultdisplay )
quickShiny <- function(uFUN,sFUN,s) { require(shiny) defaultdisplay <- list(width = "100%", height = "75%") shinyApp( ui = fluidPage(uFUN("a")), server = function(input, output) { output$a <- sFUN(s) },options = defaultdisplay ) }
quickShiny(uFUN = DT::dataTableOutput, sFUN = DT::renderDataTable, s = head(iris,5))
quickShiny(uFUN = plotOutput, sFUN = renderPlot, s = pairs(iris[,1:3]))
library(leaflet) quickShiny(uFUN = leafletOutput, sFUN = renderLeaflet, s = leaflet() %>% addTiles() )
library(dygraphs) quickShiny(uFUN = dygraphOutput, sFUN = renderDygraph, s = dygraph(mdeaths))
library(DiagrammeR) quickShiny(uFUN = grVizOutput, sFUN = renderGrViz, s = render_graph(create_random_graph(3,3)))
library(plotly)
## ## Attaching package: 'plotly'
## The following object is masked from 'package:vegalite': ## ## add_data
## The following object is masked from 'package:ggplot2': ## ## last_plot
## The following object is masked from 'package:stats': ## ## filter
## The following object is masked from 'package:graphics': ## ## layout
quickShiny(uFUN = plotlyOutput, sFUN = renderPlotly, s = plot_ly(z = volcano, type = "surface"))
uilist<-list(textInput("a","Text","Txt") ,textOutput("b")) shinyApp(ui = fluidPage(uilist) ,server = function(input, output) { output$b <-renderText(paste0(input$a,"er")) },options = defaultdisplay )
shinyApp(ui = fluidPage(uilist) ,server = function(input, output) { rand<-reactive({gen<-rpois(1,5)+1 paste0(gen,input$a)}) output$b <-renderText(rand()) },options = defaultdisplay )
uilist[[3]]<-actionButton("go","Go") shinyApp(ui = fluidPage(uilist) ,server = function(input, output) { rand<-eventReactive(input$go ,{gen<-rpois(1,5)+1 paste0(gen,input$a)}) output$b <-renderText(rand()) },options = defaultdisplay )
rvest
shinythemes
shiny::runApp()
shinyApps
packageDownload everything:
if (!require(devtools)) install.packages("devtools") devtools::install_github("stephlocke/Rtraining")