2

I have a sql table set up via dplyr like this:

num                   gameday_link stand b_height     px    pz    type
 1   1 gid_2016_04_05_houmlb_nyamlb_1     R      5-6  0.194 3.225    B
 2   1 gid_2016_04_05_houmlb_nyamlb_1     R      5-6  0.510 1.965    S
 3   1 gid_2016_04_05_houmlb_nyamlb_1     R      5-6 -1.367 2.459    B

What I would like to do is add an additional column called correct, which holds the T or F results of a custom function called isCallCorrectV. The parameters of isCallCorrectV are columns in the table.

Here is an example of isCallCorrectV in action:

> isCallCorrectV(c(0.0, 2.5), c(2.5, 0), c("S", "B"), c("6-0", "5-0"), c("R", "L"))
[1]  TRUE FALSE

I tried using this command, but I get an error.

dplyr::mutate(noswings, correct = isCallCorrectV(px, pz, type, b_height, stand))
Error in sqliteSendQuery(conn, statement) : 
error in statement: no such function: ISCALLCORRECTV
10
  • Provide code for the function and do not that spelling in R is case-sensitive. Commented Jun 20, 2016 at 23:11
  • 1
    It sounds like you're you're using a SQLite backend? That's not going to work with a custom function written in R. If you look at dplyr's vignette on databses you can find a list of the R functions that dplyr knows how to convert to SQL (and isCallCorrect isn't on that list). Commented Jun 20, 2016 at 23:17
  • 1
    If isCallCorrect is made up of only those functions then you could potentially write it out long-form inside mutate() to get your desired result. That won't be very friendly, and since your function calls another custom function that calls ldply I'm going to say it's not an option here. Commented Jun 20, 2016 at 23:33
  • 1
    Your two options are (1) (as you say) read the data into R and operate on it there using your custom R functions, or (2) rewrite the custom functions in SQL and define them in your database. Commented Jun 20, 2016 at 23:34
  • 1
    When you're using a database back-end with dplyr, what dplyr is doing for you is translating R syntax to SQL syntax. When you start using custom functions dplyr just tries to call them. If you're working in R and calling R functions, everything's great. If you're working in SQL and calling SQL functions, everything's great. But dplyr can't translate arbitrary R code to SQL, so your custom functions need to match the back-end language (and be defined on the back-end). Commented Jun 20, 2016 at 23:40

1 Answer 1

1

Per @Gregor's comments, it isn't possible to use custom functions in dplyr on a sql database. Only the functions define here can be used.

So my options are:

(1) Read the data into R and operate on it there using your custom R functions, or (2) rewrite the custom functions in SQL and define them in your database

I can use the dplyr collect command to read the data into R.

Sign up to request clarification or add additional context in comments.

1 Comment

broken link , seems exists no more

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.