2014-10-29 100 views
0

我用renderUI寫了一個小的Shiny應用程序。它正確地運行但R控制檯拋出的錯誤消息閃亮的renderUI錯誤

錯誤如果(NCHAR(軌跡)== 12){:參數是每我跑此施加時間長度零

的。

這是我的腳本。

server.R:

load("rapmsu.rda") 
convMSU <- function(locus="Os02g0677300") { 
    if (nchar(locus)==12) { 
    return(rapmsu[rapmsu$rap==locus,]) 
    } else { 
    return(NULL) 
    } 
} 
convRap <- function(locus="LOC_Os03g57940") { 
    if (nchar(locus)==14) { 
    return(rapmsu[rapmsu$msu==locus,]) 
    } else { 
    return(NULL) 
    } 
} 
convID <- function(query="", text="") { 
    if (query=="RAPdb Locus") { 
    return(convMSU(text)) 
    } else if (query=="MSU Locus") { 
    return(convRap(text)) 
    } 
} 

query.intext.conv <- c("Os02g0677300", "LOC_Os03g57940") 
names(query.intext.conv) <- c("RAPdb Locus", "MSU Locus") 

#### Shiny 
shinyServer(function(input, output) { 

    output$inTextconv <- renderUI({ 
    textInput("inTextconv", strong("Put your query here:"), 
      value=query.intext.conv[input$queryconv]) 
    }) 

    output$mytable10 = renderDataTable({ 
    convID(input$queryconv, input$inTextconv) 
    }, options = list(aLengthMenu = 1, iDisplayLength = 1, 
        bFilter = FALSE, bAutoWidth = FALSE) 
) 
}) 

ui.R:

shinyUI(fluidPage( 
    fluidRow(
    absolutePanel(
     br(), 

     selectInput("queryconv", h4("* Convert ID of MSU genomic locus 
            and RAPdb genomic locus"), 
       choices=c("RAPdb Locus", "MSU Locus")), 

     uiOutput("inTextconv"), 

     tabsetPanel(
      tabPanel(strong('Result'), dataTableOutput("mytable10")) 
    ), 

     br(), 

     right=5, left=10 
    ) 
) 
)) 

變量 「rapmsu」 是一個數據幀。

> head(rapmsu) 
      rap   msu 
1 Os01g0100100 LOC_Os01g01010 
2 Os01g0100200 LOC_Os01g01019 
3 Os01g0100300   None 
4 Os01g0100400 LOC_Os01g01030 
5 Os01g0100466   None 
6 Os01g0100500 LOC_Os01g01040 

回答

4

確保包含所有測試用例。首先測試NULLNA,然後進行nchar評估。下面是你的一個函數的修改示例:

convMSU <- function(locus="Os02g0677300") { 

    if(is.null(locus) || is.na(locus)) 
    { 
    return() 
    } 

    else if (nchar(locus)==12) { 
    return(rapmsu[rapmsu$rap==locus,]) 
    } 

    else { 
    return() 
    } 
} 

正如你所看到的,我首先測試了NULL和NA,然後評估了表達式。正如你的錯誤所說:argument is of length zero

+0

就是這樣!謝謝!事實上,我的腳本中有另一個UI界面(這裏沒有顯示),它運行時沒有任何錯誤。此UI界面完全從該界面複製。無論如何,問題解決了。 – 2014-10-29 10:19:57