jeudi 20 août 2015

R Shiny not randomizing

I am trying to write a Shiny app for a class I'm teaching that draws a random sample from a dataset and computes summary statistics. Whenever I press the reset button on the UI, a new subset should be sampled. Here is my code so far:

shinyServer(function(input, output) {

# Output for Ch. 1 Problems: Central Tendency

# Prepare data
observeEvent(input$Ch1.Prob.CT.reset, {
    Ch1.Prob.CT.n <- sample(8:12, 1)
    Ch1.Prob.CT.obs <- sample(1:nrow(cars), Ch1.Prob.CT.n)
})
data <- eventReactive(input$Ch1.Prob.CT.reset, {
    cars[Ch1.Prob.CT.obs, 'dist', drop=F]
})

# Outputs
output$Ch1.Prob.CT.Data <- renderDataTable({
    data()
})

output$Ch1.Prob.CT.Mean.out <- renderUI({
    if (is.na(input$Ch1.Prob.CT.Mean.in)) { # Error checking
        p("No answer provided")
    } else if (round(input$Ch1.Prob.CT.Mean.in, digits = 4) == round(mean(Ch1.Prob.CT.data[,1]), digits = 4)) {
        p("Correct", style = "color:green")
    } else {
        p("Incorrect", style = "color:red")
    }
})
})

The problem is that the sample is not random; it is always the same, every time. Even when I press the reset button, the sample is exactly the same as the one before.

Why is Shiny not randomizing? And how can I make it randomize again?




Aucun commentaire:

Enregistrer un commentaire