Why is RSA preferred over Diffie-Hellman if they're both used to establish shared key?
RSA is rarely appropriate instead of DH for key exchange. It's mostly only appropriate if backwards-compatibility is required, or if keys need to be sent from one party to the other instead of generated for each session (eg for encrypting e-mail). 

RSA is much, much harder to use securely. RSA requires padding of the messages, which is quite easy to screw up in ways that catastrophically break the security of the system. RSA is far more expensive to use in an ephemeral mode, and thus typically cannot provide forward secrecy. RSA keys are substantially larger than ECDH (Elliptic Curve Diffie-Hellman) keys. RSA key generation is much more complex (and harder to get right) than (some) ECDH key generation: RSA needs to generate large prime numbers &amp; ensure that they are safe, while X25519 or X448 ECDH systems can use any random number of the appropriate length. 

Where possible use ECDHE (Elliptic Curve Diffie-Hellman, Ephemeral). If you can't do that, use DHE. If you can't do that, use ECDH. If you can't do that, use DH. If you can't do that, THEN use RSA. 

RSA can also be used as part of a signature scheme, but that's not relevant to this question since it's not comparable to DH which only does key exchange. TLS 1.3 uses RSA only for signing the DHE or ECDHE key exchange, and also supports ECDSA for signing.

NaCL, Libsodium, Libhydrogen, TLS 1.3, monocypher, and probably a few other libraries and cryptographic standards have dropped the use of RSA for key exchange.