在第二个R函数中通过一个R函数调用用户输入

时间:2015-10-29 22:27:51

标签: r function user-input readline

非常感谢您提前帮助我 - 我是R编程的新手,并且一直试图使用通过另一个函数接受的用户输入。第二个功能是一个简单的工资计算器,其中三个变量是小时数,每小时工资率和一旦工作小时数超过180时费率乘以的次数。我写了一个名为enterval的第一个函数,我通过它我要求用户输入上述变量。在第二个名为salary的函数中,我试图在运行支付计算之前使用enterval接受输入。我陷入困境,因为第二个函数,工资,当我达到“if”条件时,它正在破坏,指定h>我在下面分享我的代码。再次感谢您的帮助。我在之前的答案中搜索过但找不到完全回答我的查询的特定实例 - 如果我错过了之前适当的回复,请道歉。 我在运行此代码时遇到的错误是“h> 180中的错误:   比较(6)仅适用于原子和列表类型“

enterval <- function()  {
h <- (readline("Please enter number of hours: "))
h <- as.integer(h)
r <- (readline("Please enter applicable rate: "))
r <- as.integer(r)
m <- (readline("Please confirm your multiplier: "))
m <- as.integer(m)
}
salary <- function ()  {
    enterval()
    if (h > 180)  {
        totalpay <- (180*r) + ((h-180)*r*m)
        }
    else  {
        totalpay <- (h*r)
        }
    totalpay
}

2 个答案:

答案 0 :(得分:2)

我认为你需要的是这样的功能:

//if the user inputs equals go
    else if (parts.length == 2 && parts[0].equalsIgnoreCase("go"))
    {
        //if the second part of the input is nth
        if(parts[1].equalsIgnoreCase("nth"))
        {
            //gets the room the player is in
            Room temp = getRoomLoc(player.getLocation());
            //if the player can move north
            if(temp.canMoveNth())
            {
                //sets the player's location to the room to the north
                player.setLocation(temp.getNth());
            }
            //if the player can't move north this checks why
            if(!temp.canMoveNth())
            {
                //if there is a door to the north
                if(temp.getNth()>-1)
                {
                    //checks if the door is locked
                    if(temp.getRoomlock().NthLocked)
                    {
                        //this code shouldn't run if there isn't a door
                       System.out.println("The Door to the north is "
                               + "locked");
                    }
                    else
                    {
                        //if this runs something is FUBAR
                        System.out.println("Error, you broke it: North");
                    }
                }
                //if there isn't a door to the north
                else
                {
                    System.out.print("There is no door to the north.");
                }
            }
        }
        //if the second part of the player's input is south
        else if(parts[1].equalsIgnoreCase("sth"))
        {
            //gets the player's location
             Room temp = getRoomLoc(player.getLocation());
             //if the player can move south
            if(temp.canMoveSth())
            {
                //sets the players location to the south
                player.setLocation(temp.getSth());
            }
            //if the player can't move south
            if(!temp.canMoveSth())
            {
                //if there is a door
                if(temp.getSth()>-1)
                {
                    //checks if it is locked
                    if(temp.getRoomlock().SthLocked)
                    {
                        //this code shouldn't run if there isn't a door
                       System.out.println("The Door to the south is "
                               + "locked");
                    }
                    else
                    {
                        //if this runs something is FUBAR
                        System.out.println("Error, you broke it: South");
                    }
                }
                //if there isn't a door
                else
                {
                    System.out.print("There is no door to the south.");
                }
            }
        }
        //if the second part of the player's input is east
        else if(parts[1].equalsIgnoreCase("est"))
        {
            //gets the the player's location
             Room temp = getRoomLoc(player.getLocation());
             //if the player can move east
            if(temp.canMoveEst())
            {
                //sets the player's location to the east
                player.setLocation(temp.getEst());
            }
            //if the player can't move east
            if(!temp.canMoveEst())
            {
                //if there is a door
                if(temp.getEst()>-1)
                {
                    //checks if it is locked
                    if(temp.getRoomlock().EstLocked)
                    {
                        //this code shouldn't run if there isn't a door
                       System.out.println("The Door to the east is "
                               + "locked");
                    }
                    else
                    {
                        //if this runs something is FUBAR
                        System.out.println("Error, you broke it: East");
                    }
                }
                //if there is no door
                else
                {
                    System.out.print("There is no door to the east.");
                }
            }
        }
        //if the second part of the player's input is west
        else if(parts[1].equalsIgnoreCase("wst"))
        {
            //gets the player's location
         Room temp = getRoomLoc(player.getLocation());
         //if the player can move west
            if(temp.canMoveWst())
            {
                //sets the player's location to the west
                player.setLocation(temp.getSth());
            }
            //if the player can't move west
            if(!temp.canMoveWst())
            {
                //if there is a door
                if(temp.getWst()>-1)
                {
                    //checks if it is locked
                    if(temp.getRoomlock().SthLocked)
                    {
                        //this code shouldn't run if there isn't a door
                       System.out.println("The Door to the west is "
                               + "locked");
                    }
                    else
                    {
                        //if this runs something is FUBAR
                        System.out.println("Error, you broke it: West");
                    }
                }
                //if there is no door
                else
                {
                    System.out.print("There is no door to the west.");
                }
            }    
        }

        else
        {
            System.out.println("\""+ parts[1] +"\""+" is not a valid "
                    + "direction");
        }

输出:

enterval <- function()  {
  h <- (readline("Please enter number of hours: "))
  h <- as.numeric(h)
  r <- (readline("Please enter applicable rate: "))
  r <- as.numeric(r)
  m <- (readline("Please confirm your multiplier: "))
  m <- as.numeric(m)
  list(h=h, r=r, m=m)
}

salary <- function ()  {
  inputs <- enterval()
  if (inputs$h > 180)  {
    totalpay <- (180*inputs$r) + ((inputs$h-180)*inputs$r*inputs$m)
  }
  else  {
    totalpay <- (inputs$h*inputs$r)
  }
  totalpay
}

在您的问题中,> salary() Please enter number of hours: 5 Please enter applicable rate: 0.5 Please confirm your multiplier: 2 [1] 2.5 只返回enterval中存储的值,但即使没有保存在任何地方(因为您没有将其分配给m内的变量,所以它不能由salary使用。在R函数中只返回最后一个对象(或者如果使用函数salary则返回的内容)。在上面的函数中,我返回一个包含元素return,{{的列表1}}和h

然后我将该列表保存到rm可以使用该列表。可以使用inputs运算符访问salary中的元素。

此外,作为一个小小的添加,当你说率我相信它是0-1之间的数字所以我将inputs更改为$因为as.integer将向下舍入到整数。如果确实需要整数,请随意将其更改回as.numeric

修改

更好,可能更高级的写作方式as.integer

根据@RichardScriven的评论,避免键入所有as.integer变量的好方法是使用salary,如下所示:

input$*

list2env基本上会从salary的环境中的列表元素中创建变量,这些变量可以立即访问,而无需使用salary <- function () { inputs <- enterval() list2env(inputs, environment()) if (h > 180) { totalpay <- (180*r) + (h-180)*r*m) } else { totalpay <- (h*r) } totalpay }

答案 1 :(得分:1)

R函数中分配的变量(类似于许多其他编程语言)的范围有限,这意味着您在函数中指定的m只能在该函数中使用。如果您希望变量在函数外部可用,则有两个主要选项:

  1. 返回变量,这是首选选项,它更清晰,并且是很好的编程习惯,因为很多原因在很多堆栈溢出帖子中描述。要记住的一件重要事情是函数只能返回一个变量。

  2. 您可以进行全局分配,这将使您的函数中的变量具有全局范围,并且可以在所有函数中访问。与m <<- 1相反,此代码为m <- 1。出于各种原因,不建议这样做。有关此主题的更多信息,请参阅Global variables in RGlobal and local variables in R

  3. 由于您只能返回一个变量,因此您可以将所有三个对象放入数据框或列表中并返回该变量。虽然我会质疑你是否想要在函数中完成值输入。此外,如果您的用户输入是您的目标的主要内容,则R可能不是正确的语言。这就是说下面的代码实现了你正在寻找的东西

    enterval <- function()  {
      h <- (readline("Please enter number of hours: "))
      h <- as.integer(h)
      r <- (readline("Please enter applicable rate: "))
      r <- as.integer(r)
      m <- (readline("Please confirm your multiplier: "))
      m <- as.integer(m)
      salaryVariables <- data.frame("hours" = h, "rate" = r, "multiplier" = m)
      return(salaryVariables)
    }
    salary <- function(salaryInfo)  {
      r <- salaryInfo$rate
      h <- salaryInfo$hours
      m <- salaryInfo$multiplier
      if (h > 180)  {
        totalpay <- (180*r) + ((h-180)*r*m)
      }
      else  {
        totalpay <- (h*r)
      }
      return(totalpay)
    }
    
    mySalary <- enterval()
    salary(mySalary)