Skip to content

Commit

Permalink
added new files
Browse files Browse the repository at this point in the history
  • Loading branch information
aagarw30 committed May 7, 2018
1 parent 76aff50 commit 236e5f2
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
33 changes: 33 additions & 0 deletions SelectAll_None_Choices/selectall1.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Demo example - Adding 'select All' option to the selectinput using selectInput() and updateSelectInput()

library(shiny) # Load the required package

# vector with choices for select input
choices <- c("Select All", "choice 1", "choice 2", "choice 3")

######## ui code begins here #########
ui = fluidPage(
h5("Demo example - Adding 'select All' option to the selectinput to select all the choices at once using updateSelectInput(). "),
hr(),
selectInput("myselect", "Select box", choices=choices, multiple = TRUE, selected = "choice 1"),
verbatimTextOutput("selected") # to display the selected choices by user
)

##### server code begins here ##########
server = function (input, output, session) {
observe({
if("Select All" %in% input$myselect)
selected_choices=choices[-1] # choose all the choices _except_ "Select All"
else
selected_choices=input$myselect # update the select input with choice selected by user
updateSelectInput(session, "myselect", selected = selected_choices)
})

output$selected <- renderText({
paste(input$myselect, collapse = ",")
})
}

shinyApp(ui=ui, server=server)


42 changes: 42 additions & 0 deletions SelectAll_None_Choices/selectall2.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
## Select All/None option for choices using checkboxInput() and updateCheckboxGroupInput()
library(shiny)
# vector with column variable names from mtcars dataset
myChoices = names(mtcars)

########## ui code begins here ###########
ui = fluidPage(
h5("Demo example - 'Select All/None' option for choices using updateCheckboxGroupInput()"),
hr(),

# for selectAll/None toggle checkbox
checkboxInput('all', 'Select All/None', value = TRUE),

# for the variables names from mtcars dataset
checkboxGroupInput('mtcars', 'mtcars column variables', myChoices),

verbatimTextOutput("selected") # to display the selected choices by user

)

####### server code begns here ############
server = function(input, output, session) {

observe({

# if input$all is TRUE (basically a SELECT ALL option), all choices will be selected
# if input$all is FALSE (basically a NONE option), none of the choices will be selected

updateCheckboxGroupInput(
session, 'mtcars', choices = myChoices,
selected = if(input$all) myChoices
)

})

# Display the choices selected by the user
output$selected <- renderText({
paste(input$mtcars, collapse = ",")
})
}

shinyApp(ui=ui, server=server)
39 changes: 39 additions & 0 deletions SelectAll_None_Choices/selectall3.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
## Select All/None option for choices using updateSelectInput() & checkboxInput()
library(shiny)

# vector with column variable names from mtcars dataset
myChoices = names(mtcars)

########## ui code begins here ###########
ui = fluidPage(
h5("Demo example - 'Select All/None' option for choices using updateSelectInput() & checkboxInput()"),
hr(),

selectInput('mtcars', label="mtcars column variables", choices=myChoices, multiple = TRUE),
checkboxInput('all', 'Select All/None', value = TRUE), # gives user option to select all or none for choices
verbatimTextOutput("selected") # to display the selected choices by user
)

####### server code begns here ############
server = function(input, output, session) {

observe({
# if input$all is TRUE (basically a SELECT ALL option), all choices will be selected
# if input$all is FALSE (basically a NONE option), none of the choices will be selected

updateSelectInput(
session, 'mtcars', choices = myChoices,
selected = if(input$all) myChoices


)

})

# Display the choices selected by the user
output$selected <- renderText({
paste(input$mtcars, collapse = ",")
})
}

shinyApp(ui=ui, server=server)

0 comments on commit 236e5f2

Please sign in to comment.