我正在嘗試爲班級寫一個Shiny應用程序,我正在教這個應用程序,從數據集中抽取一個隨機樣本並計算總結統計。每當我按下UI上的重置按鈕時,都應該對新子集進行採樣。這裏是我的代碼到目前爲止:R Shiny not randomizing
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")
}
})
})
問題是,樣本不是隨機的;每次都是一樣的。即使按下重置按鈕,樣本也與之前的樣本完全相同。
爲什麼Shiny不隨機化?我怎樣才能使它再次隨機化?
這是不建議使用s在閃閃發光的元素名稱(例如。使用'Ch1_Prob_CT_reset'而不是'Ch1.Prob.CT.reset') –
此外,我很久沒有使用Shiny,所以我不太確定,但我會嘗試設置'Ch1.Prob.CT.obs 'data'無功元素中,* not *在觀察者中。如果你想在觀察者中更新它,你應該使用一個反應變量。 –