I have built an app in RShiny. I am trying to allow a user to filter (using dbplyr) a dataframe stored in an Oracle database on whether the ration between two user-defined columns is more than a user-defined percentage. The following code runs, but does not filter the dataframe.
var_2 <- "column_2
my_df <- my_df %>%
filter({{var_1}} <= {{var_2}})
If I then change it to the following code,
var_2 <- "column_2
ratio_min <- 0.1
my_df <- my_df %>%
filter({{var_1}} <= ratio_min * {{var_2}})
I get the error [RStudio][OracleOCI] (3000) Oracle Caller Interface: ORA-01722: invalid number
However the following code,
var_2 <- "column_2
ratio_min <- 0.1
my_df <- my_df %>%
mutate(test_col = ratio_min * {{var_2}})
does not give me an error. Also when I filter by comparing any of the individual variables to a number, it filters fine.
The Oracle documentation (here) explains that this error message occurs when attempting and failing to convert character strings to numbers.
This means that in the database, at least one of the columns you are referencing is a character string not a number - most likely
var_2
. Hence when attempting the calculationratio_min * var_2
the database attempts a conversion ofvar_2
to numeric in order to multiply but is unable to.A common cause of this is characters like
,
and.
- the values look like numbers but are actually text:12,345,678.9
to numeric because of the commas as thousands separators. (Though they may convert to the money data-type.)0,1234
as equivalent to0.1234
.Even if R can handle the conversion correctly, with dbplyr the conversion text-to-number is happening on the database.
I would suggest the following:
TRY_CAST
or equivalent to cast data types only when relevant