forked from aagarw30/R-Shinyapp-Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |