How do I calculate the coefficient of determination (R²) in R?

You can use the summary() function to view the  of a linear model in R. You will see the “R-squared” near the bottom of the output.