首页 文章

如何在R闪亮中添加交互式textinput面板

提问于
浏览
2
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是正态分布的参数) . 当我选择样本分布时,这些参数输入会弹出 .

我该怎么办?我应该使用哪种功能?

1 回答

  • 1

    如果你想保持参数输入隐藏,除非选择了一个分布,那么我认为你想要一个conditionalPanel .

    例如:

    conditionalPanel("!(output.plot)", textInput("size", "Size"))
    

    此大小的输入面板仅在尚未呈现输出$ plot时显示 .

    在你的情况下,我认为你可以以相同的方式使用输入 .

    conditionalPanel('input.sample_dist) != "Poisson"', textInput("parameter2", "Parameter 2"))
    

    仅当未选择泊松时,这将显示参数2 .

    您需要删除submitButton以确保在选择新分发时更新参数字段 . 如果要保持相同的功能,请将其替换为通用actionButton .

    我还建议使用numericInput字段,并在sample_dist更新时为服务器函数中的每个案例设置相应的标签,min,max和step以及updateNumericInput .

相关问题