简体   繁体   中英

How to interactively subset columns from a data.frame in Shiny?

I want to create an interaction in shiny like database query. \\

  1. create a text box for a list of gene IDs copy-pasted;
  2. Alternatively, upload a file containing all the gene ID list;

In the mainPanel() I will get the subset of the dataframe by the gene IDs submitted. In R, this can be achieved as:

df[c("gene1", "gene2", "gene3", "gene4"),]

In shiny ui.R I have:

tabPanel("Browsing Gene(s)", tableOutput("queryTableCopy"), footer), 

And server.R I have:

output$queryTable = renderTable ({
    if(is.null(values$data$mat)) return(NULL)
    df[c("XLOC_005722", "XLOC_001942", "XLOC_001107"), ]  #hardcoded
})

which is hard coded.

Just started shiny, but not sure how this should be implemented in shiny.

I think this does what you want. Adding a global.R is useful, so you can use the column names of your dataframe in the selectizeInput in the UI.

global.R

df_genes = data.frame(gene1=rep(1,10),gene2=rep(2,10),gene3=rep(3,10))

ui.R

shinyUI(fluidPage(

  sidebarLayout(
    sidebarPanel(
      selectizeInput("genes", "Select genes:", choices = colnames(df_genes),selected=colnames(df_genes),multiple=TRUE)
    ),
    mainPanel(
              DT::dataTableOutput("queryTable")
    )
  )
))

server.R

library(shiny)

shinyServer(function(input, output, session) {

  output$queryTable = DT::renderDataTable({
    if(length(input$genes)>0)
    {
      df = df_genes %>% select(input$genes)
      print(df)
      return(DT::datatable(df))
    }
    else
    {
      return(NULL)
    }
  })

})

I hope this helps! Let me know if you have any questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM