What is the reprex
The reprex
is the English nickname "reproducible example".
The idea of the package and the function reprex
is to run the code in a "new" and "clean" environment, just as it will occur to those who try to reproduce their example/problem.
Thus, the simple production of a reprex
is itself already the guarantee that the example in question is reproducible.
How to use
1. Ctrl + c
There are several ways to use the reprex
. One of my favorite ones is copying the code and running it reprex::reprex()
. When the reprex
is used without arguments it checks if there is any code in your "crtl + c" and, if so, it uses to create the reproducible example.
Unfortunately this alternative is not so reproducible and I cannot create a reprex this way of using the reprex()
.
2. Inside the keys {}
Another alternative is to put the code that will be run as the first argument of reprex()
wrapped in keys ({}
). Making your example and running
library(reprex)
reprex({
(y <- 1:4)
mean(y)
})
We would have the piece down as a result.
(y <- 1:4)
#> [1] 1 2 3 4
mean(y)
#> [1] 2.5
Created on 2020-01-22 by the reprex package (v0.3.0)
Why go to the trouble?
The advantage of using the reprex()
is that he readily highlights mistakes that would cause anyone who helps you with the problem to give up trying. A good example is the one where you forget to share the data used to reproduce the problem.
In this example you could share only the piece of code
mean(meus_dados$y)
And even if it works for you, it won’t work for anyone else. This is because this code depends on the environment in which it was run and no one else has access to this environment.
If you just put that same line inside the reprex,
reprex({mean(meus_dados$y)})
The result will be:
mean(meus_dados$y)
#> Error in mean(meus_dados$y): objeto 'meus_dados' não encontrado
Created on 2020-01-22 by the reprex package (v0.3.0)
With this you will already know beforehand that you must readjust your code so that the example contains all the elements that allow it to be reproduced in another context or environment.
Thank you very much for the reply @Tomás Barcellos.
– Izak Mandrak
Just one more question, I tested on R and Reprex opened the code representation in a new browser window. How can I apply it here in Stack Overflow?
– Izak Mandrak
It already goes to "control + v". But if it doesn’t work, you can copy and paste the code you played in reprex.
– Tomás Barcellos