2022-01-26

What is ReprEx?

ReprEx means Reproducible Example

When asking for help, often online, it helps to provide enough information to EXACTLY REPRODUCE any error you might be getting

Also, take care to provide complete information

  • description of what you are trying to do

  • minimal data example

  • code you have already tried using minimal data

Is this ReprEx?

Q: Hello I’m adding legends to my boxplots but I found that if I clear the global environment this line

legend(x = 0.5, y = 28, legend = c(‘Year 17’, ‘Year 18’), fill = c(3, 5)))

stops working, it adds the legend correctly if I run the other lines and generate the graph beforehand but if I run the lot together I get the error message

Error in strwidth(legend, units = “user”, cex = cex, font = text.font) : plot.new has not been called yet

It would seem untidy to me to leave the script for the legend separate. Is there a way to get it to run with the rest of the code for that graph please?

Answer to a non-Reprex:

A: I would need a bit more code about what you are trying to answer, and your output.

I do not understand what scenario would make you clear your global environment between making a plot and drawing a legend on your plot?

e.g. something like this reproducible example works fine:

Answer to a non-Reprex:

var <- c(1,2,3,4)
factor <- c('a','a','b','b')
data <- data.frame(var, factor)

boxplot(var~factor, data=data,
        col=c('red','blue'))

legend(x=1, y = 3.5, legend = c('a', 'b'),
       col=c('red','blue'), pch=15)

Answer to a non-Reprex:

A good ReprEx has 5 features:

  • context

  • what do you want to do?

  • what have you tried (code, not screenshot)?

  • minimal data (probably also code)

  • error message or wrong outcome

Example: Compare means of just 2 of 3 flower spp

data(iris)
head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa

Example: Compare means of just 2 of 3 flower spp

my_iris <- iris[which(iris$Species != 'virginica')]

Error in `[.data.frame`(iris, which(iris$Species != "virginica")) : 
  undefined columns selected

Go through the steps

  • context

  • what do you want to do?

  • what have you tried (code, not screenshot)?

  • minimal data (probably also code)

  • error message or wrong outcome

references