[英]brushPoints RShiny - dsplay the selected tips from a phylogenetic tree
我想通过 RShiny 上传系统树并使用 brushPoints 函数允许用户选择系统树的提示。 最终,选择的提示将用作通过注释更新树的信息。 我的想法是显示选择的提示以确认选择,但我无法生成 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)
运行应用程序时,错误是:警告:[错误:维数不正确
在选择个人时,错误是:代表错误:无效的“时间”参数
如果需要,系统发育树位于此处:
ape
方法要获得用户刷过的提示,您需要知道图上内部节点和终端节点的 x 和 y 坐标。 您可以使用ape
包获得这些。 然后,一旦你有了刷过的区域的坐标,你就可以将 phylo 坐标表子集化为只有刷过的尖端。 在下面的示例中,我们必须明确告诉brushedPoints
在哪里寻找x
和y
坐标(数据框中的xvar
和yvar
列)。 根据树上的物种数量,您可能需要扩大绘图区域以避免重叠,以便可以轻松地刷过物种。
代码:
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)
动图:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.