You can do this using reprex.
As mt1022 noted, "... good package for producing minimal, reproducible example is "reprex" from tidyverse".
According to Tidyverse:
The goal of "reprex" is to package your problematic code in such a way that other people can run it and feel your pain.
An example is given on tidyverse web site.
library(reprex)
y <- 1:4
mean(y)
reprex()
I think this is the simplest way to create a reproducible example.
The one-step example is given on tidyverse web site.