brushPoints R Shiny - 显示从系统发育树中选择的提示

brushPoints RShiny - dsplay the selected tips from a phylogenetic tree

我想通过 RShiny 上传系统发育树并使用 brushPoints 功能允许用户 select 系统发育树的提示。最终,提示 selected 将用作通过注释更新树的信息。我的想法是显示 selected 的提示以确认 selection,但我无法生成 verbatiumTextOutput。 建议

以下是我的尝试:

library(shiny)
library(ggplot2)
library(treeio)
library(ggtree)
library(tidytree) 

tree <- treeio::read.newick("1509MLJF6-1.dnd")

# Define UI for application that draws a histogram
ui <- fluidPage(

  # Application title
  titlePanel("Select Individuals and Display Data"),

  # Show a plot and output table 
  mainPanel(
    plotOutput("treeDisplay", brush = "plot_brush"),
    verbatimTextOutput("selectedIndivs")

  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
  output$treeDisplay <- renderPlot({
    ggplot(tree) + geom_tree() + geom_tiplab()
  })

  output$selectedIndivs <- renderPrint({
    brushedPoints(tree, input$plot_brush)
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

在运行 App 上,错误是: 警告:[错误:维数不正确

并且 selecting 个人的错误是: 代表错误:无效 'times' 参数

如果需要,系统发育树位于here

ape接近

要获得用户刷过的提示,您需要知道图上内部节点和终端节点的 x 和 y 坐标。您可以使用 ape 包获取这些。然后,一旦你有了刷过的区域的坐标,你就可以将 phylo 坐标 table 子集化为只有刷过的尖端。在下面的示例中,我们必须明确告诉 brushedPoints 在何处查找 xy 坐标(数据框中的 xvaryvar 列)。根据树上的物种数量,您可能需要扩大绘图区域以避免重叠,以便物种可以轻松刷过。


代码:

library(shiny)
library(ape)

Tree <- rtree(n=20)
Tree <- ladderize(Tree)

ui <- basicPage(
  plotOutput("plot1", brush = "plot_brush"),
  tableOutput("brushed_subtree")
)

server <- function(input, output) {

  output$plot1 <- renderPlot({
    plot(Tree)
  })

  getTreeInfo <- reactive({
    plot(Tree)
    plotinfo <- get("last_plot.phylo", envir = .PlotPhyloEnv)
    tips_xy <- data.frame(Tip=Tree$tip.label, 
                          xvar=plotinfo$xx[1:Ntip(Tree)], 
                          yvar=plotinfo$yy[1:Ntip(Tree)])
    return(tips_xy)
  })

  # render selected tips table 
  output$brushed_subtree <- renderTable({
    brushedPoints(getTreeInfo(), input$plot_brush, xvar = "xvar", yvar = "yvar")
  })

}

shinyApp(ui, server)

动图:


ggtree接近

ggtree 方法更简单。事实上,您已经很接近了,除了您需要提供 ggtree 图的 $data 组件而不是将您的树传递给 brushedPoints 。请注意,在这种情况下,刷过的区域必须包括所需提示的终端分支,因为绘图的坐标由边缘数据框给出,并且提示是相对于先前绘制的(终端)边缘绘制的单独几何图形.


代码

library(shiny)
library(ggplot2)
library(treeio)
library(ggtree)
library(tidytree) 

tree <- treeio::read.newick("1509MLJF6-1.dnd")
treedt <- as.treedata(tree)

# Define UI for application that draws a histogram
ui <- fluidPage(

  # Application title
  titlePanel("Select Individuals and Display Data"),

  # Show a plot and output table 
  mainPanel(
    plotOutput("treeDisplay", brush = "plot_brush"),
    verbatimTextOutput("selectedIndivs")

  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

  make_tree <- reactive({
     ggplot(tree) + geom_tree() + geom_tiplab()
  })

  output$treeDisplay <- renderPlot({
    make_tree()
  })

  output$selectedIndivs <- renderPrint({
    brushedPoints(make_tree()$data, input$plot_brush)
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

动图: