This question arises because I wish to make a function for my convenience:
as.numeric_psql <- function(x) {
return(as.numeric(as.integer(x)))
}
to convert boolean values in a remote postgres table into numeric. The step to convert to integer is needed as:
There is no direct cast defined between numeric and boolean. You can use integer as middle-ground. (https://mcmap.net/q/758270/-cannot-cast-type-numeric-to-boolean)
Of course this function works as expected locally:
copy_to(con_psql, cars, 'tmp_cars')
tmp_cars_sdf <-
tbl(con_psql, 'tmp_cars')
tmp_cars_sdf %>%
mutate(low_dist = dist < 5) %>%
mutate(low_dist = as.numeric(as.integer(low_dist)))
# # Source: lazy query [?? x 3]
# # Database: postgres 9.5.3
# speed dist low_dist
# <dbl> <dbl> <dbl>
# 1 4 2 1
# 2 4 10 0
# 3 7 4 1
# 4 7 22 0
# 5 8 16 0
cars %>%
mutate(low_dist = dist < 5) %>%
mutate(low_dist = as.numeric_psql(low_dist)) %>%
head(5)
# speed dist low_dist
# 1 4 2 1
# 2 4 10 0
# 3 7 4 1
# 4 7 22 0
# 5 8 16 0
However, it doesn't work when used on the remote data frame, since as.numeric_psql
is not in the list of sql translations, so is passed to the query verbatim:
> tmp_cars_sdf %>%
+ mutate(low_dist = dist < 5) %>%
+ mutate(low_dist = as.numeric_psql(low_dist))
Error in postgresqlExecStatement(conn, statement, ...) :
RS-DBI driver: (could not Retrieve the result : ERROR: syntax error at or near "as"
LINE 1: SELECT "speed", "dist", as.numeric_psql("low_dist") AS "low_...
^
)
My question is whether there exist a easy way (i.e. not defining a custom sql translation) of getting dplyr to understand that the function as.numeric_psql
is a composition of functions that have existing sql translations, and to use those translations instead.