Constructing isolated minimum reproducible examples is at the heart of figuring things out in R (and python) (Figure 1). As Jenny Bryan has pointed out (Figure 2), more often than not, just making the minimum reproducible example helps one come to the answer on their own. However, often times we might need to reach out to others for inspiration. When answering questions, for example on StackOverflow, it is useful when answers are also reproducible. For either asking questions or supplying answers, the reprex
library is very useful. It however, took me a bit to figure out how to use the reprex()
function with a multi-line example. The trick is to place the whole affair inside curly braces {}
like this reprex({SEVERAL LINES OF CODE})
. This reprex illustrates how to convert a dataframe to a ts
object.
```{r}
#| message: false
reprex({ # This is the key, put the entire thing inside {}
library(zoo)
df <- data.frame(date = as.Date('2022-01-01') + 0:9,
values = runif(10, 10, 500) + seq(50, 59)^2)
df
str(df)
tseries <- read.zoo(df)
str(tseries)
class(tseries)
tseries_ts <- as.ts(tseries)
class(tseries_ts)
}) # This is where it closes
```
The out put of this can be pasted straight into StackOverflow (I added chunk arguments here to suppress warnings and messages):
```{r warning=FALSE, message=FALSE}
library(zoo)
#> Warning: package 'zoo' was built under R version 4.2.3
#>
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#>
#> as.Date, as.Date.numeric
df <- data.frame(date = as.Date('2022-01-01') + 0:9,
values = runif(10, 10, 500) + seq(50, 59)^2)
df
#> date values
#> 1 2022-01-01 2612.644
#> 2 2022-01-02 3037.105
#> 3 2022-01-03 2744.887
#> 4 2022-01-04 2919.702
#> 5 2022-01-05 3147.747
#> 6 2022-01-06 3118.888
#> 7 2022-01-07 3398.436
#> 8 2022-01-08 3469.737
#> 9 2022-01-09 3773.563
#> 10 2022-01-10 3813.098
str(df)
#> 'data.frame': 10 obs. of 2 variables:
#> $ date : Date, format: "2022-01-01" "2022-01-02" ...
#> $ values: num 2613 3037 2745 2920 3148 ...
tseries <- read.zoo(df)
str(tseries)
#> 'zoo' series from 2022-01-01 to 2022-01-10
#> Data: num [1:10] 2613 3037 2745 2920 3148 ...
#> Index: Date[1:10], format: "2022-01-01" "2022-01-02" "2022-01-03" "2022-01-04" "2022-01-05" ...
class(tseries)
#> [1] "zoo"
tseries_ts <- as.ts(tseries)
class(tseries_ts)
#> [1] "ts"
```
date values
1 2022-01-01 2619.907
2 2022-01-02 2881.659
3 2022-01-03 3196.352
4 2022-01-04 2957.410
5 2022-01-05 3102.946
6 2022-01-06 3309.639
7 2022-01-07 3566.296
8 2022-01-08 3738.157
9 2022-01-09 3517.246
10 2022-01-10 3870.419
'data.frame': 10 obs. of 2 variables:
$ date : Date, format: "2022-01-01" "2022-01-02" ...
$ values: num 2620 2882 3196 2957 3103 ...
'zoo' series from 2022-01-01 to 2022-01-10
Data: num [1:10] 2620 2882 3196 2957 3103 ...
Index: Date[1:10], format: "2022-01-01" "2022-01-02" "2022-01-03" "2022-01-04" "2022-01-05" ...
[1] "zoo"
[1] "ts"
Citation
@online{craig2023,
author = {Craig, Nathan},
title = {Creating a {Reproducible} {Example} with `Reprex::reprex()`},
date = {2023-10-21},
url = {https://ncraig.netlify.app/posts/2023-10-21-reprex-example/index.html},
langid = {en}
}