R data.table bricht exportierte Funktionen ab
Ich habe ein Problem damit, dass data.table in roxygen2-exportierten Funktionen funktioniert.
Hier ist eine einfache, gefälschte Funktion in einer Datei namens foo.R (im R-Verzeichnis meines Pakets), die data.table verwendet:
#' Data.table test function
#' @export
foo <- function() {
m <- data.table(c1 = c(1,2,3))
print(is.data.table(m))
m[,sum(c1)]
}
Wenn ich diese Funktion kopiere und in R einfüge, funktioniert diese Funktion einwandfrei:
> foo <- function() {
+ m <- data.table(c1 = c(1,2,3))
+ print(is.data.table(m))
+ m[,sum(c1)]
+ }
> foo()
[1] TRUE
[1] 6
Wenn ich aber einfach die exportierte Funktion lade, denkt R, dass die data.table ein data.frame ist und bricht ab:
> rm(foo)
> load_all()
Loading test_package
> foo
function() {
m <- data.table(c1 = c(1,2,3))
print(is.data.table(m))
m[,sum(c1)]
}
<environment: namespace:test_package>
> foo()
[1] TRUE
Error in `[.data.frame`(x, i, j) : object 'c1' not found
Was geht?
AKTUALISIEREN
Danke an @GSee für die Hilfe. Es sieht so aus, als wäre dies ein Devtools-Problem. Schauen Sie sich den folgenden interaktiven Befehlszeilencode an.
Nach dem Laden der Bibliothek test_packagefoo
läuft richtig:
> foo
function ()
{
m <- data.table(c1 = c(1, 2, 3))
print(is.data.table(m))
m[, sum(c1)]
}
<environment: namespace:test_package>
> foo()
[1] TRUE
[1] 6
Laufenload_all()
bricht foo:
> load_all()
Loading test_package
> foo()
[1] TRUE
Error in `[.data.frame`(x, i, j) : object 'c1' not found
Irgendwiesource('R/foo.R')
belebt foo Funktionalität:
> source('R/foo.R')
> foo
function() {
m <- data.table(c1 = c(1,2,3))
print(is.data.table(m))
m[,sum(c1)]
}
> foo()
[1] TRUE
[1] 6
Und zukünftige Anrufe anload_all()
nicht brechenfoo
nochmal:
> load_all()
Loading test_package
> foo
function() {
m <- data.table(c1 = c(1,2,3))
print(is.data.table(m))
m[,sum(c1)]
}
> foo()
[1] TRUE
[1] 6
Außerdem habe ich auf devtools 1.5 aktualisiert und versucht, es hinzuzufügen.datatable.aware=TRUE
, aber das schien nichts zu tun.