This is a symbolic math problem, R typically deals with numerical solutions. For symbolic algebra there is a library called caracas that can be used for symbolic calculations. You will need however a python installation since the caracas library uses calls to sympy python routines to do the symbolic calculation.
You can find more information about how to use the package in https://journal.r-project.org/articles/RJ-2023-090/