The code to make a reactive table is relatively straightforward once you do a little refactoring of your existing server function to make the structure more clear:
server <- function(input, output) {
x_val <- eventReactive(input$submit_dist, {
simulate(input$distribution)
})
output$distPlot <- renderPlot({
hist(x_val(), breaks = input$bins)
})
output$table <- renderTable({
data <- hist(x_val(), breaks = input$bins, plot = FALSE)
data.frame(counts = data$counts)
})
}
Here I've pulled out a separate simulate function so the logic can live outside the app, removed the redundant req() (which doesn't appear to do anything hear), and removed the bins reactive since it was just a wrapper around an input. I also removed all the histogram display tweaking since that's not related to the problem at hand, and just makes it harder to see the overall structure.
When I pulled out the simulation code I also simplified it by using switch(), which I think makes the structure more clear:
simulate <- function(dist, n = 250) {
lambda1 <- sample.int(5:15, 1)
size1 <- sample.int(10:100, 1)
sampleprop <- sample(c(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9), 1)
switch(dist,
pois = rpois(n, lambda1),
binom = rbinom(n, size1, sampleprop),
unif = runif(n, max = lambda1),
stop("Unknown `dist`")
)
}
I also simplified your UI to make it easier to understand what the structure is:
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
radioButtons("distribution", label = "dist", choices = c("Poisson" = "pois", "Binomial" = "binom", "Uniform" = "unif")),
actionButton("submit_dist", "simulate"),
sliderInput("bins", "bins", min = 5, max = 15, value = 10, step = 1),
),
mainPanel(
plotOutput("distPlot"),
tableOutput("table")
)
)
)