简体   繁体   中英

Encountered R shiny error: non-numeric matrix extent

I wrote a Rshiny app. Here, the user inputs a list of genes and the app creates a biological network out of it. The example input is shown below. The code works out of shinyapp as a R script. The input is text and not numeric.

Researched similar questions on SO here but not much useful, my input is text.

Any help is greatly appreciated.

library(shiny)
library(shinydashboard)
library(STRINGdb)

sidebar <- dashboardSidebar(
sidebarMenu(
menuItem("Analysis", tabName = "analysis")
))

body <- dashboardBody(
tabItems(
# Text Area Input for gene list
tabItem(tabName = "analysis", 
        fluidRow(
          box(
            title = "Input gene list here", status = "warning", width 
            = 6, textAreaInput("title1","",value = "", width = 
            '100%',rows=5),
            actionButton("submit", "Submit")
          )
        ),
        fluidRow(
          box(
            title = "Stirng DB network", status = "warning", width = 8, plotOutput("stringplot")
          )
        )
)))

# User interface --
ui <- dashboardPage(
dashboardHeader(title = "Analysis", titleWidth = 300),
sidebar,
body
)

# Server logic
server <- function(input, output) {
subset_dataset <- eventReactive(input$submit, 
{data.frame(strsplit(input$title1,split='[\r\n]'),col.names='genes')})
output$stringplot <- renderPlot({
string_db <- STRINGdb$new()
mapped_genes <- string_db$map(subset_dataset,subset_dataset()$genes,removeUnmappedRows = TRUE)
hits_2<-mapped_genes$STRING_id
string_db$plot_network(hits_2)})
}

shinyApp(ui = ui, server = server)

The example input is:

BRAF
NF1
NRAS
ERBB3
FLT3
PIK3CA
PTEN
TP53
CTNNB1
APC
SMAD4
NCOR1

If you insert a browser() into your eventReactive , you can check that your data.frame looks like this:

# c..BRAF....NF1....NRAS....ERBB3....FLT3....PIK3CA....PTEN....TP53... col.names
# 1                                                                  BRAF     genes
# 2                                                                   NF1     genes
# 3                                                                  NRAS     genes
# ...

You can fix this by using this expression instead: data.frame(genes = unlist(strsplit(input$title1,split='[\\r\\n]')))

Resulting data.frame :

# genes
# 1    BRAF
# 2     NF1
# 3    NRAS
# ...

I am not familiar with the package but it seems to me that the arguments passed to string_db$map were not correct:

  • First argument: the data.frame . When passing a reactive or eventReactive in Shiny you should include the parentheses (eg: subset_dataset() )
  • Second argument: use a character string: (eg: "genes" )

This works for me: mapped_genes <- string_db$map(subset_dataset(), "genes",removeUnmappedRows = TRUE)

Output:

产量

Bonus: I would add req(subset_dataset()) to the first line of renderPlot to avoid rendering when the data is null

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM