「Shiny 入门」编写一个解析GFF文件的Shiny App

2022-09-20  本文已影响0人  陈有朴

很早之前写的。

首先定义一个非常简单的用于解析GFF文件的脚本,保存为parse.R

# This script is used to parse the GFF file
parseGFF <- function(filename){
    if(file.exists(filename) == FALSE){
        cat("No GFF in your dir")
    }
    gff <- read.table(filename, comment.char = "#", header = FALSE, encoding = 'UTF-8')
    return(gff)
}

最终定义ui.Rserver.R即可,

# ui.R
library(shiny)
source('parse.R')
options(shiny.maxRequestSize=200*1024^2)

ui <- fluidPage(
    fluidRow(
        column(6, fileInput("gff", "Upload your GFF file"))
    ),

    fluidRow(
        column(4, tableOutput("Frequency Table of Input Features"))
    ),

    fluidRow(
        column(12, plotOutput('Freq'))
    )
)

# server.R
library(shiny)
library(ggplot2)
library(tidyverse)

# This function is used to parse the GFF file
parseGFF <- function(filename){
    if(file.exists(filename) == FALSE){
        cat("No GFF in your dir")
    }
    gff <- read.table(filename, comment.char = "#", header = FALSE)
    return(gff)
}


server <- function(input, output, session) {
    data <- reactive({
        req(input$gff)
        inFiles <- input$gff
        df <- parseGFF(inFiles$datapath)
        return(df)
    })

    type.df <- reactive({
        df <- as.data.frame(table(data()[, 3]))
        names(df) <- c("Type", "Freq")
        return(df)
    })

    output$`Frequency Table of Input Features` <- renderTable({
        type.df()
    })

    output$Freq <- renderPlot({
        # type.df <- as.data.frame(table(data()[, 3]))
        # names(type.df) <- c("Type", "Freq")
        ggplot(data = type.df(), aes(x = Type, y = Freq, fill = Type)) + geom_col() + 
        geom_text(aes(label = Freq), vjust=1.6, color="white", size=3.5) + 
        theme_classic() + 
        theme(axis.text.x = element_text(angle = 45, hjust=1))
    })
}

欢迎访问:https://chenyoupu.shinyapps.io/showgff/

上一篇下一篇

猜你喜欢

热点阅读