创建 Bootstrap 正态分布的闪亮应用程序,不刷新每次与actionButton,如何刷新图?

wswtfjt7  于 2022-12-30  发布在  Bootstrap
关注(0)|答案(1)|浏览(127)

我正在尝试创建一个 Bootstrap /中心极限定理的Shiny应用程序演示,它一般都能正常工作。然而,我打算有一个Shiny应用程序,每次点击“模拟”时,绘图都会刷新,并生成一个新的不同分布。然而,每次我打开应用程序并点击“模拟”时,绘图都保持静态和相同。
有没有人对如何解决/修复有什么想法?那将非常感激!

library(shiny)
library(tidyverse)

# Define the user interface for the app
ui <- fluidPage(
  
  # Add a title and sidebar
  titlePanel("Bootstrapped Distribution"),
  sidebarLayout(
    sidebarPanel(
      sliderInput("num_sims", "Number of Simulations:",
                  min = 1, max = 10000, value = 100),
      sliderInput("sampleSize", "Sample Size:",
                  min = 1, max = 10000, value = 100),
    selectInput("distT", "Distribution Type:",
                c("Normal" = "norm",
                  "Exponential" = "exp",
                  "Gamma" = "gam",
                  "Poisson" = "pois",
                  "Binomial" = "binom")),
    actionButton("simulate", "Simulate")
    ),
    
    # Add a plot to the main panel
    mainPanel(plotOutput("dist_plot"))
  )
)

# Define the server logic for the app
server <- function(input, output) {
  
  # Create a reactive expression to simulate the bootstrapped distribution
  #dist_data <- reactive({
  dist_data <- reactive({

    if(input$distT == "norm"){
      bootstrapped_mean <- replicate(input$num_sims, mean(rnorm(input$sampleSize)))
    }else if(input$distT == "exp"){
      bootstrapped_mean <- replicate(input$num_sims, mean(rexp(input$sampleSize)))
    }else if(input$distT == "gam"){
      bootstrapped_mean <- replicate(input$num_sims, mean(rgamma(input$sampleSize, shape=1)))
    }else if(input$distT == "pois"){
      bootstrapped_mean <- replicate(input$num_sims, mean(rpois(input$sampleSize, lambda = 1)))
    }else if(input$distT == "binom"){
      bootstrapped_mean <- replicate(input$num_sims, mean(rbinom(input$sampleSize, size = 1, prob = .5)))
    }
    ggplot(data=tibble(mean=bootstrapped_mean), aes(x=mean)) +
      geom_histogram(bins=30) + jtools::theme_apa()
  })

  # Update the plot when the "Simulate" button is clicked
  observeEvent(input$simulate, {
    output$dist_plot <- renderPlot({
      dist_data()
    })
  }
  
  
  )
}
kqlmhetl

kqlmhetl1#

建议:

  • 跳出dist_data返回 * 仅仅 * 数据,而不是试图把它也画出来。这个应用程序并没有从这一步中受益匪浅,但它是一个很好的方法来组织你正在做的事情。

例如,如果您稍后添加了一个汇总随机数据的表,那么您就没有一种简单的方法让该表获取随机数据,因为data_dist返回的是ggplot grob,而不是原始数据。

  • dist_dataReact性模块中添加对input$simulate的依赖项:这将导致每次按下按钮时重新生成随机数据。如果没有这个,数据将生成一次,并且永远不会重做。
  • output$dist_plot移出观察点(不再需要)。
library(shiny)
# library(tidyverse)
library(ggplot2)

# Define the user interface for the app
ui <- fluidPage(
  # Add a title and sidebar
  titlePanel("Bootstrapped Distribution"),
  sidebarLayout(
    sidebarPanel(
      sliderInput("num_sims", "Number of Simulations:",
                  min = 1, max = 10000, value = 100),
      sliderInput("sampleSize", "Sample Size:",
                  min = 1, max = 10000, value = 100),
      selectInput("distT", "Distribution Type:",
                  c("Normal" = "norm",
                    "Exponential" = "exp",
                    "Gamma" = "gam",
                    "Poisson" = "pois",
                    "Binomial" = "binom")),
      actionButton("simulate", "Simulate")
    ),

    # Add a plot to the main panel
    mainPanel(plotOutput("dist_plot"))
  )
)

# Define the server logic for the app
server <- function(input, output) {

  # Create a reactive expression to simulate the bootstrapped distribution
  dist_data <- reactive({
    req(input$simulate)
    bootstrapped_mean <-
      if (input$distT == "norm") {
        replicate(input$num_sims, mean(rnorm(input$sampleSize)))
      } else if (input$distT == "exp") {
        replicate(input$num_sims, mean(rexp(input$sampleSize)))
      } else if (input$distT == "gam") {
        replicate(input$num_sims, mean(rgamma(input$sampleSize, shape = 1)))
      } else if (input$distT == "pois") {
        replicate(input$num_sims, mean(rpois(input$sampleSize, lambda = 1)))
      } else if (input$distT == "binom") {
        replicate(input$num_sims, mean(rbinom(input$sampleSize, size = 1, prob = 0.5)))
      }
  })

  output$dist_plot <- renderPlot({
    X <- req(dist_data())
    ggplot(data = tibble(mean = X), aes(x = mean)) +
      geom_histogram(bins = 30) +
      jtools::theme_apa()
  })

}

shinyApp(ui, server)

相关问题