【发布时间】:2020-11-12 23:08:28
【问题描述】:
我正在尝试为我的学生创建一个抽样分发应用程序。我希望能够向他们展示,当您采集另一个样本时,采样分布就会建立。我有“add1”和“add5”按钮,用于将那么多样本添加到我当前的样本集中。 模型用于为应用程序首次运行时生成的单个样本创建图形。这也使得“一个可能的样本图”可以匹配在抽样分布中绘制的值。创建 add1 或 add5 按钮后,代码是从 model1 的输出开始绘制图形。这部分正在工作。但是,当我再次单击按钮时,我们会返回到一个样本,并且按钮不再能够将更多样本添加到我的集合中。我不确定原因是否与按钮无法正常工作有关,或者我的值是否在模型 1 中被覆盖。
我的另一个顾虑(实际上可能是上面的问题)是,当我尝试在 model1 中将 props 设置为等于 model1()$props 时,在触发新按钮单击时本地环境已被删除,并且旧样本和 model1()$props 已经消失,因此 props 设置为等于 model()$props。有什么想法吗?!请和谢谢。
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)
})
})
【问题讨论】: