Learn what the heck shiny is and how you use it!
library(data.table) library(shiny) defaultdisplay<-list( width="100%", height="75%" ) shinyAppDir( system.file("examples/06_tabsets", package="shiny"), options = defaultdisplay )
Board pack <!--
- Firstly, PowerBI is amazeballs
- It's excellent at allowing easy consumption
- It does not however facilitate a workflow
- Shiny does the dashboarding stuff + processing -->
A shiny application report consists of two functions[^1]
shinyServer()
shinyUI()
One says what to execute and the other states how to present it the results.
[^1]: Don't actually need to use these anymore
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.
lsp <- function(package, all.names = FALSE, pattern,...) { package <- deparse(substitute(package)) ls( pos = paste("package", package, sep = ":"), all.names = all.names, pattern = pattern ) } pages<-lsp(shiny,pattern = "Page") pages<-pages[-grep(pattern ="^update|Handler$",pages)] knitr::kable(data.table(`Page Types`=pages,` `=""))
inputs<-lsp(shiny,pattern = "Input") inputs<-inputs[-grep(pattern ="^update|Handler$",inputs)] knitr::kable(data.table(`Input controls p1`=inputs[1:6], `Input controls p2`=c(inputs[7:11],"") ))
outputs<-lsp(shiny,pattern = "Output") knitr::kable(data.table(`Output controls p1`=outputs[1:4], `Output controls p2`=outputs[5:8] ))
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) 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
package
- shiny is an easy framework
- shiny is very extendable
- shiny has a variety of deployment options
if (!require(devtools)) install.packages("devtools") devtools::install_github("stephlocke/Rtraining")
- Attendees
- Organisers and volunteers
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.