如何删除 r Shiny 中多个 select 中的最后一个逗号

How to delete last comma in multiple select in r Shiny

我在使用 R shiny 工具时遇到了问题。当我使用多个 select 按钮时,我想在每个 select 离子的末尾添加一个逗号,这是我所做的:

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,","))

输出将是这样的:

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

然而,我不想要最后一个selection("D . Province ,"后面的)末尾的最后一个逗号,但到目前为止我还没有找到删除它的方法自动地。你能帮帮我吗?

非常感谢,

诗歌

由于您没有提供 input.select_table,只是 conditionalPanel,我已经调整了代码以获得一个工作示例。您基本上想将 paste 函数的最后一部分更改为 collapse= " , "。请注意逗号周围的 space,这是为了获得与您提供的格式相同的格式。因此:

## 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 = " , "))
})

我使用 "D" 而不是您的 substring(input$Select_Table,1,1),因为 OP 中没有提供。