How to seperate comma-separated string column as seletced input columns choice in R shiny?

Hi,
I am using the R shiny to show my dataset based on the case (Shiny - Basic DataTable) to show the outputs based on the user's query column inputs.
However, one column('Feature') have multiple strings separated by comma(,). In this case, is it possible to sort the unique strings in the column and set all the options as input for sorting?

Thank you so much for your help!

My example input table(For a specific row it may have multiple features in the Feature column)

Chrom Start End Dataset Feature
chr1 136895 136895 AML promoter_2000,promoter_1000,cpg_shore
chr1 181286 181286 AML promoter_2000

The snapshot for the output for my code below. You can see the options are still comma-separated like promoter_2000,promoter_1000,cpg_shore, but I want the options be promoter_2000, promoter_1000 , cpg_shore and show corresponding results.

Screen Shot 2020-11-08 at 4.23.37 PM

My code ui.R :

library(ggplot2)
library(data.table)

fluidPage(titlePanel("Test"),
          title = "Test",
          fluidRow(
            column(4,
                   selectInput("Feature",
                               "Feature:",
                               c("All",
                                 unique(as.character(data$Feature))))
            )
          ),
          # Create a new row for the table.
          DT::dataTableOutput("table")
)

server.R

library(ggplot2)
library(DT)

function(input, output) {
  output$table <- DT::renderDataTable(DT::datatable({
    df <- data
    columns = names(df)
    if (input$Feature != "All") {
      df <- df[df$Feature == input$Feature]
    }
    df
  },
}

Here is a method to get the unique values in the Feature column if you know the maximum number of Features in any case.

library(tidyr)
library(dplyr)

DF <- data.frame(Chrom = c("chr1", "chr1"), Start = c(136895, 181286),
                 End = c(136895, 181286), Dataset = "AML",
                 Feature = c("promoter_2000,promoter_1000,cpg_shore", "promoter_2000"))

DF <- DF %>% separate(col = Feature, into = c("F1", "F2", "F3"), fill = "right", sep = ",")
Features <- DF %>% select(F1:F3) %>% 
  pivot_longer(cols = everything()) %>% 
  filter(!is.na(value)) %>% 
  select(value) %>% 
  unique()
Features
#> # A tibble: 3 x 1
#>   value        
#>   <chr>        
#> 1 promoter_2000
#> 2 promoter_1000
#> 3 cpg_shore

Created on 2020-11-08 by the reprex package (v0.3.0)

If you want to look at data from the original data frame based on a selected value of Feature, I think you can use this reshaped version of the data.

DF2 <- DF %>% separate(col = Feature, into = c("F1", "F2", "F3"), fill = "right", sep = ",") %>% 
  pivot_longer(cols = F1:F3)

Hi FJCC,
Thank you so much for your advice!

I gave a try on your example but found that the same content won't be separated into the same column(In the example case it is promoter2000, one in row1, column F1, another one in row2, column F2)....

> DF <- data.frame(Chrom = c("chr1", "chr1"), Start = c(136895, 181286),
+                  End = c(136895, 181286), Dataset = "AML",
+                  Feature = c("promoter_2000,promoter_1000,cpg_shore", "exon,promoter_2000"))
> DF
  Chrom  Start    End Dataset                               Feature
1  chr1 136895 136895     AML promoter_2000,promoter_1000,cpg_shore
2  chr1 181286 181286     AML               cpg_shore,promoter_2000
> DF <- DF %>% separate(col = Feature, into = c("F1", "F2", "F3"), fill = "right", sep = ",")
> DF
  Chrom  Start    End Dataset            F1            F2        F3
1  chr1 136895 136895     AML promoter_2000 promoter_1000 cpg_shore
2  chr1 181286 181286     AML exon          promoter_2000      <NA>

I found another clumsy way to do that. I am using concat.split.multiple function in splitstackshape package to split concatenated cells and reshape the outputs. In this case, it reshapes the feature column and creates new rows for each string separated by a comma in the feature column.

> DF <- data.frame(Chrom = c("chr1", "chr1"), Start = c(136895, 181286),
+                  End = c(136895, 181286), Dataset = "AML",
+                  Feature = c("promoter_2000,promoter_1000,cpg_shore", "exon,promoter_2000"))
> DF
  Chrom  Start    End Dataset                               Feature
1  chr1 136895 136895     AML promoter_2000,promoter_1000,cpg_shore
2  chr1 181286 181286     AML                    exon,promoter_2000
> DF <- concat.split.multiple(DF, split.col="Feature", sep=",",direction="long")
> DF
   Chrom  Start    End Dataset       Feature
1:  chr1 136895 136895     AML promoter_2000
2:  chr1 136895 136895     AML promoter_1000
3:  chr1 136895 136895     AML     cpg_shore
4:  chr1 181286 181286     AML          exon
5:  chr1 181286 181286     AML promoter_2000

This topic was automatically closed 54 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.