I think context is everything with this particular question, so if you are talking about doing this in the context of teaching then yeah — I’d move it to #teaching. In general, I think it would help to have more info on the setting in which you envision doing this, and the problem you’re trying to solve.
I would personally not use this specific code — if anything goes wrong, the recursion is either risking having a user whom you already don’t trust to install packages getting stuck in an endless loop of package installation, or the whole function is going to error out making it moot anyway. (Take a quick skim through our #package-installation tag if you need to convince yourself that things can go wrong in all sorts of ways when installing packages!). Along these lines, note also that specifying
type = source might not be a great idea, since this will fail on most Windows and Mac systems belonging to “average” useRs (who will not have installed the necessary build tools).
If you’re interested in talking more generally about some sort of code along these lines (maybe written a bit more safely), then I think that something like this can be totally okay in some circumstances, and can be anywhere from awfully rude to a complete disaster in other circumstances. It depends as much on human communication and shared understanding as it does on the technical scenario.
Your code is potentially making significant changes to the recipient’s environment (e.g., the whole tidyverse is a lot of packages when you include all the dependencies) and not cleaning up after itself, so speaking for myself, I would want to have the recipient’s informed consent before I did that.
In a teaching context, it’s also worth thinking about what you are communicating with your own actions and code and whether you want to pass those practices on to whoever you’re teaching. Learners tend to imitate the code practices they see their teachers use, and won’t know which parts are risky (and in what contexts) unless you call that out for them.