Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apply UDF with more than one argument in spark_apply() function #1540

Closed
sparkcassuser opened this issue Jun 7, 2018 · 2 comments
Closed

Comments

@sparkcassuser
Copy link

sparkcassuser commented Jun 7, 2018

i have R function with more than one argument and uses dplyr functions in it.

now, i want to apply this UDF to spark data frame.
The sample code

myfun=function(objdf,x,y,k){
  
  f <- function(x1,y1,x2,y2) { 
    d=(x2-x1) + (y2-y1)
  }   
  search=function(df,x,y,k){
    df1=data.frame(cbind(df,f(x,y,df$xx,df$yy)))
    colnames(df1)=c(colnames(df),"val")
    colnames(df1)
    new_df=df1 %>% arrange(val) %>% head(k)
    return(new_df)
  }
  
  searchwithk <- function(x,y,k) {force(x,y,k);function(df) search(df,x,y,k)}
  
  res <- spark_apply(objdf, function(df) {
    searchwithk(x,y,k)
  })
  #return(res)       
}

#df= spark_dataframe
x=12.12
y=-74.5
k=5
result=myfun(df,x,y,k)
result

it gives me long error / unused parameter in force statement

how to resolve?

@harryprince
Copy link

pass all your customized functions in spark_apply function block.

like this:

spark_apply(df,function(df){

search=function(df,x,y,k){
df1=data.frame(cbind(df,f(x,y,df$xx,df$yy)))
colnames(df1)=c(colnames(df),"val")
colnames(df1)
new_df=df1 %>% arrange(val) %>% head(k)
return(new_df)
}

searchwithk <- function(x,y,k) {force(x,y,k);function(df) search(df,x,y,k)}

return(searchwithk(x,y,k))

})

@javierluraschi
Copy link
Collaborator

You can also make use of the context parameter in spark_apply() if you need to run with different values of x,y,k. Closing since the question seems to be answered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants