Hi, 

I have been using the package [fgsea][1], and I would like to use a for loop in R to output multiple Enrichment plots. 

So far I can generate a plot of the desired pathway using:

    plotEnrichment(pathway = 
    pathways.hallmark[["HALLMARK_EPITHELIAL_MESENCHYMAL_TRANSITION"]], 
    gseaParam = 1, ticksSize = 0.5, stats= ranks) 
    + labs(title="Epithelial to Mesenchymal Transition") 
    + theme(plot.title = element_text(hjust = 0.5, face="bold"))

<a href="https://ibb.co/nfFHcHf"><img src="https://i.ibb.co/sQZ0w0Q/Screen-Shot-2019-07-20-at-16-19-18.png" alt="Screen-Shot-2019-07-20-at-16-19-18" border="0" /></a>


 I decided to capture the statistically relevant pathways, and attempted to use a for loop to output them all at once: 

    filtered_pathway <- subset(fgseaResTidy, pval < 0.05)

    filt_p <- as.vector(filtered_pathway$pathway)

    filt_p
     [1] "HALLMARK_APICAL_JUNCTION"                   "HALLMARK_MYOGENESIS"                       
     [3] "HALLMARK_HEDGEHOG_SIGNALING"                "HALLMARK_PI3K_AKT_MTOR_SIGNALING"
    ...

    for (i in filt_p){
        plotEnrichment(pathway = pathways.hallmark[[i]], 
        gseaParam = 1, ticksSize = 0.5, stats= ranks) + 
        labs(title=i) + theme(plot.title = element_text(hjust = 0.5, face="bold"))
    }

This code does not generate anything. I am aware this is not a bioinformatics question, however I have no doubt many would be able to point out the problem in 2 seconds. Hopefully this post will be helpful to people using fgsea in the future. 

Kind regards, 

Barry




  [1]: http://bioconductor.org/packages/release/bioc/html/fgsea.html

The loop you are running will generate plot but will not save anything . 
I have modified the code, I hope it helps.

    for (i in filt_p){
        pdf(paste0(i,".pdf"),height=5,width=5) # change height and width parameter
        plt <- plotEnrichment(pathway = pathways.hallmark[[i]], 
        gseaParam = 1, ticksSize = 0.5, stats= ranks) + 
        labs(title=i) + theme(plot.title = element_text(hjust = 0.5, face="bold"))
        print(plt)
        dev.off()
    }