title: “Normal Distribution Simulation” output: flexdashboard::flex_dashboard: orientation: columns vertical_layout: fill theme: cosmo runtime: shiny —

ui <- fluidPage(
  theme = bslib::bs_theme(version = 5),
  titlePanel("A simulation of Normal Distribution"),
  
  sidebarLayout(
    mainPanel(
      tabsetPanel(
        tabPanel("Histogram", 
                 plotOutput("hist", height = "500px"),
                 verbatimTextOutput("summary")),
        tabPanel("Interactive Plot", 
                 plotlyOutput("plot3d", height = "600px")),
        tabPanel("About", 
                 includeMarkdown("about.md"))
      )
    ),
    sidebarPanel(
      sliderInput("n", "Sample Size:", 
                  min = 10, max = 1000, 
                  value = 100, step = 10),
      sliderInput("mean", "Population Mean:",
                  min = -5, max = 5, 
                  value = 0, step = 0.1),
      sliderInput("sd", "Population Standard Deviation:",
                  min = 0.1, max = 5, 
                  value = 1, step = 0.1),
      checkboxInput("show_normal", "Show Normal Curve", value = TRUE),
      actionButton("resample", "Generate New Sample"),
      width = 4
    )
  )
)
server <- function(input, output, session) {
  x <- reactiveVal(NULL)
  
  observeEvent(input$resample, {
    x(rnorm(input$n, mean = input$mean, sd = input$sd))
  })
  
  observe({
    if (is.null(x())) {
      x(rnorm(input$n, mean = input$mean, sd = input$sd))
    }
  })
  
  output$hist <- renderPlot({
    data <- data.frame(value = x())
    graph <- ggplot(data, aes(x = value)) +
      geom_histogram(aes(y = ..density..), bins = 30, fill = "skyblue", color = "white") +
      labs(title = "Histogram of Normal Distribution",
           x = "Value",
           y = "Density") +
      theme_minimal(base_size = 14)
    
    if (input$show_normal) {
      graph <- graph + stat_function(fun = dnorm, 
                             args = list(mean = input$mean, sd = input$sd),
                             color = "red", size = 1)
    }
    
    graph
  })
  
  output$plot3d <- renderPlotly({
    x_range <- seq(-5, 5, length.out = 100)
    mean_range <- seq(-3, 3, length.out = 50)
    sd_range <- seq(0.5, 2, length.out = 50)
    
    z <- outer(mean_range, sd_range, function(mean, sd) {
      dnorm(input$mean, mean = mean, sd = sd)
    })
    
    plot_ly() %>%
      add_surface(x = mean_range, y = sd_range, z = z,
                  colorscale = list(c(0, 1), c("orange", "red")))  %>%
      layout(scene = list(xaxis = list(title = "x = Mean"),
                          yaxis = list(title = "y = Standard Deviation"),
                          zaxis = list(title = "z = Probability Density")),
             title = "3D Visualization of Normal Distribution")
  })
  
  output$summary <- renderPrint({
    cat("Summary Statistics:\n")
    summary_stats <- summary(x())
    print(summary_stats)
    
    cat("\nSample Mean:", mean(x()), "\n")
    cat("Sample Standard Deviation:", sd(x()), "\n")
    
  })
}
renderPrint({
  cat("Summary Statistics:\n")
  summary_stats <- summary(x())
  print(summary_stats)
  
  cat("\nSample Mean:", mean(x()), "\n")
  cat("Sample Standard Deviation:", sd(x()), "\n")
    
  })

writeLines(
  c("# About Normal Distribution",
    "",
    "The normal distribution, also known as the Gaussian distribution, is a continous probability distribution that is symmetric about the mean, showing that data near the mean are more frequent in occurrence than data far from the mean. 
    The normal distribution describes how the values of a variable are distributed for a variety of phenomena. ",
    "",
    
    "## Key Features",
    "",
    "- **Bell-shaped curve**: The normal distribution has a characteristic \"bell-shaped\" curve.",
    "- **Symmetry**: It is symmetric around the center (mean, median, and mode are all equal).
    They’re all unimodal, and cannot model skewed distributions.",
    "",
    
    "- **Defined by two parameters**: The mean (μ) and standard deviation (σ) fully define the distribution.
        In the case of STANDARD normal distribution, the mean is zero and the standard deviation is 1. This distribution is also known as the Z-distribution.",
    "",
    
    "## Applications",
    "",
    "The normal distribution is widely used in natural and social sciences for real-valued random variables whose distributions are not known.",
    "",
    "## Central Limit Theorem",
    "",
    "The central limit theorem states that, under certain conditions, the mean of a sufficiently large number of independent random variables, each with a well-defined mean and variance, will approximate a normal distribution."),
"about.md"
)

shinyApp(ui = ui, server = server)
Shiny applications not supported in static R Markdown documents