inst/app/old_v/v0.6.1.R

library(shiny)

ui <- fluidPage(
  
  titlePanel("Arabidopsis EcosGEx"),
  
  sidebarLayout(
    sidebarPanel(
      textInput("agi", "Entre your AGI ID:"),
      helpText("Example: AT5G61590"),
      actionButton("find", "Find"),
      helpText("It will take some time. Be patient after you hit Find."),
      uiOutput("download")
    ),
    
    mainPanel(
      h3("Ecotype specific Gene Expression", align = "center"),
      dataTableOutput("mytable"),
      
      
      tabsetPanel(type = "tabs",
                  tabPanel("Plot", plotOutput("plot", click = "plot_click"),verbatimTextOutput("info")),
                  tabPanel("Table", dataTableOutput("table")),
                  tabPanel("Summary", verbatimTextOutput("summary"))
      )
    )
  )
  
)

###########################################################################
server <- function(input, output) {
  
  #Load packages
  library(data.table)
  library(maptools)
  library(maps)
  library(ggmap)
  library(ggplot2)
  library(plyr)
  
  observeEvent(input$find, {
    
    withProgress(message = 'Processing:', value = 0, {
      
      incProgress(1/2, detail = paste("Finding your gene")) ###########Progress step
      
      #Read data.
      expr <- read.csv("GSE80744_gene_expression.csv", sep = "\t", row.names = 1)
      cord <- read.csv("665_geo_coordinates.csv")
      
      #Taking the AGI ID and process the table.
      agi_id <- toupper(input$agi)
      gene <- t(expr[agi_id,])
      gene_df <- as.data.frame(gene)
      gene_expr <- setDT(gene_df, keep.rownames = TRUE)[]
      colnames(gene_expr)[1] <- "gene_id"
      all <- merge(x=gene_expr, y=cord, by= "gene_id", all=TRUE)
      all_combined <- na.omit(all)
      colnames(all_combined)[1] <- "Ecotype_ID"
      colnames(all_combined)[2] <- "Gene_expression"
      
      incProgress(2/2, detail = paste("Printing table"))  ###########Progress step
      
      #Printing the table to screen on table tab
      output$table = renderDataTable({
        all_combined
      })
      
      
      #Downlord the file
      output$tabledownload <- downloadHandler(
        filename = function() {
          paste(toupper(input$agi), ".csv", sep = "")
        },
        content = function(file) {
          write.csv(all_combined, file, row.names = FALSE)
        }
      )
      
      #Downlord button (for table)
      output$download <- renderUI({
        if(!is.null(input$agi)) {
          downloadButton("tabledownload", "Download Table")
        }
      })
      
      # Plot
      mapWorld <- borders("world", colour="gray50", fill="gray50") # create a layer of borders
      mp <- ggplot() +   mapWorld
      mp <- mp+ geom_point(aes(x=all_combined$longitude, y=all_combined$latitude) ,color="blue", size=3) 
      top20 <- head(arrange(all_combined, desc(all_combined$Gene_expression)) , n = 20)
      last20 <- tail(arrange(all_combined, desc(all_combined$Gene_expression)) , n = 20)
      mp <- mp+ geom_point(aes(x=top20$longitude, y=top20$latitude) ,color="red", size=3)
      mp <- mp+ geom_point(aes(x=last20$longitude, y=last20$latitude) ,color="green", size=3)
      
      output$plot <- renderPlot({
       mp
      })
      # Plot Click
      output$info <- renderText({
        paste("########### Interactive Plot Details ###########",
              "\n","\nClick Anywhere on the map to get the Corrdinates",
              "\n",
              "\nLongitude=", input$plot_click$x, "\nLatitude=", input$plot_click$y,
              "\n","\n               Note",
              "\nRed: High Expression of Top 20 gene.",
              "\nGreen: Low Expression Least 20 gene.",
              "\nBlue: Medium Expression"
        )
              
      })
      
      
      ################### After this withProgress and observeEvent ################
      
    })
  })  
}

shinyApp(ui = ui, server = server)
sk-sahu/EcoGEx documentation built on March 10, 2020, 4:52 p.m.