stata
stata

Reputation: 553

checkboxInput and conditionalPanel in shiny

When choose s1, only show one sidebarPanel and mainPanel,the results alpha was 0.05 and power was 0.8. When choose s2, only show one sidebarPanel and mainPanel,the results alpha was 0.1 and power was 0.9.

This is my ui.R and server.R.

Below is the code for my ui.R file:

library(shiny)
ui <- shinyUI(fluidPage(
  titlePanel("aaaaaaaaaaaaaaaa"),
  tabsetPanel(
    navbarMenu("Means",
           tabPanel("One Mean"),
           tabPanel("Two Means",
                    wellPanel(
                      checkboxInput(inputId = "s1", label = "S1", value = FALSE),
                      checkboxInput(inputId = "s2", label = "S2", value = FALSE)
                    ),
                    conditionalPanel(condition="input.s1==true",
                      sidebarPanel(
                        p(strong("Error Rates")),
                        numericInput("alpha", label="Alpha", min=0, max=1,value=0.05),
                        numericInput("power", "Power", 0.8),
                        actionButton("submit","Submit")
                      ),
                      mainPanel(
                        tabsetPanel(
                          tabPanel("Main",
                                   tableOutput("Table"),
                                   uiOutput("Text")
                          )
                        )
                      )
                    ),
                    conditionalPanel(condition="input.s2==true",
                                     sidebarPanel(
                                       p(strong("Error Rates")),
                                       numericInput("alpha", label="Alpha", min=0, max=1,value=0.1),
                                       numericInput("power", "Power", 0.9),
                                       actionButton("submit","Submit")
                                     ),
                                     mainPanel(
                                       tabsetPanel(
                                         tabPanel("Main",
                                                  tableOutput("Table"),
                                                  textOutput("Text")
                                         )
                                       )
                                     )
                    )
          )
    ))))

Below is the code for my server.R file:

server <- shinyServer(function(input, output) {
output$Table <- renderTable({
if(input$submit > 0) { 
  output<-data.frame(input$alpha,input$power)
  output
}
})

output$Text<-renderText({
if(input$submit > 0) {
  paste("alpha and power are",input$alpha,"and",input$power)
}
})
})

shinyApp(ui = ui, server = server)

Thank you very much.

Upvotes: -1

Views: 2611

Answers (2)

Eli Berkow
Eli Berkow

Reputation: 2725

I came across this question recently while researching a similar one.

This is not an alternative to the above answer but I thought it necessary to point out as the title of the question is checkboxInput and conditionalPanel in shiny.

The simple answer to the the checkboxInput condition at least is as below:

conditionalPanel(condition="input.s1==1",

and

conditionalPanel(condition="input.s2==1",

Obviously there is still the issue of unique IDs and the fact that radioButtons() is more appropriate in this context which are dealt with above.

Upvotes: 1

krish
krish

Reputation: 1438

This should work for you. You would need to slightly modify the server code to handle the different IDs. You cannot have the same id for the various UI elements.

ui <- shinyUI(fluidPage(
  titlePanel("aaaaaaaaaaaaaaaa"),
  tabsetPanel(
    navbarMenu("Means",
               tabPanel("One Mean"),
               tabPanel("Two Means",
                        wellPanel(
                          radioButtons(inputId = "buttons", "Selections", c("S1", "S2"), selected = "S1", inline = TRUE)
                        ),
                        sidebarPanel(
                          conditionalPanel(condition = "input.buttons == 'S1'",
                                           p(strong("Error Rates")),
                                           numericInput("alpha", label="Alpha", min=0, max=1,value=0.05),
                                           numericInput("power", "Power", 0.8),
                                           actionButton("submit","Submit")
                                           ),
                          conditionalPanel(condition = "input.buttons == 'S2'",
                                           p(strong("Error Rates")),
                                           numericInput("alpha1", label="Alpha", min=0, max=1,value=0.1),
                                           numericInput("power1", "Power", 0.9),
                                           actionButton("submit1","Submit")
                                           )
                        ),
                        mainPanel(
                          tabsetPanel(
                            tabPanel("Main",
                                     tableOutput("Table"),
                                     textOutput("Text")
                            )
                          )
                        )
               )
    ))))

Upvotes: 3

Related Questions