【问题标题】:How to interactively subset columns from a data.frame in Shiny?如何在 Shiny 中以交互方式从 data.frame 中对列进行子集化?
【发布时间】:2017-12-23 13:01:25
【问题描述】:

我想在闪亮的数据库查询中创建交互。 \

  1. 为复制粘贴的基因 ID 列表创建一个文本框;
  2. 或者,上传一个包含所有基因ID列表的文件;

mainPanel() 中,我将通过提交的基因 ID 获取数据框的子集。在 R 中,这可以实现为:

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

在闪亮的 ui.R 我有:

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

还有 server.R 我有:

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

这是硬编码的。

刚开始闪亮,但不确定应该如何在闪亮中实现。

【问题讨论】:

  • 您好,感谢您编辑我的帖子。编辑后的标题不完全是我的问题。我的意思是“数据框的子集行......”。

标签: r dataframe shiny subset


【解决方案1】:

我认为这可以满足您的需求。添加 global.R 很有用,因此您可以在 UI 的 selectizeInput 中使用数据框的列名。

全球.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")
    )
  )
))

服务器.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)
    }
  })

})

我希望这会有所帮助!如果您有任何问题,请告诉我。

【讨论】:

  • 感谢弗洛里安!我的意思是添加一个函数,以便我可以将geneIDs 粘贴到textAreaInput 中,或者上传包含geneIDs 的文件。到目前为止,在 UI.RI 中有: conditionalPanel(condition = "input.tabs1 == 'Browsing Gene(s)'", h4("Export options"), textAreaInput("geneIDs", "Gene IDs of interest:",) ), in server.R: output$queryTable = renderTable({ mydf
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-19
  • 1970-01-01
  • 2014-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多