错误:二元运算符的非数字参数 [英] Error: non-numeric argument to binary operator

查看:69
本文介绍了错误:二元运算符的非数字参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,在 RStudio/Shiny 中制作应用程序对我来说很新鲜,我正在处理一个将外部文件用作输入数据的项目.这是我的 CSV:

Good Day, making application in RStudio/Shiny is very new to me and I am working with a project in which an external file is to be used as the input data. Here is my CSV:

我应该是应用程序:

因此,用户在第 1 行插入带有设备名称的 csv,在第 2 行插入瓦数,就像上图一样,然后 selectInput 将被更新.用户选择什么设备和使用时间,然后计算其成本

So the user insert a csv with appliance name in row 1 and their wattage in row 2 just like in the picture above, then the selectInput will be updated. The user choose what appliance and hours of usage then it will be calculated for its cost

这是我的服务器代码:

server <- function(input,output,session) {
  
  observeEvent(input$file1, {
    
    mytable <- read.csv(input$file1$datapath)
    
    updateSelectInput(session, "select", "Appliances", 
                      choices = colnames(mytable))
    
  })
  
  dataset_day <- reactive ({
    costday <- ((input$select * input$hour) / 1000) * 8.9535
    costday <- data.frame(costday)
    names(costday)  <- "Electricity Cost per day (Philippine Peso)"
    print(costday)
  })
  
  dataset_month <- reactive ({
    costmonth <- (((input$select * input$hour) / 1000) * 8.9535) * 30
    costmonth <- data.frame(costmonth)
    names(costmonth)  <- "Electricity Cost per month (Philippine Peso)"
    print(costmonth)
  })

我需要帮助将用户在所选设备的 CSV 中写入的值放入等式.

I need help in putting the value written in the CSV of the chosen appliance by the user to the equation.

简而言之,如何将非数字参数修复为二元运算符错误.

In short how to fix the non-numeric argument to binary operator error.

我认为错误出在 input$select 中,但我对此迷茫了.

I think the error is in the input$select but I am lost about this.

((input$select * input$hour) / 1000) * 8.9535

我是否需要更改或添加某些内容才能解决此问题?这个应用程序可以做吗?提前致谢!

Do I need to change or add something in order to fix this? Is this app possible to do? Thank you in advance!

推荐答案

当您理解这种反向非消息时,这个完全非描述性的错误消息完全正确:

This utterly non-descriptive error message is totally spot on, when you understand this backwards non-message:

您正在尝试使用非数字的东西进行数学运算.

You are trying to do math with something that is not numbers.

在这种情况下,您可能是正确的,因为它出现在 input$select * input$hour 中,因为其中一个或两个都可能是字符.在应用程序的 HTML 端,值中没有数字.只有字符串.那么我们能得到什么?

In this case, you are probably correct in that it occurs in input$select * input$hour, because either or both are probably characters. On the HTML side of your app, there are no numbers in the values. Only strings. So what do we get back?

尝试插入程序员已知的最佳调试工具:打印语句.在这里,尝试简单地添加一个观察者,例如:

Try inserting the Best Debugging Tool known to programmer: print-statements. Here, try simply adding an observer e.g.:

observe({
  cat('This is observer\n')
  str(input$select)
  str(input$hour)
})

您现在应该在控制台中看到如下所示的内容:

You should now see something in your console like this:

This is observer
  chr "Bulb"
  num 12.0

This is observer
  chr "11"
  num 12.0

这意味着什么?input$select 不是数字,而是一个字符串.

What does this imply? That input$select is not numeric, but a character string.

解决方案:将其转换为数字,例如as.numeric(后一种情况),或将其映射到数字(前一种情况).

Solution: Convert it to numeric with e.g. as.numeric (latter case), or map it to something numeric (former case).

这篇关于错误:二元运算符的非数字参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆