Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modified print function is not called from script in package directory #1264

Open
swhalemwo opened this issue Sep 4, 2023 · 0 comments
Open

Comments

@swhalemwo
Copy link

I'm using a custom function for printing data.table objects, which basically prints them with the formatting of tibbles.

print.data.table <- function(x, ...) {
  ans <- x
  attr(ans, "row.names") <- .set_row_names(nrow(x))
  attr(ans, "class") <- c("tbl", "data.frame")
  attr(ans, "sorted") <- NULL
  attr(ans, ".internal.selfref") <- NULL
  print(ans, ...)
invisible(x)
}

This has so far worked fine, but I've recently started writing my own package, in which many functions return data.table objects. When I call these functions from a script in my package directory, my custom print.data.table function isn't used, but the original one provided by data.table.

fx <- function() {
  as.data.table(mtcars)
}

calling fx from a script file (with ess-eval-line/C-j) doesn't apply my custom print.data.table function (output below).

> > fx()
     mpg cyl  disp  hp drat    wt  qsec vs am gear carb
 1: 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
 2: 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
 3: 22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
 4: 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
 5: 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
 6: 18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
 7: 14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
 8: 24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
 9: 22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
10: 19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
11: 17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
12: 16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
13: 17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
14: 15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
15: 10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
16: 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
17: 14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
18: 32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
19: 30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
20: 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
# more lines here

However, calling it in terminal/R process buffer directly applies my print.data.table function correctly:

> fx()
# A data frame: 32 × 11
     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1  21       6  160    110  3.9   2.62  16.5     0     1     4     4
 2  21       6  160    110  3.9   2.88  17.0     0     1     4     4
 3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1
 4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1
 5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2
 6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1
 7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4
 8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2
 9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2
10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4
# ℹ 22 more rows
# ℹ Use `print(n = ...)` to see more rows

This is also the output I get when calling it from a script file which is not inside a package directory.

When calling fx() from a script, there are also two chevrons (">") in the terminal output. However, this seems to be at most a side effect, since when I execute a region I get a single chevron for each statement, but still my print.data.table function isn't used:

10
fx()

results in

> 10
[1] 10
> fx()
     mpg cyl  disp  hp drat    wt  qsec vs am gear carb
 1: 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
 2: 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
 3: 22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
 4: 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
 5: 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
# more rows

Turning off ess-r-package-mode doesn't change this, nor does setting ess-r-package-auto-activate or ess-r-package-auto-enable-namespaced-evaluation to nil, and in Rstudio this problem does not occur. I have for now recompiled data.table with my print.data.table function instead of the original function (which isn't a big hassle since there are few data.table updates these days), but it's still a hacky band aid. Any ideas of what's going here or how to fix this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant