When you write a script that includes variable assignment with <- inside a Custom R Command, the following error may occur:
Error in %>% var_name <- "..." :
could not find function "%>%<-"
A Custom R Command is treated as “a single pipe operation on a data frame (. %>% function(...))”. Because of this, when you write a <- assignment statement immediately after the pipe, R tries to interpret %>% and <- together as a single function called %>%<-, which results in the “could not find function” error.
Solution
If you want to run a multi-line script that includes assignments in a Custom R Command, wrap the entire script in { } so that it is treated as a single block.
Example:
{
threshold <- 100
df <- .
filter(df, Sales > threshold)
}
By wrapping the code in { }, you can write variable assignments and multi-step processing inside the block, and the value of the last expression (in this example, filter(df, ...)) is treated as the return value of the Custom R Command.