Home > Back-end >  Adding user Data in addition to list of values in select input in R UI, Looking for some thing in co
Adding user Data in addition to list of values in select input in R UI, Looking for some thing in co

Time:07-04

Sample 1

Would wish to add user input as well in addition to what has been already listed. Then I'm using a function to save the data entered to DB.

library(shiny)
ui <- fluidPage(
    titlePanel("Select or add Data"),
    sidebarLayout(
        sidebarPanel(
            selectInput("selectedregion","Select Region",multiple = FALSE,c("North", "East", "West"))
        ),

    mainPanel()
    )
)
server <- function(input, output) { }

shinyApp(ui = ui, server = server)

Sample 2

I wish to give option to the user to add "south" which is in addition to the data listed.

CodePudding user response:

The selectInput does not support user-defined entries but it's "companion" the selectizeInput does. With the argument options = list(create = TRUE) you enable that behaviour. Now you still need a listener in the server. An observercan do that. All you need to do now is to add your own code to add new values to the data base.

library(shiny)

ui <- fluidPage(
  titlePanel("Select or add Data"),
  sidebarLayout(
    sidebarPanel(
      selectizeInput("selectedregion", "Select Region",
                  multiple = FALSE, c("North", "East", "West"),
                  options = list(create = TRUE))
    ),
    
    mainPanel()
  )
)
server <- function(input, output) { 
  observe({
    req(input$selectedregion) # explicitly specify the dependency
    print(input$selectedregion) # This line is for testing purpose only
    # The newly added value is the selected value. If you want to store
    # those in a data base, add that code here.
  })
}

shinyApp(ui = ui, server = server)
  • Related