0

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)
  })
})
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213

1 Answers1

1

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.

YBS
  • 19,324
  • 2
  • 9
  • 27
  • I did not realize that the buttons were not Boolean. I thought they were on off, not counters. This helped solve one problem! Thank you very much for your response. The second concern is still an issue though. – Victoria Woodard Nov 13 '20 at 17:25
  • A `checkbox` would be boolean. `actionButton` not. I am not sure I fully understand the other issue. A MRE would be useful to verify the issue. Perhaps the statement `props <- model1()$props` is an issue as it should not call inside the `model1()`. – YBS Nov 13 '20 at 17:36
  • Yes, I believe that is the main issue now. I'm just not sure how to store that value outside of model1 and then reuse it once model1 is called again. – Victoria Woodard Nov 13 '20 at 21:40
  • Please assign it to a `reactiveValues` object, and pull-in that information inside model1() – YBS Nov 14 '20 at 03:22