2
votes

I wrote a small Shiny application using renderUI. It ran correctly but the R console thrown out an error message

Error in if (nchar(locus) == 12) { : argument is of length zero

every time I ran this application.

Here are my scripts.

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
    )
  )
))

The variable "rapmsu" is a data frame.

> 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
1

1 Answers

4
votes

Make sure you include all the test cases in your function. Test for NULL and NA first and then proceed to the nchar evaluation. Here's modified example of one of your functions:

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

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

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

  else {
    return()
  }
}

As you can see I tested for NULL and NA first and then evaluated the expression. As your error says:argument is of length zero