简体   繁体   中英

non-numeric argument to binary operator error in R shiny

Whilst working on an R-shiny app, I came across an odd issue regarding the formatting of some numerical inputs. In this app, I would like to dynamically populate the content of different choice options (You would first specify the number of options you want to define and then the app would automatically adjust the number of inputs). I've used lapply() within renderUI to implement this. I then used the inputs (which are numerical) to compute some probabilities. It seems to work but I still get the error/warning message: "Warning: Error in *: non-numeric argument to binary operator". In the computation I applied as.numeric() to the inputs, but it does not seem to make a difference. Do you have any idea about what might be causing this issue? and how to fix it? Many thanks for your help!

rm(list=ls())
library(shiny)

ui = fluidPage(
  mainPanel(
    sidebarLayout(
      sidebarPanel(
        
        ### select number of options
        numericInput(
          inputId='nbopt',
          label='Number of options',
          value=2,
          min = 2,
          max = 10)),
      
      ### specify content of options
      mainPanel(
        fluidRow(
          column(width=5,
                 h2("Feature 1"),
                 uiOutput("perfx1")),
          column(width=5,
                 h2("Feature 2"),
                 uiOutput("perfx2"))),
        ### Display output table
        tableOutput('pcp_tab'))
      )))


server = function(input, output){
  
  # Define content of options  
  # Feature 1

  output$perfx1 = renderUI({
    lapply(1:input$nbopt, function(j){
      sliderInput(
        inputId=paste0('opt',j,'_x1'),
        label=paste0('Option ',j),
        value=20,
        min = 0,
        max = 40)
    })
  })
  
  # Feature 2

  output$perfx2 = renderUI({
    lapply(1:input$nbopt, function(j){
      sliderInput(
        inputId=paste0('opt',j,'_x2'),
        label=paste0('Option ',j),
        value=15,
        min = 0,
        max = 40)
    })
  })
  
  
  # Compute proba  
  pcp = reactive({
    #
    eu = NULL
    eu = sapply(1:input$nbopt, function(j){
      umove = as.numeric(input[[paste0('opt',j,'_x1')]]) * runif(100,0,1)
      uinfo = as.numeric(input[[paste0('opt',j,'_x2')]]) * runif(100,0,1)
      return(exp(umove + uinfo))})
    
    #
    prb = 100*eu/rowSums(eu)
    
    #
    pcp = NULL
    pcp = data.frame(
      opt =paste0('opt',1:input$nbopt),
      mu  =apply(prb,2,mean),
      se  =apply(prb,2,sd),
      stringsAsFactors=F)
    as.data.frame(pcp)
  })
  
  # Output table

  output$pcp_tab = renderTable({
    pcp()
  })
  
}

shinyApp(ui=ui, server=server)

Whilst working on an R-shiny app, I came across an odd issue regarding the formatting of some numerical inputs. I used numerical inputs to compute some probabilities but obtained the following error/warning message: "Warning: Error in *: non-numeric argument to binary operator".

The issue is that your sliderInput s do not exist when you start your app. Hence, eg input$opt1_x2 is NULL and the sapply inside your reactive pcp returns a list instead of a matrix . As a result the following code line 100*eu/rowSums(eu) will throw an error as eu is a list and a list isn't a numeric argument. To fix that you could eg add a req(input$opt1_x2) so that pcp will only run after the sliderInput s have been created:

  pcp <- reactive({
    req(input$opt1_x2)
    eu <- sapply(1:input$nbopt, function(j) {
      umove <- as.numeric(input[[paste0("opt", j, "_x1")]]) * runif(100, 0, 1)
      uinfo <- as.numeric(input[[paste0("opt", j, "_x2")]]) * runif(100, 0, 1)
      return(exp(umove + uinfo))
    })
    prb <- 100 * eu / rowSums(eu)

    data.frame(
      opt = paste0("opt", 1:input$nbopt),
      mu = apply(prb, 2, mean),
      se = apply(prb, 2, sd),
      stringsAsFactors = F
    )
  })

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