14

I have an app wherein I am looking to take user input in the "ui" file and use that information to update a dataframe in the "server" file. The following is a simplified version of what the code looks like:

Dataframe <- readRDS(Dataframe.rds)
Table <- readRDS(Table.rds)    

ui <- fluidPage(
     selectInput("Location","Location",
              unique(as.character(Table$Locations)), multiple = TRUE)
                )

server <- function(input,output) {
 Dataframe2 <- Dataframe %>% select(get(input$Location))
                                 }

The above code works if I do not use the "multiple = TRUE" option for selectInput, meaning that the Dataframe2 object only selects the column that matches with the single input that the user has chosen. However, I do not know how I can do the same thing for multiple inputs, when the choices could vary from only 1 item being passed on from selectInput up to 10 items in total.

2 Answers 2

21

If I understood your question correctly this is an example with multiple selection using the mtcars data frame:

library(shiny)
library(dplyr)
data(mtcars)

ui <- fluidPage(
  titlePanel("MTCARS"),
  selectInput("Columns","Columns",
              names(mtcars), multiple = TRUE),
  verbatimTextOutput("dfStr")
)

server <- function(input, output) {
    Dataframe2 <- reactive({
            mtcars[,input$Columns] 
    })
    output$dfStr <- renderPrint({
            str(Dataframe2())
    })
}

shinyApp(ui, server)
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, how would you access each of the individual elements in the input$Columns vector? or is it a list? If for instance there was a for loop you couldn't use input$Columns[[i]] right? How would you do it?
Hi @InesGuardans. If you put print(class(input$Columns)) and after making a selection in the UI you will se the class printed bellow the str() command output. In this case it is a character vector. So you can use for example input$Columns[1]. Use length(input$Columns) to see how many selections you have. Check also for null since it is null when you start the app. Hope this helps!
0

Why don't you pass a vector into select function if you need more than one option?

    ui <- fluidPage(
  titlePanel("App"),
      selectInput('column', 'Select a column', names(DataFrame), multiple = TRUE)
      ),
    mainPanel(
      DT::DTOutput('table')))

server <- function(input, output){
  output$table <- DT::renderDT({
    DataFrame%>%
      select(c(input$column))
  })
}

I simplified but hopefully you got the idea, that worked for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.