Introduction to Shiny Applications for Basic Histograms

I
n
t
r
o
d
u
c
t
i
o
n
 
t
o
 
S
h
i
n
y
Linda Dib and Frédéric Schütz
Your turn: Exercise 1
     
Build and learn
My Fist Shiny App
ui
<- fluidPage(
  titlePanel("Workshop - Example 1 – Basic  Histogram"),
  sidebarLayout(
       sidebarPanel(
      
  
numericInput(inputId="n", label="Number of observations", value=1000)
 
       ),
       mainPanel(plotOutput("plot"))
  )
)
server
<-function(input, output) {
   data <- reactive({
        x <- rnorm(input$n)
        x
   })
output$plot <- renderPlot({
         
 
hist(data(), 50,main="",  xlab="x")
   })
}
shinyApp(ui,server)
inputId and outputId
Solution
 Change the application app1
to add the bins
shinyUI(fluidPage(
  titlePanel("Workshop - Example 1 – Basic
Histogram"),
  sidebarLayout(
       sidebarPanel(
              numericInput(inputId="n",
 
label="Number of observations",
 
value=1000),
              
numericInput(inputId="bins",
                    label="Number of bins",value=50)
 
       ),
       mainPanel(
plotOutput("plot")
      )
  )
))
shinyServer(function(input, output) {
   data <- reactive({
        x <- rnorm(input$n)
        x
   })
output$plot <- renderPlot({
         
 
hist(data(),
input$bins
,main="",
  
xlab="x")
   })
})
Solution
: How to add text
shinyUI(fluidPage(
  titlePanel("Workshop - Example 1 – Basic
Histogram"),
  sidebarLayout(
       sidebarPanel(
              numericInput(inputId="n",
 
label="Number of observations",
 
value=1000),
              numericInput(inputId="bins",
                    label="Number of bins",value=50)
 
       ),
       mainPanel(
uiOutput(“text”),
          plotOutput("plot")
      )
  )
))
shinyServer(function(input, output) {
   data <- reactive({
        x <- rnorm(input$n)
        x
   })
   output$text <- renderText({
             "<h2>My first Shiny app!</h2>"
  })
   output$plot <- renderPlot({
         
 
hist(data(),input$bins,main="",
  
xlab="x")
   })
})
Solution
: Mean and stdv
shinyUI(fluidPage(
  titlePanel("Workshop - Example 1 - Basic Histogram"),
  sidebarLayout(
      sidebarPanel(
            numericInput(inputId="n", label="Number of
                            observations",value=1000),
            numericInput(inputId="bins", label="Number of
                             bins",value=50),
            numericInput(inputId="mu",
                             label="Mean",value=0),
            numericInput(inputId="sig", label="Standard
                              Deviation",value=1),
            textInput("ttl","Graph Title",value="")
      ),
      mainPanel(
            uiOutput("text"),
            plotOutput("plot")
      )
  )
))
shinyServer(function(input, output) {
   data <- reactive({
        x <- rnorm(input$n,input$mu,input$sig)
        x
   })
   output$text <- renderText({
             line <- paste("<h4>Mean:",input$mu,"</h4>")
             line[2] <- paste("<h4>Std:",input$sig,"</h4>")
             line
   })
   output$plot <- renderPlot({
          hist(data(),input$bins,
main=input$ttl
,xlab="x")
   })
})
Solution
: Mean and stdv
Solution
: different displays
shinyUI(fluidPage(
  titlePanel("Workshop - Example 1 - Basic Graphs"),
  sidebarLayout(
      sidebarPanel(
            numericInput(inputId="n", label="Number of
observations",value=1000),
            numericInput(inputId="bins",
 
label="Number of bins",value=50),
            numericInput(inputId="mu",
 
label="Mean",value=0),
            numericInput(inputId="sig",
 
label="Standard Deviation",value=1),
            textInput("ttl","Graph Title",value=""),
            radioButtons(inputId="whichgraph",
 
label="Which Graph?",
 
choices=c("Histogram","Boxplot")
         ),width=3
      ),
      mainPanel(
            uiOutput("text"),
            plotOutput("plot", width = "500px", height =
"500px")
      )
  )
))
shinyServer(function(input, output) {
   data <- reactive({
        x <- rnorm(input$n,input$mu,input$sig)
        x
   })
   output$text <- renderText({
             line <- paste("<h4>Mean:",input$mu,"</h4>")
             line[2] <- paste("<h4>Std:",input$sig,"</h4>")
             line
   })
   output$plot <- renderPlot({
       if(input$whichgraph=="Histogram")
           hist(data(),input$bins,main=input$ttl,xlab="x")
       else
           boxplot(data(),main=input$ttl,xlab="x")
   })
})
Solution
: different displays
shinyUI(fluidPage(
  titlePanel("Workshop - Example 1 - Basic Graphs"),
  sidebarLayout(
      sidebarPanel(
            numericInput(inputId="n", label="Number of observations",value=1000),
            
conditionalPanel(condition = "input.whichgraph=='Histogram'",
                   numericInput(inputId="bins", label="Number of bins",value=50)
            ),
            numericInput(inputId="mu", label="Mean",value=0),
            numericInput(inputId="sig", label="Standard Deviation",value=1),
            textInput("ttl","Graph Title",value=""),
            radioButtons(inputId="whichgraph",label="Which Graph?",
                   choices=c("Histogram","Boxplot")),width=3
      ),
      mainPanel(
            uiOutput("text"),
            plotOutput("plot", width = "500px", height = "500px")
      )
  )
))
Solution
: Panel appear & disappear:
shinyUI(fluidPage(
  titlePanel("Workshop - Example 1 - Basic Graphs"),
  sidebarLayout(
      sidebarPanel(
           
selectInput("dataset", HTML("<h5>Choose a dataset:</h5>"),
                  choices = c("Newcomb's Speed of Light",
 
"Weight of Euro Coins","Forbes
 
500","Random"),selected="Random"),
           conditionalPanel(condition = "input.dataset=='Random'",
               numericInput(inputId="n", label="Number of 
 
 
observations",value=1000),
               conditionalPanel(condition = 
   
input.whichgraph=='Histogram'",
                   numericInput(inputId="bins", label="Number of 
  
 
bins",value=50)
               ),
               numericInput(inputId="mu", label="Mean",value=0),
               numericInput(inputId="sig", label="Standard 
  
 
Deviation",value=1),
               textInput("ttl","Graph Title",value="")
            ),
            radioButtons(inputId="whichgraph",label="Which Graph?",
                  choices=c("Histogram","Boxplot")),width=3
      ),
      mainPanel(
            uiOutput("text"),
            plotOutput("plot", width = "500px", height = "500px")
      )
  )
))
shinyServer(function(input, output) {
   data <- reactive({
        if(input$dataset=="Random")
             return(rnorm(input$n,input$mu,input$sig))
        if(input$dataset=="Newcomb's Speed of Light") {
            source("newcomb.R")
            return(newcomb)
        }
        if(input$dataset=="Weight of Euro Coins") {
            source("euros.R")
            return(euros)
        }
        if(input$dataset=="Forbes 500") {
            source("forbes.R")
            return(forbes$Assets)
        }
   })
   output$text <- renderText({
        if(input$dataset!="Random") return("")
        line <- paste("<h4>Mean:",input$mu,"</h4>")
        line[2] <- paste("<h4>Std:",input$sig,"</h4>")
        line
   })
   output$plot <- renderPlot({
       if(input$dataset=="Random") ttl<-input$ttl
       else ttl <- input$dataset
       if(input$whichgraph=="Histogram")
           hist(data(),input$bins,main=ttl,xlab="x")
       else
           boxplot(data(),main=ttl,xlab="x")
   })
})
Solution: 
File Input
Solution
: Text Output
 output$text <- renderText({
        x <- data()
        line <- "<table border=1>"
        line[2] <- "<tr><th>Sample Size</th>
 
          <th>Mean</th><th>Standard
Deviation</th></tr>"
        line[3] <- paste("<tr><td>",length(x),
                         "</td><td>",round(mean(x),2),
                         "</td><td>",round(sd(x),3),"</td></tr>")
        line[4] <- "</table>"
        line
   })
Solution
: Tables appearance
shinyUI(fluidPage(
  tags$head(
      tags$style(HTML("
          table, th, td {
              text-align:right;
          }
          th, td {
              padding: 10px;
          }
       "))
  ),
  titlePanel("Workshop - Example 1 - Basic Graphs"),
Solution
: Panels
 mainPanel(
            tabsetPanel(
                tabPanel("Statistics",uiOutput("text")),
                tabPanel("Graphs",plotOutput("plot",
width =
    
"500px", height = "500px")),
                id="Tabs"
            )
     )
conditionalPanel( condition = "input.Tabs == 'Graphs'",
                  radioButtons(inputId="whichgraph",
  
label="Which Graph?",
                         choices=c("Histogram","Boxplot")),
                  conditionalPanel(condition =
  
        "input.dataset=='Random'",
                      
 
textInput("ttl","Graph Title",value=""))
            )
Solution
: Selection
Solution
: Animation
In ui.R:
 sliderInput("k","Repeat!",min=1, max=10, value=0,step=1,
                animate=animationOptions(interval = 500,playButton=“Go!")
            )
In server.R:
 if(input$dataset=="Random") {
            for(i in 1:input$k) mu<-input$mu
            return(rnorm(input$n,input$mu,input$sig))
        }
Solution
: Using libraries
In server.R:
require(ggplot2)
shinyServer(function(input, output) {
 output$plot <- renderPlot({
       if(input$dataset=="Random") ttl<-input$ttl
       else ttl <- input$dataset
       dta<-data.frame(x=data())
       if(input$whichgraph=="Histogram") {
                  bw <- diff(range(data()))/input$bins
                  plt <- ggplot(data=dta,aes(x))+
                  geom_histogram(aes(y = ..density..),color="black",fill="white", binwidth = bw)
       }
       else
          plt <- ggplot(data=dta,aes(factor(1,length(x)),x))+
                  geom_boxplot()
       plt <- plt + xlab(ttl)+ylab("")
       print(plt)
   })
Slide Note
Embed
Share

Explore the basics of creating Shiny applications for histograms using R programming. Learn how to build interactive plots, customize input parameters, and add text elements to enhance user experience.

  • Shiny Applications
  • Basic Histograms
  • R Programming
  • Data Visualization
  • Interactive Plots

Uploaded on Sep 19, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Introduction to Shiny Linda Dib and Fre de ric Schu tz www.sib.swiss

  2. Your turn: Exercise 1 Build and learn

  3. My Fist Shiny App

  4. inputId and outputId ui<- fluidPage( titlePanel("Workshop - Example 1 Basic Histogram"), sidebarLayout( sidebarPanel( numericInput(inputId="n", label="Number of observations", value=1000) ), mainPanel(plotOutput("plot")) ) ) server<-function(input, output) { data <- reactive({ x <- rnorm(input$n) x }) output$plot <- renderPlot({ hist(data(), 50,main="", xlab="x") }) } shinyApp(ui,server)

  5. Solution Change the application app1 to add the bins shinyUI(fluidPage( shinyServer(function(input, output) { titlePanel("Workshop - Example 1 Basic Histogram"), data <- reactive({ x <- rnorm(input$n) x }) output$plot <- renderPlot({ hist(data(),input$bins,main="", xlab="x") }) }) sidebarLayout( sidebarPanel( numericInput(inputId="n", label="Number of observations", value=1000), numericInput(inputId="bins", label="Number of bins",value=50) ), mainPanel( plotOutput("plot") ) ) ))

  6. Solution: How to add text shinyServer(function(input, output) { shinyUI(fluidPage( data <- reactive({ x <- rnorm(input$n) x }) output$text <- renderText({ "<h2>My first Shiny app!</h2>" }) output$plot <- renderPlot({ hist(data(),input$bins,main="", xlab="x") }) }) titlePanel("Workshop - Example 1 Basic Histogram"), sidebarLayout( sidebarPanel( numericInput(inputId="n", label="Number of observations", value=1000), numericInput(inputId="bins", label="Number of bins",value=50) ), mainPanel( uiOutput( text ), plotOutput("plot") ) ) ))

  7. Solution: Mean and stdv

  8. Solution: Mean and stdv shinyServer(function(input, output) { shinyUI(fluidPage( data <- reactive({ x <- rnorm(input$n,input$mu,input$sig) x }) titlePanel("Workshop - Example 1 - Basic Histogram"), sidebarLayout( sidebarPanel( numericInput(inputId="n", label="Number of observations",value=1000), numericInput(inputId="bins", label="Number of bins",value=50), numericInput(inputId="mu", label="Mean",value=0), numericInput(inputId="sig", label="Standard Deviation",value=1), textInput("ttl","Graph Title",value="") ), mainPanel( uiOutput("text"), plotOutput("plot") ) ) )) output$text <- renderText({ line <- paste("<h4>Mean:",input$mu,"</h4>") line[2] <- paste("<h4>Std:",input$sig,"</h4>") line }) output$plot <- renderPlot({ hist(data(),input$bins,main=input$ttl,xlab="x") }) })

  9. Solution: different displays

  10. Solution: different displays shinyUI(fluidPage( titlePanel("Workshop - Example 1 - Basic Graphs"), sidebarLayout( sidebarPanel( numericInput(inputId="n", label="Number of observations",value=1000), numericInput(inputId="bins", label="Number of bins",value=50), numericInput(inputId="mu", label="Mean",value=0), numericInput(inputId="sig", label="Standard Deviation",value=1), textInput("ttl","Graph Title",value=""), radioButtons(inputId="whichgraph", label="Which Graph?", choices=c("Histogram","Boxplot") ),width=3 ), mainPanel( uiOutput("text"), plotOutput("plot", width = "500px", height = "500px") ) ) )) shinyServer(function(input, output) { data <- reactive({ x <- rnorm(input$n,input$mu,input$sig) x }) output$text <- renderText({ line <- paste("<h4>Mean:",input$mu,"</h4>") line[2] <- paste("<h4>Std:",input$sig,"</h4>") line }) output$plot <- renderPlot({ if(input$whichgraph=="Histogram") hist(data(),input$bins,main=input$ttl,xlab="x") else boxplot(data(),main=input$ttl,xlab="x") }) })

  11. Solution: Panel appear & disappear: shinyUI(fluidPage( titlePanel("Workshop - Example 1 - Basic Graphs"), sidebarLayout( sidebarPanel( numericInput(inputId="n", label="Number of observations",value=1000), conditionalPanel(condition = "input.whichgraph=='Histogram'", numericInput(inputId="bins", label="Number of bins",value=50) ), numericInput(inputId="mu", label="Mean",value=0), numericInput(inputId="sig", label="Standard Deviation",value=1), textInput("ttl","Graph Title",value=""), radioButtons(inputId="whichgraph",label="Which Graph?", choices=c("Histogram","Boxplot")),width=3 ), mainPanel( uiOutput("text"), plotOutput("plot", width = "500px", height = "500px") ) ) ))

  12. Solution: File Input shinyUI(fluidPage( titlePanel("Workshop - Example 1 - Basic Graphs"), sidebarLayout( sidebarPanel( selectInput("dataset", HTML("<h5>Choose a dataset:</h5>"), choices = c("Newcomb's Speed of Light", "Weight of Euro Coins","Forbes 500","Random"),selected="Random"), conditionalPanel(condition = "input.dataset=='Random'", numericInput(inputId="n", label="Number of observations",value=1000), conditionalPanel(condition = input.whichgraph=='Histogram'", numericInput(inputId="bins", label="Number of bins",value=50) ), numericInput(inputId="mu", label="Mean",value=0), numericInput(inputId="sig", label="Standard Deviation",value=1), textInput("ttl","Graph Title",value="") ), radioButtons(inputId="whichgraph",label="Which Graph?", choices=c("Histogram","Boxplot")),width=3 ), mainPanel( uiOutput("text"), plotOutput("plot", width = "500px", height = "500px") ) ) )) shinyServer(function(input, output) { data <- reactive({ if(input$dataset=="Random") return(rnorm(input$n,input$mu,input$sig)) if(input$dataset=="Newcomb's Speed of Light") { source("newcomb.R") return(newcomb) } if(input$dataset=="Weight of Euro Coins") { source("euros.R") return(euros) } if(input$dataset=="Forbes 500") { source("forbes.R") return(forbes$Assets) } }) output$text <- renderText({ if(input$dataset!="Random") return("") line <- paste("<h4>Mean:",input$mu,"</h4>") line[2] <- paste("<h4>Std:",input$sig,"</h4>") line }) output$plot <- renderPlot({ if(input$dataset=="Random") ttl<-input$ttl else ttl <- input$dataset if(input$whichgraph=="Histogram") hist(data(),input$bins,main=ttl,xlab="x") else boxplot(data(),main=ttl,xlab="x") }) })

  13. Solution: Text Output output$text <- renderText({ x <- data() line <- "<table border=1>" line[2] <- "<tr><th>Sample Size</th> <th>Mean</th><th>Standard Deviation</th></tr>" line[3] <- paste("<tr><td>",length(x), "</td><td>",round(mean(x),2), "</td><td>",round(sd(x),3),"</td></tr>") line[4] <- "</table>" line })

  14. Solution: Tables appearance shinyUI(fluidPage( tags$head( tags$style(HTML(" table, th, td { text-align:right; } th, td { padding: 10px; } ")) ), titlePanel("Workshop - Example 1 - Basic Graphs"),

  15. Solution: Panels mainPanel( tabsetPanel( tabPanel("Statistics",uiOutput("text")), tabPanel("Graphs",plotOutput("plot", width = "500px", height = "500px")), id="Tabs" ) )

  16. Solution: Selection conditionalPanel( condition = "input.Tabs == 'Graphs'", radioButtons(inputId="whichgraph", label="Which Graph?", choices=c("Histogram","Boxplot")), conditionalPanel(condition = "input.dataset=='Random'", textInput("ttl","Graph Title",value="")) )

  17. Solution: Animation In ui.R: sliderInput("k","Repeat!",min=1, max=10, value=0,step=1, animate=animationOptions(interval = 500,playButton= Go!") ) In server.R: if(input$dataset=="Random") { for(i in 1:input$k) mu<-input$mu return(rnorm(input$n,input$mu,input$sig)) }

  18. Solution: Using libraries In server.R: require(ggplot2) shinyServer(function(input, output) { output$plot <- renderPlot({ if(input$dataset=="Random") ttl<-input$ttl else ttl <- input$dataset dta<-data.frame(x=data()) if(input$whichgraph=="Histogram") { bw <- diff(range(data()))/input$bins plt <- ggplot(data=dta,aes(x))+ geom_histogram(aes(y = ..density..),color="black",fill="white", binwidth = bw) } else plt <- ggplot(data=dta,aes(factor(1,length(x)),x))+ geom_boxplot() plt <- plt + xlab(ttl)+ylab("") print(plt) })

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#