evalWithAB <- function(expr, var, a, b) {
  env <- new.env()           
  assign(var, a, envir=env)  
                             
  atA <- eval(parse(text=expr), env)
                             
                             
  assign(var, b, envir=env)
  atB <- eval(parse(text=expr), env)
  return(atB - atA)
}

print(evalWithAB("2*x+1", "x", 5, 3))
print(evalWithAB("2*y+1", "y", 5, 3))
print(evalWithAB("2*y+1", "x", 5, 3))