How does it work and how to use the Reprex function of R?

Asked

Viewed 77 times

2

In researches right here in Sopt I saw suggestions of reproducible examples. One of them was the function reprex(), I researched about and saw that its use is specific for HTML, Github and here in Stack Overflow. But I still don’t understand how it works.

I followed the steps in reprex, but R brings me an error message:

install.packages("reprex")
library(reprex)

(y <- 1:4)
mean(y)

reprex()

Rendering reprex...
Error: callr subprocess failed: <text>:18:7: '/' inesperado
17: #+ reprex-body
18: https:/
          ^

What am I doing wrong? How it works and how to use the function reprex of R?

1 answer

4


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.

  • 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?

  • It already goes to "control + v". But if it doesn’t work, you can copy and paste the code you played in reprex.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.