2014-11-25 10 views
7

Ho la seguente funzione per uso personale. Riceve il nome di un autore per vedere se ho dei loro pacchetti sul mio computer.Perché i rownames (installed.packages()) hanno un attributo nomi?

authoredPackages <- function (author) 
{ 
    s <- sapply(rownames(installed.packages()), 
     packageDescription, fields = "Author") 
    names(grep(author, s, value = TRUE)) 
} 

Ecco il problema. All'apertura di una nuova sessione R e all'assegnazione della funzione, la prima chiamata alla funzione sempre restituisce un vettore di caratteri di stringhe vuote sulla lunghezza corretta del vettore che si suppone debba restituire. Per mostrarlo, apri una nuova sessione R, assegna la funzione ed eseguila con il cognome dell'autore del tuo pacchetto preferito. Va innanzitutto restituire un vettore di carattere vuoto ...

authoredPackages("Temple Lang") 
# [1] "" "" "" "" 

... e poi farlo di nuovo e restituisce il risultato corretto ...

authoredPackages("Temple Lang") 
# [1] "jsonlite" "RCurl" "RJSONIO" "XML"  

esso sempre avviene solo alla prima chiamata in una nuova sessione R. Perché ciò accade e come posso risolverlo in modo che la funzione funzioni sempre al primo tentativo?

mie info R --vanilla sessione:

R version 3.1.1 (2014-07-10) 
Platform: x86_64-pc-linux-gnu (64-bit) 

locale: 
[1] LC_CTYPE=en_US.UTF-8  LC_NUMERIC=C    
[3] LC_TIME=en_US.UTF-8  LC_COLLATE=en_US.UTF-8  
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 
[7] LC_PAPER=en_US.UTF-8  LC_NAME=C     
[9] LC_ADDRESS=C    LC_TELEPHONE=C    
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C  

attached base packages: 
[1] stats  graphics grDevices utils  datasets methods base 

Aggiornamento: All'apertura R, è sembra rownames(installed.packages()) ha un attributo names a causa del pacchetto di lme4. Non so perché, ed è l'unico nome. È anche molto strano come scompaia alla seconda chiamata.

rownames(installed.packages())[228] 
# ret0 
# "lme4" 
+1

A partire da R --vanilla? 'SessionInfo()'? per me 'authoredPackages()' funziona come previsto per R versione 3.1.2 Patched (2014-10-31 r66919)/Piattaforma: x86_64-unknown-linux-gnu (64-bit) e R Under development (unstable) (2014- 11-23 r67046)/Piattaforma: x86_64-unknown-linux-gnu (64-bit) –

+2

sembra come 'ret0' è menzionato nel sorgente' installed.packages', quando noCache = FALSE verso la fine della funzione; forse aggiungere 'noCache = TRUE' come argomento è un work-around? –

+1

@MartinMorgan: Bella ipotesi, ma l'aggiunta di 'noCache = TRUE' produce ancora gli stessi risultati per me. –

risposta

7

strano, ma sembra rownames(installed.packages()) ha un names attribuire la prima volta che si chiama.

> str(rownames(installed.packages())) 
Named chr [1:125] "bdsmatrix" "bitops" "blotter" "brew" "car" "changepoint" "chron" "colorout" ... 
- attr(*, "names")= chr [1:125] "" "" "" "" ... 
> str(rownames(installed.packages())) 
chr [1:125] "bdsmatrix" "bitops" "blotter" "brew" "car" "changepoint" "chron" "colorout" "colorspace" ... 

Siamo spiacenti, che ha lasciato a voi per rispondere alla domanda. Assicurati che non ci siano nomi. Questo è un problema per te perché ti stai affidando al valore sapply predefinito di USE.NAMES=TRUE, ma questo aggiunge solo nomi se non sono già presenti. E sono presenti per qualche motivo davvero strano.

authoredPackages <- function (author) 
{ 
    r <- setNames(rownames(installed.packages()), NULL) 
    s <- sapply(r, function(x) packageDescription(x)$Author) 
    names(grep(author, s, value = TRUE)) 
} 

Ecco la mia sessionInfo (avvio con R --vanilla):

> sessionInfo() 
R version 3.1.1 (2014-07-10) 
Platform: x86_64-pc-linux-gnu (64-bit) 

locale: 
[1] LC_CTYPE=en_US.UTF-8  LC_NUMERIC=C    
[3] LC_TIME=en_US.UTF-8  LC_COLLATE=en_US.UTF-8  
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 
[7] LC_PAPER=en_US.UTF-8  LC_NAME=C     
[9] LC_ADDRESS=C    LC_TELEPHONE=C    
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C  

attached base packages: 
[1] stats  graphics grDevices utils  datasets methods base  

loaded via a namespace (and not attached): 
[1] tools_3.1.1 

Ho appena aggiornato a R-3.1.2 e ha cercato di nuovo. Ottengo ancora gli stessi risultati strani e li ottengo in modo coerente se uso il suggerimento di @MartinMorgan per utilizzare noCache=TRUE.

> str(rownames(installed.packages(noCache=TRUE))) 
Named chr [1:125] "bdsmatrix" "bitops" "blotter" "brew" "car" "changepoint" "chron" "colorout" ... 
- attr(*, "names")= chr [1:125] "" "" "" "" ... 
> str(rownames(installed.packages(noCache=TRUE))) 
Named chr [1:125] "bdsmatrix" "bitops" "blotter" "brew" "car" "changepoint" "chron" "colorout" ... 
- attr(*, "names")= chr [1:125] "" "" "" "" ... 
> str(rownames(installed.packages(noCache=TRUE))) 
Named chr [1:125] "bdsmatrix" "bitops" "blotter" "brew" "car" "changepoint" "chron" "colorout" ... 
- attr(*, "names")= chr [1:125] "" "" "" "" ... 
> sessionInfo() 
R version 3.1.2 (2014-10-31) 
Platform: x86_64-pc-linux-gnu (64-bit) 

locale: 
[1] LC_CTYPE=en_US.UTF-8  LC_NUMERIC=C    LC_TIME=en_US.UTF-8  LC_COLLATE=en_US.UTF-8  
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 LC_PAPER=en_US.UTF-8  LC_NAME=C     
[9] LC_ADDRESS=C    LC_TELEPHONE=C    LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C  

attached base packages: 
[1] stats  graphics grDevices utils  datasets methods base  

other attached packages: 
[1] setwidth_1.0-3 colorout_1.0-1 

loaded via a namespace (and not attached): 
[1] tools_3.1.2 
Problemi correlati