Reputation: 1
I am trying to create a sampling distribution app for my students. I want to be able to show them that when you take another sample, the sampling distribution builds. I have "add1" and "add5" buttons that are meant to add that many samples to my current set of samples. model is being used to create the graph for the single sample that is generated when the app is first run. This also makes it possible for a "One possible sample graph" to match the value being graphed in the sampling distribution. After the add1 or add5 buttons are created, the code is to start graphing from the output of model1. This part is working. However, when I click on the buttons again, we go back to one single sample and the buttons stop being able to add any more samples to my set. I'm not sure if the reason has to do with the buttons not working like I think they should or if my values are getting overwritten in model1.
My other concern (which may actually be the problem from above) is that when I try to set props equal to model1()$props, within model1, the local environment has been erased at the triggering of a new button click and the old samples and model1()$props are gone making is so that props is set equal to model()$props instead. Any thoughts?! Please and thank you.
server <- function(input, output) {
toListen <- reactive({
list(input$samps,input$prob, input$size, input$color1, input$color2)
})
model <- eventReactive(toListen(), {
p <- as.numeric(input$prob)
n <- as.numeric(input$size)
props <- data.frame(sum(rbinom(n,1,p))/n)
colnames(props) <- "props"
list(props = props, p = p, n = n, color1 = color1, color2 = color2)
})
toListen1 <- reactive({
list(input$add1, input$add5)
})
model1 <- eventReactive(toListen1(),{
p <- as.numeric(input$prob)
n <- as.numeric(input$size)
add1 <- 0
add5 <- 0
if(exists(paste('"model1()$props"'))){
props <- model1()$props
}
else
props <- model()$props
if(input$add1 ==1){
add1 <- 1
tempprops <- data.frame(sum(rbinom(n,1,p))/n)
colnames(tempprops) <- "props"
props <- data.frame(rbind(props, tempprops))
shinyjs::disable("add1")
}
if(input$add5 ==1){
add5 <- 1
tempprops <- matrix(NA, 5,1)
for (i in 1:5){
rand.samp <- rbinom(n,1,p)
tempprops[i] <- sum(rand.samp)/n
}
colnames(tempprops) <- "props"
props <- data.frame(rbind(props, tempprops))
shinyjs::disable("add5")
}
list(props = props, add1 = add1, add5 = add5)
})
})
Upvotes: 0
Views: 103
Reputation: 21287
Perhaps you should change
if(input$add1 ==1 ){
add1 <- 1
...
}
if(input$add5 ==1 ){...}
to
if(input$add1 >0 ){
add1 <- 1
...
}
if(input$add5 >0 ){...}
Otherwise, input$add1
is equal to 1 only once. Subsequent click will keep adding 1 more to the value of input$add1
. Same applies for input$add5
.
Upvotes: 1