Search code examples
rshinyshiny-server

How to delete last comma in multiple select in r Shiny


I'm using R shiny tools and I meet a problem. When I use the multiple select button, I want to add a comma at the end of every selections,here is what I do:

UI.R

conditionalPanel("input.Select_Table == 'Demographics'",
               selectInput(inputId ="demo",label ="select variables you need", multiple = TRUE,
                             choices=c('Respondent_ID','year','month','City','City_Level','Province',
                             'Region','Actual_Age','Age_Level','Household_Income','Personal_Income_Level')))

Server.R

output$demo <- renderText(paste(substring(input$Select_Table,1,1),".",input$demo,","))

the output would be like this:

D . City , D . year , D . Province ,

however,I don't want the last comma at the end of last selection(the one behind"D . Province ,"), but so far I haven't find a way to delete it automatically. Could you please help me?

Thanks a lot,

Verse


Solution

  • As you didnt provide the input.select_table just the conditionalPanel I have tweaked the code to get a working example. You basically want to change the last part of the paste function to be collapse= " , ". Note the space around the comma, that is to get the same formatting as you provided. As such:

    ## ui.R
    
    shinyUI(fluidPage(
    
    selectInput(inputId ="demo",label ="select variables you need", multiple = TRUE,
                                    choices=c('Respondent_ID','year','month','City','City_Level','Province',
                                            'Region','Actual_Age','Age_Level','Household_Income','Personal_Income_Level')),
    
    
    mainPanel(
      textOutput("demo")
    )
    ))
    
    
    
    ## server.R
    
    shinyServer(function(input, output, session) {
    
    output$demo <- renderText(paste("D"," .",input$demo, collapse = " , "))
    })
    

    I used "D" instead of your substring(input$Select_Table,1,1) as that wasnt provided in the OP.