In reply to Soheil.

Break R into 10 pieces (roughly 6909 rows each) and call the function 10 times, saving the closest point and the distance for each run. Then of the 10 closest neighbors in each subproblem, choose the closest overall.