You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:基于R Shiny与ape包实现进化树可视化的Server函数优化

修复后的Shiny进化树可视化应用代码

以下是调整后的完整代码,实现了文件类型检测、进化树读取与可视化,同时加入了错误处理机制:

library(shiny)
library(ape)

ui <- navbarPage("进化树可视化工具", 
                 tabPanel("系统发育树",
                          fluidRow(
                            class = "inputs",
                            column(6, align = "center",
                                   h4("选择文件类型:"),
                                   selectInput(inputId = "file_type", "", 
                                               choices = list("Newick" = "newick", 
                                                              "NEXUS" = "nexus",
                                                              "Phylip" = "phylip",
                                                              "RAxML" = "raxml"),
                                               selected = "newick")
                            ),
                            
                            column(6, align = "center",
                                   h4("选择树文件:"),
                                   fileInput(inputId = "tree_file", "",
                                             buttonLabel = "浏览")
                            )
                          ),
                          # 显示错误提示
                          verbatimTextOutput("error_msg"),
                          # 进化树可视化区域
                          plotOutput('tree_plot')
                 )
)

server <- function(input, output, session) {
  # 响应式读取进化树文件
  tree_data <- reactive({
    req(input$tree_file) # 等待用户完成文件上传
    file_path <- input$tree_file$datapath
    
    # 根据选择的文件类型匹配对应读取函数
    read_func <- switch(input$file_type,
                        newick = read.tree,
                        nexus = read.nexus,
                        phylip = read.phylip,
                        raxml = read.tree) # RAxML输出属于Newick格式
    
    # 捕获读取过程中的错误
    tryCatch({
      tree <- read_func(file_path)
      list(tree = tree, error = NULL)
    }, error = function(e) {
      list(tree = NULL, error = paste("文件读取失败:", e$message))
    })
  })
  
  # 渲染进化树图
  output$tree_plot <- renderPlot({
    data <- tree_data()
    if (!is.null(data$tree)) {
      # 绘制进化树,可根据需求调整样式
      plot(data$tree, 
           type = "phylogram", 
           edge.color = "#2c3e50", 
           tip.color = "#e74c3c",
           cex = 0.8)
      title(paste("上传的", input$file_type, "格式进化树"))
    }
  })
  
  # 渲染错误信息
  output$error_msg <- renderPrint({
    data <- tree_data()
    if (!is.null(data$error)) {
      cat(data$error)
    }
  })
}

shinyApp(ui = ui, server = server)

关键修改说明:

  • 文件类型匹配逻辑:通过switch函数根据用户选择的文件类型,调用ape包对应的读取函数,确保不同格式文件的兼容性(RAxML输出本质是Newick格式,复用read.tree处理)。
  • 响应式与校验:用reactive()封装文件读取逻辑,req(input$tree_file)确保只有用户上传文件后才执行读取操作,避免空值报错。
  • 错误捕获机制:使用tryCatch捕获文件读取时的异常(如格式不匹配、文件损坏),并将错误信息展示给用户,防止应用崩溃。
  • 可视化优化:为进化树添加了基础样式调整(分支颜色、标签颜色、字体大小),提升展示效果。
  • UI布局优化:新增错误信息输出区域,方便用户快速排查问题。

内容的提问来源于stack exchange,提问作者CornOnTheCob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 20:15:33