Using conditional statements with renderText

Hi,

I have been using r shiny for around 3 weeks now, primarily to make educational web apps for university courses. I am struggling with this particular problem, which I think is to do with conditional statements and renderText. So, I've created a button which changes the random variable to binomial, poisson or normal, and then the output would be (to begin with at least) a verbatim text output of the values, and underneath it, the same values, ordered in size. In my minimal example given underneath, you can see this is the case for the last option "normal" but the other two options don't output anything. I tried removing the last two options, which made the sole option "binomial" work, but apart from that, I'm not sure how to proceed.

Any advice (even about how I phrased this question, I'm kinda new here) on how to fix this would be much appreciated.

Thanks!

library(shiny)

#---------------------- 
ui <- bootstrapPage(
   headerPanel("Order Statistics"),
   sidebarPanel(
      selectInput("illustration", "Choose Distribution:",choices = c("Binomial" = "binom", "Poisson" = "poi","Normal"="norm")),
      numericInput("rstat", "rth order statistics:", 1, min = 1, max = 100),
      
      conditionalPanel(
         condition="input.illustration==\"binom\"",
         sliderInput("binom.n","Number of attempts",min=1,max=100,value=10),
         sliderInput("binom.p","Success probability p",min=0,max=1,value=0.5),
         sliderInput("binom.size","Size",min = 1,max = 10,value=1),
         sliderInput("binom.seed","Seed for Random Numbers",min=1,max=5000,value=1231,step=1)
      ),
      
      conditionalPanel(
         condition="input.illustration==\"poi\"",
         sliderInput("poi.n","Number of attempts",min=1,max=100,value=10),
         sliderInput("poi.lam",withMathJax("\\(\\lambda\\)"),min=0.1,max=10,value=1,step=0.1)
      ),
      
      conditionalPanel(
         condition="input.illustration==\"norm\"",
         sliderInput("norm.n",withMathJax("\\(n\\)"),min=1,max=100,value=1,step=1),
         sliderInput("norm.mean","\\(\\mu\\)",min=0.1,max=10,value=1,step=0.1),
         sliderInput("norm.sd","\\(\\sigma\\)",min=0.1,max=10,value=1,step=0.1)
      ),
     
   ),
   mainPanel(verbatimTextOutput("observations"),plotOutput("likelihood"))
)


#------------------------------------
server <- function(input, output) {
   output$observations <- renderText({
      
      if(input$illustration=="binom"){
      n <- input$binom.n
      p <- input$binom.p
      size <- input$binom.size
      set.seed(input$binom.seed)
      X <- rbinom(n,size,p)
      Xsorted <- sort(X,decreasing = FALSE)
      paste("Observations:",
            paste(X,collapse=", "),
            "Ordered Observations:",paste(Xsorted, collapse =", "),
            sep="\n")
      }
      
      if(input$illustration=="poi"){
      n <- input$poi.n
      lam <- input$poi.lam
      Y <- rpois(n,lam)
      Ysorted <- sort(Y,decreasing = FALSE)
      paste("Observations:",
            paste(Y,collapse=", "),
            "Ordered Observations:",paste(Ysorted, collapse =", "),
            sep="\n")
      }
      
      if(input$illustration=="norm"){
      n <- input$norm.n
      mean <- input$norm.mean
      sd <- input$norm.sd
      Z <- rnorm(m,mean,sd)
      Zsorted <- sort(Z,decreasing = FALSE)
      paste("Observations:",
            paste(Z,collapse=", "),
            "Ordered Observations:",paste(Zsorted, collapse =", "),
            sep="\n")
      }
   })
}


shinyApp(ui = ui, server = server)

This seems to work. Use the IF's to determine the form of the output but leave the actual writing of the output until the end of the renderText().

library(shiny)

#---------------------- 
ui <- bootstrapPage(
  headerPanel("Order Statistics"),
  sidebarPanel(
    selectInput("illustration", "Choose Distribution:",choices = c("Binomial" = "binom", "Poisson" = "poi","Normal"="norm")),
    numericInput("rstat", "rth order statistics:", 1, min = 1, max = 100),
    
    conditionalPanel(
      condition="input.illustration==\"binom\"",
      sliderInput("binom.n","Number of attempts",min=1,max=100,value=10),
      sliderInput("binom.p","Success probability p",min=0,max=1,value=0.5),
      sliderInput("binom.size","Size",min = 1,max = 10,value=1),
      sliderInput("binom.seed","Seed for Random Numbers",min=1,max=5000,value=1231,step=1)
    ),
    
    conditionalPanel(
      condition="input.illustration==\"poi\"",
      sliderInput("poi.n","Number of attempts",min=1,max=100,value=10),
      sliderInput("poi.lam",withMathJax("\\(\\lambda\\)"),min=0.1,max=10,value=1,step=0.1)
    ),
    
    conditionalPanel(
      condition="input.illustration==\"norm\"",
      sliderInput("norm.n",withMathJax("\\(n\\)"),min=1,max=100,value=1,step=1),
      sliderInput("norm.mean","\\(\\mu\\)",min=0.1,max=10,value=1,step=0.1),
      sliderInput("norm.sd","\\(\\sigma\\)",min=0.1,max=10,value=1,step=0.1)
    )
    
  ),
  mainPanel(verbatimTextOutput("observations"),plotOutput("likelihood"))
)


#------------------------------------
server <- function(input, output) {
  output$observations <- renderText({
    
    if(input$illustration=="binom"){
      n <- input$binom.n
      p <- input$binom.p
      size <- input$binom.size
      set.seed(input$binom.seed)
      X <- rbinom(n,size,p)
      Xsorted <- sort(X,decreasing = FALSE)
      Out <- paste("Observations:",
            paste(X,collapse=", "),
            "Ordered Observations:",paste(Xsorted, collapse =", "),
            sep="\n")
    }
    
    if(input$illustration=="poi"){
      n <- input$poi.n
      lam <- input$poi.lam
      Y <- rpois(n,lam)
      Ysorted <- sort(Y,decreasing = FALSE)
      Out <- paste("Observations:",
            paste(Y,collapse=", "),
            "Ordered Observations:",paste(Ysorted, collapse =", "),
            sep="\n")
    }
    
    if(input$illustration=="norm"){
      n <- input$norm.n
      mean <- input$norm.mean
      sd <- input$norm.sd
      Z <- rnorm(n,mean,sd)
      Zsorted <- sort(Z,decreasing = FALSE)
      Out <- paste("Observations:",
            paste(Z,collapse=", "),
            "Ordered Observations:",paste(Zsorted, collapse =", "),
            sep="\n")
    }
    Out
  })
}


shinyApp(ui = ui, server = server)

Yes, this works. One question though, I was looking for the substantive difference between what you did, and what I did - and it seems to be just the inclusion of the command

Out

After all the conditional statements, I was wondering what this did? I couldn't seem to find this in any of the help files.

Thank you!

Out is just a variable I added to the code. Its value stores the output of the paste() function at the end of each IF statement. For example, if

input$illustration=="poi"

is true, the output of the paste() within that IF is loaded into Out .

Out <- paste("Observations:",
            paste(X,collapse=", "),
            "Ordered Observations:",paste(Xsorted, collapse =", "),
            sep="\n")

Writing Out at the end of the renderText() simply prints the value of Out and that is what gets passed to the verbatimTextOutput() in the ui.

Aha! I understand now, I seem to have glossed over this part of the program. Thanks again!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.