1
votes


I am trying to write a shiny app that searches for patterns in a column of an excel file called "SYMBOL" and outputs matching rows. The problem is that the code keeps running without outputting anything. No errors. The data frame is also being created. What did I do wrong? Please find the code below and the excel file can be found in this link https://drive.google.com/file/d/1VX9RXIyRaGr9Ox-h-ZNGkbYsAasPeKBp/view?usp=sharing.
Thanks in advance.

  library(shiny)
  library(xlsx)
  library(DT)
  library(tidyverse)

  # Escape special chars for regex matching
  escapeSpecialChars <- function(p) {
    sChars <- "/(){}[]\\.*^$|+?"
    q <- unlist(strsplit(p,""))
    for(i in 1:length(q)){
      if (length(q[i] %>% contains(sChars, ignore.case = TRUE))){
        q[i] <- paste0("[", q[i], "]")
      }
    }
    q <- paste0(q, collapse = "")
    return(q)
  } 


  # Read GO file
  GO_Symbol <-
    read.xlsx2(
      'GO_Symbol.xlsx',
      1,
      check.names = FALSE
    )


  # UI logic
  ui <- fluidPage(
    titlePanel(""),

    sidebarLayout(
      sidebarPanel(
        textAreaInput(
          "Symbol",
          "Paste your Genes below",
          "",
          width = "200px",
          height = "400px"
        ),
        hr(),
        actionButton("gobutton", "Submit")
      ),

      mainPanel(
        DT::dataTableOutput("dataFrameOutput")
      )
    )
  )

  # Define server logic
  server <- function(input, output) {
    data <- eventReactive(
      input$gobutton,
      {
        if (is.null(input$Symbol)) {
          return()
        }

        df <- data.frame(matrix(ncol = ncol(GO_Symbol), nrow = 0))
        colnames(df) <- colnames(GO_Symbol)
        ts <- unlist(strsplit(input$Symbol, "\n"))
        for (i in 1:length(ts)) {
          p <- escapeSpecialChars(ts[i])
          idx <-
            grep(p, GO_Symbol$SYMBOL, ignore.case = TRUE)
          if (length(idx)) {
            df <- rbind(df, GO_Symbol[idx, ])
          }
          else {
            df <- df %>% add_row(SYMBOL = ts[i])
          }
        }
      })

    output$dataFrameOutput <- DT::renderDataTable({
      data()
    })
  }

  # Run the app
  shinyApp(ui = ui, server = server)
1

1 Answers

0
votes

Ok, looks like you need to add

return(df)

in eventReactive brackets (next after for loop)