0

I am trying to create an app where you are able to select the columns that you want to see.

This post helped me a lot: Shiny How to dynamically select columns of imported dataset for further analysis

However, I want to be able to select the columns if the user doesn't click on the options and writes the columns who wants to see.

Right now, in order to be able to select columns you need to click or write ONE column.

image1

However when you try to write more than 1 column (like this: "cyl mpg hp", in the same line) it doesn't appear anything.

image2

This is the code:

ui <- fluidPage(
    
    # Application title
    titlePanel("Old Faithful Geyser Data"),
    
    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            selectInput("select", "Select columns to display", c('col1,col2'), multiple = TRUE),
            actionButton("update", "Update Data set", class = "btn-primary",style='padding:4px; font-size:120%')
        ),
        
        # Show a plot of the generated distribution
        mainPanel(
            h2('The Mydata'),
            #tableOutput("mytable")
            DT::dataTableOutput("mytable")
            
        )
    )
)

library(shiny)
library(DT)

server <- function(session, input, output) {
    
    data <- reactive({
       mtcars
    })
    
    filtereddata <- eventReactive({
        input$update
        data()
    },  {
        req(data())
        if(is.null(input$select) || input$select == "")
            data() else 
                data()[, colnames(data()) %in% input$select]
    })
    
    observeEvent(data(), {
        updateSelectInput(session, "select", choices=colnames(data()))
    })
    
    output$mytable  <- renderDataTable(filtereddata())
    
} 

# Run the application 
shinyApp(ui = ui, server = server)

I have tried doing in another way, like here: https://shiny.rstudio.com/reference/shiny/1.6.0/varSelectInput.html but I have the same problem.

Thanks in advance

Regards

emr2
  • 1,436
  • 7
  • 23
  • Your code works perfectly for me. If I type `mpg` and `cyl` followed by `Enter` I get the mtcars table with only those two columns. – Julian_Hn Jun 03 '21 at 14:32
  • @Julian_Hn I don't understand. I only can get the table with the selected columns if I select one column and I click on the update button. What did you do? – emr2 Jun 03 '21 at 14:34
  • Exactly the same thing. I entered `mpg` and `cyl` and then clicked on the update button (or hit enter). The table immediately updates for me – Julian_Hn Jun 03 '21 at 14:36
  • @Julian_Hn but did you write those columns at the same time, in the same line? Because if I enter mpg and then cyl, the table updates perfectly. My problem is that when you are trying to write in the search button "mpg cyl", like this, the table doesn't update. That is the thing I want to fix. – emr2 Jun 03 '21 at 14:40
  • Aah ... sorry then I misunderstood your question, I'll see what I can do – Julian_Hn Jun 03 '21 at 14:46
  • @Julian_Hn don't worry, it is my fault. I have edited the post writing that example. Look forward to your reply if it is possible to do it! Thanks – emr2 Jun 03 '21 at 14:54
  • Okay ... So with selectinput this unfortunately does seem to be impossible. But why do you need the entries in one line in the first place? Does it really matter if you have to hit Enter or Space between entries? – Julian_Hn Jun 03 '21 at 15:19

1 Answers1

0

I have a way to pick n number of columns with one string containing a space (or more if there's a typo) per column name, but it requires a textInput. SelectInput doesn't allow me to enter a string.

It will show an error if only one column is typed, but that can be modified with an if statement.

example code:

library(stringr)
library(shiny)
library(tidyverse)
library(rebus)

ui <- fluidPage(
    
    # Application title
    titlePanel("Old Faithful Geyser Data"),
    
    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            textInput('select_text', 'Type a column', placeholder = 'enter col names between spaces'),
            actionButton("update", "Update Data set", class = "btn-primary",style='padding:4px; font-size:120%')
        ),
        
        # Show a plot of the generated distribution
        mainPanel(
            h2('The Mydata'),
            #tableOutput("mytable")
            DT::dataTableOutput("mytable")
            
        )
    )
)

library(shiny)
library(DT)

server <- function(session, input, output) {
    
    data <- reactive({
        mtcars
    })
    
    filtereddata <- eventReactive(
        input$update,  {
        
            cols_spaces <- str_split(input$select_text, rebus::one_or_more(rebus::SPACE),simplify = TRUE) #format the names to a vector
                data()[, colnames(data()) %in% cols_spaces] #now they can be used to subset data()
                
        }
                    
    )
    
    observeEvent(data(), {
        updateSelectInput(session, "select", choices=colnames(data()))
    })
    
    output$mytable  <- renderDataTable(filtereddata())
    
} 

# Run the application 
shinyApp(ui = ui, server = server)
jpdugo17
  • 6,816
  • 2
  • 11
  • 23