This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to run both fileInput and textAreaInput in shinyapp?

I would like to put both fileInput and textAreaInput in my app but I do not know how to define them in the server. For example, when choosing fileInput consider the uploaded file, and when choosing textAreaInput consider the entered regions (sep=" |\t").

The below code is working well when I upload a file in bed format but it cannot recognize when I put coordinate as text in the textAreaInput.

Any help would be appreciated!

ui <- fluidPage( tabPanel("Data", icon = icon("database"),
                      tabsetPanel(type = "pills",
                                  tabPanel("Data Table",icon = icon("table"),
                                           sidebarLayout(
                                             sidebarPanel (
                                               selectInput("choose","Choose file source",choices = c("file","text"),selected = NULL),
                                           conditionalPanel("input.choose=='file'",
                                                            fileInput("data_file", "Upload coordinates in .bed/.csv format:",multiple = TRUE,accept = c(".bed",".csv"))),
                                           conditionalPanel("input.choose=='text'",
                                                            textAreaInput("data_paste", "Enter coordinates:",placeholder =  "Paste like chr1 100 200 per line")),
                                           actionButton("run", label="Run overlap"),
                                           actionButton("add.table", "See overlap results"),
                                           width = "3"),
                                           mainPanel(dataTableOutput("overlap.table"))
                                           )
                                           )
                                  )
                      )
             )


server <- function (input, output, session) { 
  ## file or text condition
 data <- reactiveVal()

 observeEvent(
eventExpr = input$run,
handlerExpr = {
  switch(input$choose,
         file = fread(input$data_file$datapath, header=F) %>%
           dplyr::rename (chr =V1, start=V2, end=V3) %>% setkey(chr, start, end),
         text = data.table(input$data_paste)%>%
           dplyr::rename (chr =V1, start=V2, end=V3) %>% setkey(chr, start, end)
  ) %>% data()
}
  )

 DF<-reactive({
df<-data.table(chr=c("chr3","chr1","chr3","chr2"),start=c(10,180,50,20),end=c(12,250,55,22))%>% setDT() %>% setkey(chr, start, end)
  })
  ## Run Analyze
  analyzed <- eventReactive(input$run, {
    req(input$run)
    query_overlap<- foverlaps(data() ,DF(),  nomatch = 0)
    })

  output$overlap.table <- renderDT(analyzed()) 

  }

shinyApp(ui, server)

And getting the below error:

Listening on http://127.0.0.1:7624
Warning: Error in : Can't rename columns that don't exist.
x Column `V2` doesn't exist.

Warning: Error in foverlaps: y and x must both be data.tables. Use 
`setDT()` to convert list/data.frames to data.tables by reference or 
as.data.table() to convert to data.tables by copying.
r shiny_app shinyapp shiny shiny-server

0 answers

No answers yet.

Log in to answer this question.