使用R-shinny通过使用不同的分布来模拟CLT 库(闪亮) 图书馆(GG2) ui

使用R-shinny通过使用不同的分布来模拟CLT 库(闪亮) 图书馆(GG2) ui,r,shiny,R,Shiny,错误表示代码应该在反应性或观察语句中。看看我是如何包装和使用变量的 Warning: Error in .getReactiveEnvironment()$currentContext: Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.) S

错误表示代码应该在
反应性
观察
语句中。看看我是如何包装和使用变量的

Warning: Error in .getReactiveEnvironment()$currentContext: Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
Stack trace (innermost first):
    49: .getReactiveEnvironment()$currentContext
    48: .subset2(x, "impl")$get
    47: $.reactivevalues
    46: $ [#4]
    45: server [#4]
     4: <Anonymous>
     3: do.call
     2: print.shiny.appobj
     1: <Promise>
Error in .getReactiveEnvironment()$currentContext() : 
  Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
库(闪亮)
图书馆(GG2)

谢谢你,波克!还有一个问题:为什么这里要使用反应函数?我仍然对此感到困惑。嗯,这是故意的。如果您想使用客户机
输入
变量并对其进行操作,则必须将它们包装在动态调用中,例如函数,因为它必须能够在必要时转换为
HTML
。因此,所有的操作都必须作为一个带有括号的函数使用。
()
还有另一个bug。为什么模拟无法返回正确的结果?现在所有的样本一旦被模拟,实际上都是一个数字。例如,如果输入$sample.size*输入$simulation=1000,则所有1000个数字都是相同的。您必须自己检查代码。在创建变量时也尽量不要使用
Warning: Error in .getReactiveEnvironment()$currentContext: Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
Stack trace (innermost first):
    49: .getReactiveEnvironment()$currentContext
    48: .subset2(x, "impl")$get
    47: $.reactivevalues
    46: $ [#4]
    45: server [#4]
     4: <Anonymous>
     3: do.call
     2: print.shiny.appobj
     1: <Promise>
Error in .getReactiveEnvironment()$currentContext() : 
  Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
library(shiny)
library(ggplot2)

ui <- shinyUI(fluidPage(
  titlePanel("Central Limit Theorem Simulation"),
  sidebarLayout(
    sidebarPanel(
      numericInput("sample.size", "Size of each random sample", 
                   value = 30, min = 1, max = 100, step = 1),
      sliderInput("simulation", "THe number of simulation",
                  value = 100, min = 100, max = 1000, step = 1),
      selectInput("sample.dist", "Population Distribution where each sample is from",
                  choices = c("Binomial","Poisson", "Normal", "Uniform") ),
      numericInput("bins", "Number of bins in the histogram", 
                   value = 20, min = 1, max = 50, step = 1),
      submitButton(text = "Submit")
    ),
    mainPanel(
      h3('Illustrating outputs'),

      h4('mean of random sample mean'),
      textOutput(outputId = "output_mean" ),

      h4('variance of random sample mean'),
      textOutput(outputId = "output_var"),

      h4("Table"),
      tableOutput(outputId = "output_table"),

      h4('histogram of random normal sample'),
      plotOutput(outputId = "output_hist") 
    )
  )


))


server <- shinyServer(function(input, output) {

  # Return the random sample
  rsample <- reactive({
    ifelse(
      input$sample.dist == "Binomial", rbinom(input$sample.size * input$simulation, 1, 0.5),
      ifelse(input$sample.dist == "Poisson", rpois(input$sample.size * input$simulation, 1),
             ifelse(input$sample.dist == "Normal", rnorm(input$sample.size * input$simulation),
                    runif(input$sample.size * input$simulation) ) ) )
  })

  # Return the random sample matrix
  rsamplematrix <- reactive({
    matrix(rsample(), nrow = input$simulation)
  })

  # output mean of sample mean
  output$output_mean <- renderText({
    sample.mean <- rowMeans(rsamplematrix())
    mean(sample.mean)
  })

  # output variance of sample mean
  output$output_var <- renderText({
    sample.mean <- rowMeans(rsamplematrix())
    var(sample.mean)
  })

  # output histogram of sample mean
  output$output_hist <- renderPlot({
    sample.mean <- rowMeans(rsamplematrix())
    ggplot(data.frame(sample.mean), aes(x = sample.mean)) +
      geom_histogram(bins = input$bins)
  })

})


shinyApp(ui = ui, server = server)