简体   繁体   English

如何交互地从Shiny的data.frame子集中列?

[英]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; 为复制粘贴的基因ID列表创建一个文本框;
  2. Alternatively, upload a file containing all the gene ID list; 或者,上传一个包含所有基因ID列表的文件;

In the mainPanel() I will get the subset of the dataframe by the gene IDs submitted. mainPanel()我将通过提交的基因ID获得数据帧的子集。 In R, this can be achieved as: 在R中,可以这样实现:

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

In shiny ui.R I have: 在闪亮的ui.R中,我有:

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

And server.R I have: server.R我有:

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很有用,因此您可以在UI的selectizeInput中使用数据框的列名。

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. 如果您有任何疑问,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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