I recently started playing with Shiny. I tried to write something to demonstrate a central limit theorem. my code is as follows:
ui.R:
#****************************************ui.R file code*****************************
library(shiny)
shinyUI(pageWithSidebar(headerPanel("Central Limit Theorem"),
sidebarPanel(selectInput("Distribution",
"Distribution:",
list("normal", "lognormal")),
br(),
sliderInput("sam_size",
"Sample size:",
min = 5,
max = 500,
value = 5)
),
mainPanel(tabPanel("Plot", plotOutput("plot")))
))
server.R:
library(shiny)
shinyServer(function(input, output){
data <- reactive(function(){Distribution <- switch(input$Distribution,
normal = rnorm,
lognormal = rlnorm,
rnorm
)
Distribution(input$sam_size*2000)})
output$plot <- reactive(function(){
Distribution <- input$Distribution
sam_size <- input$sam_size
temp <- matrix(data(), ncol=2000)
xbars <- colMeans(temp)
hist(xbars, main=paste("Sampling Distribution of the Mean Based on a", Distribution,
"distribution with n =", sam_size))})
})
When I tried to run the code using runApp(), below I got. As you can see, the graph is not displayed.

The strange part is that when I returned to my Rstudio and pressed βEscβ to exit the application, the graph is displayed in my Rstudio, as shown below:

I wonder if anyone knows what the problem is with my code. Thank!!
source
share