Nicktz / Rbot

Making R and your Phone Best Friends
1 stars 1 forks source link

How to include arguments in Foo_Bot? #1

Open AlbertoAlmuinha opened 2 years ago

AlbertoAlmuinha commented 2 years ago

Hello, it is not clear to me how to include the arguments of the functions in Foo_Bot to later call them from Telegram. For example, the following doesn't work:

sum_fun <- function(x, y){return(x+y)}

Function_List$Foo4 <-
  list(Function = sum_fun,
       Call = "Sum_Example",
       Args = TRUE,
       Message = "x and y")

Foo_Bot(Bot_Name = Bot_Name, Function_List = Function_List, LoadMessage = "My connection with R",
        KillR = TRUE, KillCPU = FALSE)

If I make the following call from Telegram I get the following error:

/Sum_Example c(x = 1, y = 3)

Function ERROR (Connection still open): Function: /Sum_Example

I constantly come across this error and I don't understand how to enter the arguments of the functions... could you shed some light on it?

Thanks.

Nicktz commented 2 years ago

Hi Alberto,

First, you can replicate the error you are receiving in R by running:

  sum_fun <- function(x,y){print(x, y)}
  args <- c(1,2)
  sum_fun(args)

The idea with Foo_Bot is really to kick off more advanced processes on user side, as opposed to allowing multiple argument inputs from Telegram.

Foo_Bot it takes in a vector argument - so to achieve what you wanted, you could rather try:

sum_fun1 <- function(x){ print(sum( as.numeric(x) ) ) }

Function_List$Foo1 <- list(Function = sum_fun1, Call = "SE", Args = TRUE, Message = "x and y")

Foo_Bot(Bot_Name = "YourBot", Function_List = Function_List, LoadMessage = "My connection with R", KillR = TRUE, KillCPU = FALSE)

And in Telegram run:

/sum_fun1 1 2 3 4 5

Which should give you sum(1,2,3,4,5) in the prompt in R.

From a worfklow perspective- rather have many function calls ready (with appropriate names) that you can call that can execute more advanced commands on your side, as opposed to allowing multiple inputs to functions (like your sum example).

This means you should try to plan a workflow that requires single vector inputs ideally (as my example shows).

As an example, I've built calls in the past that execute functions on my side such as:

Exe_Foo <- function(X){ source( "XXXX/Function_myside_to_Source.R" )) Function_myside(X) }

And then from telegram:

/ Exe_Foo SP500

or

/Exe_Foo Nikkei

Which will then kick off processes.

Hope this helps?

N

Nico Katzke ____ https://bio.nfkatzke.com/ http://bio.nfkatzke.com/

On Sun, Sep 18, 2022 at 2:50 PM Alberto González Almuiña < @.***> wrote:

Hello, it is not clear to me how to include the arguments of the functions in Foo_Bot to later call them from Telegram. For example, the following doesn't work:

sum_fun <- function(x, y){return(x+y)}

Function_List$Foo4 <- list(Function = sum_fun, Call = "Sum_Example", Args = TRUE, Message = "x and y")

Foo_Bot(Bot_Name = Bot_Name, Function_List = Function_List, LoadMessage = "My connection with R", KillR = TRUE, KillCPU = FALSE)

If I make the following call from Telegram I get the following error:

/Sum_Example c(x = 1, y = 3)

Function ERROR (Connection still open): Function: /Sum_Example

  • Error details: Error in .f(...): argument "y" is missing, with no default

I constantly come across this error and I don't understand how to enter the arguments of the functions... could you shed some light on it?

Thanks.

— Reply to this email directly, view it on GitHub https://github.com/Nicktz/Rbot/issues/1, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADRS2NVDOKAK7QL2GHLP2DTV64FYNANCNFSM6AAAAAAQPN355A . You are receiving this because you are subscribed to this thread.Message ID: @.***>