简体   繁体   中英

R - error “non-numeric argument to binary operator” in shiny

I'm very new to r (and programming in general) and we were asked to make a web app using r shiny, the plan was to make a matrix operations calculator from different input files,(first.cvs file contains matrix 1, 2nd.cvs file contains matrix 2) but an error keeps appearing

 Listening on http://127.0.0.1:3420
 Warning: Error in FUN: non-numeric argument to binary operator
 99: eval
 98: eval
 97: Ops.data.frame
 96: renderTable [C:/Users/Acer/Desktop/FirstWebApp/app (1).R#45]
 95: func
 82: origRenderFunc
 81: output$oput
  1: runApp

this is my UI

     ui <- fluidPage(
  titlePanel("Multiple file uploads"),
  sidebarLayout(
    sidebarPanel(
     fileInput("file1",
               label="Upload CSVs here"),
     fileInput("file2", "upload file here"),
     selectInput("ops", "Select Operation",
                 choices = c("addition","subtraction","multiplication","division"))
     
  ),
  mainPanel(
     tableOutput("input_file"),
     tableOutput("input_file2"),
     tableOutput("oput")

and my server looks like this

server <- function(input, output) {
output$input_file <- renderTable({
  file_to_read =  input$file1
  if (is.null(file_to_read)) {
    return()
 }
  read.table(file_to_read$datapath, sep = ',', header = FALSE)
})

output$input_file2 <- renderTable({
  file_to_read =  input$file2
  if (is.null(file_to_read)) {
    return()
  }
  read.table(file_to_read$datapath, sep = ',', header = FALSE)
})
output$oput <- renderTable({
switch(input$ops,
       "addtion" = input$file1 + input$file2,
       "subtraction" = input$file1 - input$file2,
       "multiplication" = input$file1 * input$file2,
       "division" = input$file1 / input$file2)
})
}

how do I fix this and if this error is fixed will the program run?

Your Ui is good no need to change anything. But your server code has minor corrections. Problem lies in your switch case:

switch(input$ops,
       "addtion" = input$file1 + input$file2,
       "subtraction" = input$file1 - input$file2,
       "multiplication" = input$file1 * input$file2,
       "division" = input$file1 / input$file2)
})

here you are adding input$file1 & input$file2. input$file1 is not a matrix of your data, but it is a dataframe of:

Browse[1]> input$file1
       name size     type
1 file1.csv   21 text/csv
                                                                          datapath
1 C:\\Users\\temp\\AppData\\Local\\Temp\\RtmpCEZJPF/957a518de2fae08f6a7b7201/0.csv

Therefore you have to store your matrix data in a reactiveVal() for using them later in the switch case

server <- function(input, output) {
  
  file1_Result <- reactiveVal(NULL)
  file2_Result <- reactiveVal(NULL)
  
  
  output$input_file <- renderTable({
    file_to_read =  input$file1
    if (is.null(file_to_read)) {
      return()
    }
   Result <- read.table(file_to_read$datapath, sep = ',', header = FALSE)
   file1_Result(Result)
   return(Result)
  })
  
  output$input_file2 <- renderTable({
    file_to_read =  input$file2
    if (is.null(file_to_read)) {
      return()
    }
    Result <- read.table(file_to_read$datapath, sep = ',', header = FALSE)
    file2_Result(Result)
    return(Result)
  })
  output$oput <- renderTable({
    browser()
    switch(input$ops,
           "addtion" = file1_Result() + file2_Result(),
           "subtraction" = file1_Result() - file2_Result(),
           "multiplication" = file1_Result() * file2_Result(),
           "division" = file1_Result() / file2_Result())
  })
}

Hope this works:-)

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