Rshiny - How do I check if user input exists in my data set?
1
1
Entering edit mode
7.8 years ago
BioinfGuru ★ 1.7k

Hi all,

I have created a shiny app to display RNA seq analysis data. The user must enter a gene name in a specific format (e.g. Adh4, not ADH4). If they don't use the correct format, my server crashes. :(

The simplest fix conceptually is to check if the user input exists in my set of gene names --> then if it doesn't i just returns a message telling them to re input in the correct format or choose a different gene.

First: I ruled out using selectInput - it only allows 1000 entries to choose from (I have 20000 genes)

Second: Ive looked into validate/need but Im finding it confusing so Im not really sure how to use it.

MY CODE:

1) The user is asked for input in the form of a gene name via the UI with the following command:

textInput(inputId = "gene_id", label = "Enter gene name (capitalise first letter only) or XLOC number", value = "", placeholder = "Adh4 or XLOC_009602"),

2) The input is then used to create an object called "my_gene" containing all info about the gene name entered using the following server reactive command: (I think this is where validate/need goes)

my_gene <- reactive({getGenes(cuff,geneIdList = input$gene_id)})

3) The object is then parsed to retrieve the gene name only using the following server render command

output$gsn <- renderText({if (input$gene_id != "") {paste("Gene short name: ", as.character(featureNames(my_gene())$gene_short_name))}})

4) The gene name is passed to the user as output with the following UI command

textOutput("gsn")

Thanks in advance, Kenneth

Rshiny textInput validate need Shinyapp • 7.5k views
ADD COMMENT
1
Entering edit mode
7.8 years ago

Have you looked at validate() ?

ADD COMMENT
0
Entering edit mode

Yes....but this is very confusing...I really dont know how to form the command. I know that it is inserted into:

my_gene <- reactive(

validata()

{getGenes(cuff,geneIdList = input$gene_id)}
)

but I dont know how.

ADD REPLY
0
Entering edit mode

You could do something like

validate(
    need( input$gene_id %in% gene_list, 'Gene name not found. Make sure only the first letter is capitalized!')
 )

You should maybe also consider making your code more tolerant of gene symbol capitalization. What I usually do from the start to avoid such issue is uppercase or lowercase every string that's used as some sort of ID or key.

ADD REPLY
0
Entering edit mode

I'm trying your code....but it appears to not close the validate bracket .....2 open, 1 close.

Here is what I tried: I've literally copied and pasted your code inside mine.

my_gene <- reactive( validate( need( input$gene_id %in% gene_list, 'Gene name not found. Make sure only the first letter is capitalized!') ) {getGenes(cuff,geneIdList = input$gene_id,sampleIdList = input$sns)})

I get 2 errors:

1) unmatched opening bracket @ reactive(

2) unexpected token '{' unexpected closing bracket '}' unexpected closing bracket ')'

Adding a comma ',' after your code @ "capitalized!'))," removes the errors but the app then won't run.

I know this is probably a syntax thing.

Kenneth.

PS: As regards making all IDs lowercase (which would be great), Id rather not mess with the output of cuffdiff....just to make it fit into my shiny app. Id rather fix the app to handle the output.

ADD REPLY
0
Entering edit mode

The syntax for reactive() should be

reactive( {
   validate( need(...)  )
   getGenes(cuff,geneIdList = input$gene_id)
})
ADD REPLY
0
Entering edit mode

So I have taken your advice and added your code but found the error: cannot finde "gene_list" object. So I attempted to create a gene list as soon as I create the CuffSet (however this slows down the start up of the app dramatically:

cuff <- readCufflinks(genome = 'rn6', gtfFile = "merged.gtf")
myGeneIds<-featureNames(genes(cuff))  # gets all XLOC numbers from cuff
myGenes<-getGenes(cuff,myGeneIds)     # gets all info about each XLOC number
gene_list<-featureNames(myGenes)      # gets all gene_short_names for each XLOC number  
my_gene <- reactive({
                    validate(
                              need(
                                   input$gene_id %in% gene_list, 
                                   'Gene name not found. Capitalize first letter only'
                                   )
                              )
                    getGenes(cuff,geneIdList = input$gene_id,sampleIdList = input$sns)
                    })

Now when I enter a gene name correctly (e.g. Adh4) the code gives me the error message: Gene name not found. Capitalize first letter only.

Close ... but no trophy however t least it appears the server isn't breaking.

Suggestions?

Thanks again for your input.

Kenneth

ADD REPLY
0
Entering edit mode

Unless there's something I am missing, this would mean that 'Adh4' is not present in gene_list as entered. Check that both the input and gene_list contain what you expect. For example, there could be extra whitespace characters before or after some gene names.

ADD REPLY
0
Entering edit mode

Thank you...I will try that.

I also tried your suggestion of making all letters lowercase with the script below but this returns an error in the app (so the app doesnt load) and the database created is much smaller than it should be. I think there is some dependency on uppercase somewhere.

opendir(DIR, ".");
my @files = readdir(DIR);
my @sorted = sort (@files);
shift @sorted;              # removes "." from the array
shift @sorted;              # removes ".." from the array
pop @sorted;                # removes this script from the array                    
close DIR;                                      
print "@sorted\n";
foreach my $file(@sorted)
    {
     open(FH,"$file");                              
     open(OUT, ">$file.lc");                                
     while(<FH>)                                    
          { 
          $_ =~ tr/[A-Z]/[a-z]/;                            
              print OUT $_;                             
          rename "$file.lc", "$file";                   
          }
      close(FH);                                    
    }
ADD REPLY

Login before adding your answer.

Traffic: 2918 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6