$\pi_0: Sp\to Ab$ is a direct sum preserving functor, and it sends $E_1$-ring spectra to rings, and modules over them to modules over them.
In particular you get a functor $\pi_0: Mod_R\to Mod_{\pi_0(R)}$. If $R^n\simeq R^m$ as $R$-modules, then $\pi_0(R)^n\cong \pi_0(R)^m$ as $\pi_0(R)$-modules. So if $\pi_0(R)$ is commutative and nonzero (which is a much weaker hypothesis than $R$ having an $E_\infty$-structure), this implies $n=m$.
More generally, it suffices that $\pi_0(R)$ have the invariant basis property.
Note that conversely, if $\pi_0(R)$ does not have the invariant basis property, we can find inverse nonsquare matrices $M,N$ with coefficients in $\pi_0(R)$, and you can view them as elements of $\pi_0map_R(R^n,R^m)$ ($\pi_0map_R(R^m,R^n)$ respectively), and their matrix product corresponds to the composition up to homotopy, so that $R^n\simeq R^m$ as $R$-modules.
So it's an "if and only if" situation with $\pi_0(R)$.
A related claim is the fact that group-completion $K$-theory (here meaning only $K_0$) only sees $\pi_0$, namely if $R$ is a ring spectrum, then the group-completion $K$-theory of projective $R$-modules (summands of $R^n$ for some finite $n$, no shifts) is the same as that of $\tau_{\geq 0}R$ (this is true for the whole group completion K-theory space), which is the same as that of $\pi_0(R)$ (this is true only for $K_0$)