-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathobj_inspector.Rmd
95 lines (57 loc) · 1.87 KB
/
obj_inspector.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
---
title: "Suggested packages"
output: github_document
---
`R` has the issue that some `R` objects that don't work correctly if their package is not attached (common to `xts`, `tibble`, and `data.table`). This is, unfortunately, easier to do in `R` than in other languages (for example `Python`'s `pickle` will attach packages).
A solution is: `saveRDS()` could be augmented to add the `suggested_packages()` as an attribute of what it writes out, say ".suggested_packages`. Then `readRDS()` could look for this attribute and issue a warning if any of them are not attached during the read.
To explore the feasibility of the above, let's experiment with finding suggested packages (without package order) for `R` objects.
First: define the suggestion function.
```{r setup}
source("find_pkgs.R")
```
Example 1: an `xts` object.
```{r}
library("tibble")
library("xts")
data(sample_matrix)
sample.xts <- as.xts(sample_matrix, descr='my new xts object')
suggested_packages(sample.xts)
```
However, notice if `data.table` is attached the advice changes.
```{r}
library("data.table")
suggested_packages(sample.xts)
```
We can ask for more details to see why this is.
```{r}
suggested_packages(sample.xts, show_details = "data.table")
```
Example 2: a `tibble`.
```{r}
d <- as_tibble(data.frame(x = 1))
suggested_packages(d)
```
Example 3: a `data.table`.
```{r}
dt <- data.table(x = 2)
suggested_packages(dt)
```
Example 4: `data.frame`
```{r}
df <- data.frame(x = 4)
suggested_packages(df)
```
Example 5: nested stuff.
```{r}
df2 <- data.frame(x = 1)
df2$y <- list(tibble(x = 5))
class(df2)
suggested_packages(df2)
```
Example 6: `tidyverse` adding packages to the outcome
```{r}
library("tidyverse") # extra packages to show interference effects
d <- as_tibble(data.frame(x = 1))
suggested_packages(d)
suggested_packages(d, show_details = c("dplyr", "ggplot2", "tidyr"))
```