Is there a way to split the characters in selectinput

From the below application, I getting the output as shown below

enter image description here

library(shiny)
library(DT)

nba <- data.frame(
  player = c("c('James','James1')", "Durant", "Curry", "Harden", "Paul", "Wade"), 
  team = c("CLEOH", "GSWOAK", "GSWOAK", "HOUTX", "HOUTX", "CLEOH"),
  day1points = c("25","23","30","41","26","20"), 
  day2points = c("24","25","33","45","26","23"),
  rating=c("1","2","3","4","5","1")
)

ui <- navbarPage(
  title="SADDAS",
  sidebarLayout(
    sidebarPanel(uiOutput("var1_select")),
    mainPanel(tableOutput("reportOutput"))
  ))

server <- function(input, output) {
  output$var1_select <- renderUI({
    selectInput(
      "ind_var_select",
      "Select Names", 
      choices = as.character(nba[,1] ),
      multiple = TRUE,
      selected = as.character(nba[1,1]),
    )
  })
  
  output$reportOutput = renderTable({
    # Filter it
    subset(nba[,1:3], player %in% input$ind_var_select)
  }, options = list(scrollX = TRUE))
}

shinyApp(ui, server)

But is there a way to split it like shown below

Expected output. Basically I am trying to split the characters in selectinput

enter image description here

Note : Please forget the table at the right:)

I think you are asking to do something like this:


nba <- data.frame(
  player = c("c('James','James1')", "Durant", "Curry", "Harden", "Paul", "Wade"), 
  team = c("CLEOH", "GSWOAK", "GSWOAK", "HOUTX", "HOUTX", "CLEOH"),
  day1points = c("25","23","30","41","26","20"), 
  day2points = c("24","25","33","45","26","23"),
  rating=c("1","2","3","4","5","1"),
  stringsAsFactors = FALSE
)


splitter <- function(x){
  if(startsWith(x,"c('") & endsWith(x,"')")){
    strsplit(substr(x,4,nchar(x)-2),
             "','") %>% unlist()
  } else
    x
}
library(purrr)

nba[,1]
unlist(flatten(map(nba[,1],splitter)))

This is perfect. Thanks a lot :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.