0

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

Verse he
  • 11
  • 3
  • try `renderText(paste(substring(input$Select_Table,1,1),".",input$demo, sep = ","))` ? – Deena Jul 21 '16 at 11:29
  • @Dee Will add a weird double comma as such `,.,` and im not too sure why. That was what my first though was. – amwill04 Jul 21 '16 at 11:32
  • @Dee change to `renderText(paste(substring(input$Select_Table,1,1),".",input$demo, collapse = " , "))` – amwill04 Jul 21 '16 at 11:34

1 Answers1

0

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.

amwill04
  • 1,330
  • 1
  • 11
  • 18
  • Hi , thanks for your contribution, but I'm looking for a way to remove the last comma other than replace the 'D'. – Verse he Jul 22 '16 at 05:20
  • @Versehe read my answer. you didnt provide the ability to create the `input` `select_table` so I had to subsitute. The `D` was simply an example. Provide the `input` otherwise. The answer you want is in the `collapse` argument of the `paste` function. With that it will add a `,` between each element given to `paste`. You currently add the comma as an argument, not the separator, hence why it will always add the last comma. – amwill04 Jul 22 '16 at 21:04
  • @ amwill04 , sorry I just realized that. I followed your step and it works well now, thank you very much, you really help me well. – Verse he Jul 25 '16 at 02:57