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

R Shiny基因组工具故障排查:两功能无输出及cat函数报错

Fixing AAC and AMIP Function Errors in Your Shiny Genomic Tool

Let's break down and fix the issues with your AAC and AMIP functions step by step—most of the problems come from small syntax/typo errors and mismatched output handling:

1. Fix Case Sensitivity & Trigger Event Mismatches

Shiny input IDs are case-sensitive, and your code has critical mismatches here:

  • You used input$signatures (lowercase 's') for AMIP checks, but your UI's selectInput is named Signatures (uppercase 'S'). This means Shiny can't match the selection for AMIP at all.
  • Your df1 reactive uses input$actions as the trigger, but your submit button is input$action (missing the 's'). So the AMIP function never actually runs when you click submit.

2. Clean Up Switch Statement Syntax

In your original df reactive, the switch statement has an extra trailing comma after extractAAC_mod—this can cause unexpected behavior. Remove that comma to avoid parsing issues.

3. Handle List Output from extractAAC_mod

The error argument 1 (type 'list') cannot be handled by 'cat' happens because extractAAC_mod returns a list, but renderPrint tries to use cat() which doesn't work with lists. Instead, use print() to properly display the list, or convert it to a data frame for cleaner, more readable output.

4. Unify AMIP Trigger & Logic

Merge the AMIP logic into the main reactive flow (or fix its reactive trigger) so it runs when the submit button is clicked, and use the correct input ID for the selection.

Modified Working Code

Here's the corrected version with all fixes applied:

library("shinythemes")
library("shiny")
library("htmltools")
library("bsplus")
library("DT")
library("shinyalert")
library("shinyjs")
library("shinycssloaders")
library("dplyr")
library("data.table")
library("reshape2")
library("ggplot2")
library("plotly")
library("tools")
library("readxl")
library("writexl")
library("seqinr")
library("Biostrings")
library("BiocManager")
library("entropy")
library("protr")
source("GS_Source.R")

ui = fluidPage(
 headerPanel(
 h1(tags$strong("Welcome to Web based Tool for Computation of Genomic"), style = "font-size: 20pt; line-height: 30pt; width = 100; color: #Red", align = "center"),
 windowTitle = "Home"
 ),
 sidebarLayout(
 sidebarPanel(
 style = "background-color: #E52B50;",
 tags$style(type='text/css', "label { font-size: 12px; }", ".selectize-input { font-size: 12pt; line-height: 12pt;}, .selectize-dropdown { font-size: 12pt; line-height: 12pt; }"),
 fileInput('file', HTML('<p style="color:#4A2768; font-size: 12pt"> Choose file to upload the expression data </p>'),accept = c('text/csv','text/comma-separated-values', 'text/tab-separated-values','text/plain','.csv', '.tsv','.fasta')),
 selectInput("Signatures", HTML('<p style="color:#4A27C5; font-size: 12pt"> Genome signature </p>'), c("GC-content","RSCU","CGR","DI-nuculeotide odd Ratio","AAC","AMIP")),
 numericInput("num1",label = "FROM",value = 2),
 numericInput("num2", label = "TO", value = 3),
 useShinyalert(),
 actionButton("action", tags$b("Submit")),
 width = 3, style="color: #fff; background-color: #337ab7; border-color: #2e6da4"
 ),
 mainPanel(
 verbatimTextOutput("res1"),
 verbatimTextOutput("res2"),
 plotOutput("res3"),
 verbatimTextOutput("res4"),
 verbatimTextOutput("res5"),
 verbatimTextOutput("res6"),
 )
 )
)

server <- function(input, output, session) {
 # Combine all function logic into one reactive for consistency
 df <- eventReactive(input$action, {
   req(input$file) # Ensure file is uploaded before running
   switch(input$Signatures, 
          "GC-content" = GC_content7(input$file$datapath),
          "RSCU" = RSCU(input$file$datapath),
          "CGR" = cgr_res(input$file$datapath),
          "DI-nuculeotide odd Ratio" = zscore_cal(input$file$datapath),
          "AAC" = extractAAC_mod(input$file$datapath),
          "AMIP" = AMIP(input$file$datapath, input$num1, input$num2)
   )
 })

 # Output handlers with fixed case matching and proper printing
 output$res1 <- renderPrint({ 
   req(input$Signatures == "GC-content")
   print(df()) # Use print() instead of relying on default cat
 })
 output$res2 <- renderPrint({ 
   req(input$Signatures == "RSCU")
   print(df())
 })
 output$res3 <- renderPlot({ 
   req(input$Signatures == "CGR")
   df()
 })
 output$res4 <- renderPrint({ 
   req(input$Signatures == "DI-nuculeotide odd Ratio")
   print(df())
 })
 output$res5 <- renderPrint({ 
   req(input$Signatures == "AAC")
   print(df()) # Properly prints the list from extractAAC_mod
 })
 output$res6 <- renderPrint({ 
   req(input$Signatures == "AMIP")
   print(df())
 })

 observeEvent(input$action, { 
   shinyalert(title = "Please wait for the results...", type = "success") 
 })
}

shinyApp(ui, server)

Key Changes Explained:

  • Unified Reactive: Combined all function calls into a single df reactive triggered by input$action, which simplifies logic and avoids mismatched triggers.
  • Case Consistency: Used input$Signatures (uppercase 'S') everywhere for selection checks to match the UI input ID.
  • List Handling: Replaced implicit output with print(df()) in renderPrint to properly display list outputs from extractAAC_mod.
  • Fixed Switch Syntax: Removed the trailing comma and organized the switch cases clearly.
  • Added req() Guards: Ensured outputs only render when the correct selection is made and a file is uploaded, preventing unnecessary errors.

If extractAAC_mod returns a structured list (like a data frame inside a list), you could also use DT::renderDataTable to display it as a table instead of verbatim text—just replace verbatimTextOutput("res5") with DTOutput("res5") and the server output with:

output$res5 <- renderDT({
  req(input$Signatures == "AAC")
  as.data.frame(df())
})

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 13:07:30