From 236e5f213daecf4f7f8a55def10e687d830037fc Mon Sep 17 00:00:00 2001 From: aagarw30 Date: Mon, 7 May 2018 07:14:04 +0530 Subject: [PATCH] added new files --- SelectAll_None_Choices/selectall1.R | 33 +++++++++++++++++++++++ SelectAll_None_Choices/selectall2.R | 42 +++++++++++++++++++++++++++++ SelectAll_None_Choices/selectall3.R | 39 +++++++++++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 SelectAll_None_Choices/selectall1.R create mode 100644 SelectAll_None_Choices/selectall2.R create mode 100644 SelectAll_None_Choices/selectall3.R diff --git a/SelectAll_None_Choices/selectall1.R b/SelectAll_None_Choices/selectall1.R new file mode 100644 index 0000000..0d9ffdd --- /dev/null +++ b/SelectAll_None_Choices/selectall1.R @@ -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) + + diff --git a/SelectAll_None_Choices/selectall2.R b/SelectAll_None_Choices/selectall2.R new file mode 100644 index 0000000..48b88f4 --- /dev/null +++ b/SelectAll_None_Choices/selectall2.R @@ -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) \ No newline at end of file diff --git a/SelectAll_None_Choices/selectall3.R b/SelectAll_None_Choices/selectall3.R new file mode 100644 index 0000000..5f60f13 --- /dev/null +++ b/SelectAll_None_Choices/selectall3.R @@ -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) \ No newline at end of file