Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R shiny; how to use multiple inputs from selectInput to pass onto 'select' option in dplyr?

Tags:

r

dplyr

shiny

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.

like image 399
Naj S Avatar asked Sep 30 '16 18:09

Naj S


1 Answers

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)
like image 92
Valter Beaković Avatar answered Nov 01 '22 11:11

Valter Beaković