data science tutorials and snippets prepared by tomis9
shiny
and why would you use it?Sounds like a dream?
Advantages:
easy to learn the basics;
easy to set up.
Disadvantages:
scalability;
performance;
in order to make the application work the way you want to, you have to involve javascript, html and css. This may be cumbersome, as the documentation is not very helpful;
shiny is a nieche framework, so it’s community is little. Stackoverflow disappoints annoyingly often;
lack of a good book/tutorial. RStudio articles are not structured and I’ve spent hard time finding a learning path.
Concluding, I’ve got mixed feelings about shiny
. As a data scientist, maybe you should concentrate on fitting models and wrangling data instead of preparing a bright and shiny front-end.
You can store your application in one file (e.g. “app.R”), like this:
library(shiny)
inputBins <- 10
ui <- shinyUI(fluidPage(
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Choose your favourite number:",
min = 1,
max = 50,
value = 30)
),
mainPanel(
plotOutput("distPlot")
)
)
))
server <- shinyServer(function(input, output) {
r <- reactiveValues()
observeEvent(input$bins, {
r$bins <- input$bins
output$distPlot <- renderPlot(f())
})
f <- function() {
x <- faithful[, 2]
inputBins <- r$bins
bins <- seq(min(x), max(x), length.out = inputBins + 1)
hist(x, breaks = bins, col = 'darkgray', border = 'white')
}
})
shinyApp(ui, server)
and run it with
Rscript app.R
or divide it into two separate files:
ui.R
library(shiny)
ui <- shinyUI(fluidPage(
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Choose your favourite number:",
min = 1,
max = 50,
value = 30)
),
mainPanel(
plotOutput("distPlot")
)
)
))
server.R
library(shiny)
inputBins <- 10
server <- shinyServer(function(input, output) {
r <- reactiveValues()
observeEvent(input$bins, {
r$bins <- input$bins
output$distPlot <- renderPlot(f())
})
f <- function() {
x <- faithful[, 2]
inputBins <- r$bins
bins <- seq(min(x), max(x), length.out = inputBins + 1)
hist(x, breaks = bins, col = 'darkgray', border = 'white')
}
})
and run it with
Rscript -e "shiny::runApp()"
or from within the R console
shiny::runApp()