library(shiny)
library(ggplot2)
ui <- shinyUI(fluidPage(
titlePanel("Central Limit Theorem Simulation"),
sidebarLayout(
sidebarPanel(
numericInput("sample_size", "Size of each random sample\n(max: 100)",
value = 30, min = 1, max = 100, step = 1),
sliderInput("simulation", "THe number of simulation",
value = 100, min = 100, max = 1000, step = 1),
selectInput("sample_dist", "Population Distribution where each sample is from",
choices = c("Binomial","Poisson", "Normal", "Uniform")),
numericInput("bins", "Number of bins in the histogram\n(max: 50)",
value = 20, min = 1, max = 50, step = 1),
submitButton(text = "Submit")
),
mainPanel(
tabsetPanel(type = "pills",
tabPanel("mean of random sample mean", br(),
textOutput(outputId = "output_mean")),
tabPanel("variance of random sample mean", br(),
textOutput(outputId = "output_var")),
tabPanel("summary table", br(),
tableOutput(outputId = "output_table")),
tabPanel("sample matrix", br(),
verbatimTextOutput(outputId = "output_sample")),
tabPanel("histogram of random normal sample", br(),
plotOutput(outputId = "output_hist"))
)
)
)
))
server <- shinyServer(function(input, output) {
# Return the random sample
rsample <- reactive({
if (input$sample_dist == "Binomial") {
rsample <- rbinom(input$sample_size * input$simulation, 1, 0.5)
} else if (input$sample_dist == "Poisson") {
rsample <- rpois(input$sample_size * input$simulation, 1)
} else if (input$sample_dist == "Normal") {
rsample <- rnorm(input$sample_size * input$simulation)
} else {
rsample <- runif(input$sample_size * input$simulation)
}
rsample
})
# Return the random sample matrix
rsamplematrix <- reactive({
matrix(rsample(), nrow = input$simulation)
})
# output mean of sample mean
output$output_mean <- renderText({
sample_mean <- rowMeans(rsamplematrix())
mean(sample_mean)
})
# output variance of sample mean
output$output_var <- renderText({
sample_mean <- rowMeans(rsamplematrix())
var(sample_mean)
})
# output summary table of sample mean
output$output_table <- renderTable({
sample_mean <- rowMeans(rsamplematrix())
data.frame(mean(sample_mean), var(sample_mean))
})
# output the first 5 rows and 5 columns of the sample matrix
output$output_sample <- renderPrint({
k = rsamplematrix()
k[1:5, 1:5]
})
# output histogram of sample mean
output$output_hist <- renderPlot({
sample_mean <- rowMeans(rsamplematrix())
ggplot(data.frame(sample_mean), aes(x = sample_mean, y = ..density..)) +
geom_histogram(bins = input$bins, fill = "steelblue", col = "white")
})
})
shinyApp(ui = ui, server = server)
でインタラクティブのTextInputパネルを追加する方法上記のコードはうまく動作します。今、各分布に対して分布パラメータのテキスト入力を追加したいとします(たとえば、pは二項分布のパラメータ、muおよびsigmaは正規分布のパラメータです)。これらのパラメータは、サンプルの配布を選択したときにポップアウトします。光沢のあるR
どうすればよいですか?どの機能を使うべきですか?