 # R shiny: Plot the margin of error vs sample size. No plot at the end, help.

``````#create an app that plots the sample size agaist margin of error given sample size and standard deviation
#define UI
ui <- fluidPage(
h1("Central Limit Theorem - CLT"), #heading
titlePanel("Margin of error"), #add the label as an argument
sidebarLayout(
numericInput(inputId = "stanDev", label = "standard deviation", value = 10),
numericInput(inputId = "smpl", "sample size", value = 16),

),

mainPanel("Sample size against Margin of error!",
textOutput("plot"))
))
server <- function(input, output) {

calc_sem<-(1.96 * input\$stanDev/sqrt(input\$smpl))
population <- ceiling(runif(2700, 150, 250)) #model the population using 2700 uniformly drawn integers(assumption we can use any)

sample_sem<- for (i in 1:input\$smpl) {  #Calculate SEM for samples ranging in size from 1 to smpl entered
samp[[i]]<-sample(population, i)
sem[[i]]<-calc_sem(samp[[i]])}

#creating a DF and adding a col for the sample size
sem_df<-data.frame(sem)
sem_df["size"]<-c(1:input\$smpl)

# code for the plot
output\$plot <-renderPlot({
plot(sample_sem,aes(x = size, y = sem))+geom_line()+xlab("Sample Size")+ylab("Standard Error of the Mean")
}
}
#run the app
shinyApp(ui, server)``````

I recommend that you study the basics of shiny reactivity from
Chapter 3 Basic reactivity | Mastering Shiny (mastering-shiny.org)
in particular focus on section 3.3.4 Reactive expressions

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.