What does "hidden list" in the output of `str()` mean? - r

In attempting to Answer this Question I came across this in the output of str()
## R reference
rref <- bibentry(bibtype = "Manual",
title = "R: A Language and Environment for Statistical Computing",
author = person("R Development Core Team"),
organization = "R Foundation for Statistical Computing",
address = "Vienna, Austria",
year = 2010,
isbn = "3-900051-07-0",
url = "http://www.R-project.org/")
> str(rref)
Class 'bibentry' hidden list of 1
$ :List of 7
..$ title : chr "R: A Language and Environment for Statistical Computing"
..$ author :Class 'person' hidden list of 1
.. ..$ :List of 5
.. .. ..$ given : chr "R Development Core Team"
.. .. ..$ family : NULL
.. .. ..$ role : NULL
.. .. ..$ email : NULL
.. .. ..$ comment: NULL
..$ organization: chr "R Foundation for Statistical Computing"
..$ address : chr "Vienna, Austria"
..$ year : chr "2010"
..$ isbn : chr "3-900051-07-0"
..$ url : chr "http://www.R-project.org/"
..- attr(*, "bibtype")= chr "Manual"
In particular, I'm puzzled by this bit:
> str(rref)
Class 'bibentry' hidden list of 1
$ :List of 7
What does the "hidden list" bit refer to? What kind of object is this? Is this just some formatting output from str() when there is only a single component in the object that is itself a list? If so how is there a way to force str() to show the full structure?

This seems like an artefact of str. My interpretation is that the words hidden list are printed in the output of str if the obect is not a pairlist.
Since your object is of class bibtex, and there is no str method for bibtex, the method utils:::str.default is used to describe the structure.
Condensed extract from str.default:
...
if (is.list(object)) {
i.pl <- is.pairlist(object)
...
cat(if (i.pl)
"Dotted pair list"
else if (irregCl)
paste(pClass(cl), "hidden list")
else "List", " of ", le, "\n", sep = "")
...
}
The key bit that defines irregCl is:
....
else {
if (irregCl <- has.class && identical(object[[1L]],
object)) {
....
and that explain the hidden list bit - it hides the outer list if the object has a class and object and object[[1]] are identical. As you showed in the Answer you linked to, the [[ method returns an identical object if the list contains a single "bibentry" object.

Related

R adding slots to existing S4 object

I am trying to follow this answer but getting lost with what to change for my example. Please can someone spell out specifically for the below?
I have the following S4 object called "classic":
> str(classic)
Formal class 'topGOresult' [package "topGO"] with 5 slots
..# description: chr "Custom Afol today"
..# score : Named num [1:7633] 0.5735 0.0883 0.8081 0.8005 0.4941 ...
.. ..- attr(*, "names")= chr [1:7633] "GO:0000002" "GO:0000003" "GO:0000018" "GO:0000027" ...
..# testName : chr "fisher"
..# algorithm : chr "classic"
.. ..- attr(*, "testClass")= chr "classicCount"
..# geneData : Named int [1:4] 9171 620 5 5361
.. ..- attr(*, "names")= chr [1:4] "Annotated" "Significant" "NodeSize" "SigTerms"
I am able to edit contents of existing slots, such as:
classic#description = "Custom Afol today"
However, I would like to add a new slot called "ontology" with character input of "BP". Is there a way to add a new slot to an existing object such as this?
Many thanks in advance for help.

understanding the output of str function in R

I use the str to print out information for a calculate variable
str(samples)
'mcmc' num [1:1000, 1:228] 0.1079 -0.2367 -0.0757 -0.3414 -0.3382 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:228] "B[1,1,1]" "B[2,1,1]" "B[3,1,1]" "B[4,1,1]" ...
- attr(*, "mcpar")= num [1:3] 20005 25000 5
But how to read these information, or what does this output can tell us? For instance, the third component is attr(*, "mcpar")= num [1:3] 20005 25000 5, what does it mean?
The attr implies there is an attribute named 'mcpar' which is a numeric vector
attributes(samples)
returns a list of all the attributes of the samples i.e. it will return dimnames as one attribute and the 'mcpar' as another one
According to documentation
The ‘mcpar’ attribute of an MCMC object gives the start iteration the end iteration and the thinning interval of the chain.

First argument is not an open RODBC channel

I am connecting to an Oracle database via DNS (set up the connection on the tnsnames.ora with the name "database").
I am able to succesfully run the following code:
con <- DBI::dbConnect(odbc::odbc(),
"database",
UID = "user",
PWD = "password",
trusted_connection = TRUE)
I am also able to succesfully list all the tables in the database via:
list <- dbListTables(con)
View(list)
However, when I run:
results <- sqlQuery(channel = con, query = "select * from myschemaname.table")
I get the error:
Error in sqlQuery(channel = con, query = "select * from myschemaname.table") :
first argument is not an open RODBC channel
I have owner privileges and I am also able to update Excel Spreadsheets connected to this database via ODBC.
If it's of any use, here's the output of str(con)
str(con)
Formal class 'Oracle' [package ".GlobalEnv"] with 4 slots
..# ptr :<externalptr>
..# quote : chr "\""
..# info :List of 13
.. ..$ dbname : chr ""
.. ..$ dbms.name : chr "Oracle"
.. ..$ db.version : chr "11.02.0040"
.. ..$ username : chr "user"
.. ..$ host : chr ""
.. ..$ port : chr ""
.. ..$ sourcename : chr "database"
.. ..$ servername : chr "database"
.. ..$ drivername : chr "SQORA32.DLL"
.. ..$ odbc.version : chr "03.80.0000"
.. ..$ driver.version : chr "11.02.0001"
.. ..$ odbcdriver.version : chr "03.52"
.. ..$ supports.transactions: logi TRUE
.. ..- attr(*, "class")= chr [1:3] "Oracle" "driver_info" "list"
..# encoding: chr ""
You are using RODBC::sqlQuery() for a connection created with DBI::dbConnect(). Either use DBI::dbGetQuery() with DBI::dbConnect() or create a connection with RODBC::odbcConnect(), and use RODBC::sqlQuery().
Another context, but the same error:
If the table of your DB consists of numerics, you first of all have to load an object (nameofyourtable) of class xts:
>library(RODBC)
>library(DMwR)
>library(xts)
>data(nameofyourtable)
So I had something like this connecting to a SQL Server database. In the end I had to change the Trusted_Connection=TRUE to Trusted_Connection=Yes...

Conditionally selecting sublists from a large JSON generated list

I have a list generated from an API pull and converted from JSON by the httr package.
It has 80-odd top level numbered elements / lists, each of which contains between 10 next-level elements / lists. At this next level I'd like to be able to filter so I only have the 1 from the 10 that has the name "My Color" , I don't want the other 9 that don't match.
Here is the output in R for the 2nd level, (1 of 10) list:
..$ data:List of 10
.. ..$ :List of 3
.. .. ..$ id : int 1
.. .. ..$ name : chr "My Color"
.. .. ..$ color:List of 1
.. .. .. ..$ data:List of 1
.. .. .. .. ..$ :List of 3
.. .. .. .. .. ..$ id : int 15
.. .. .. .. .. ..$ name: chr "Blue"
I'm struggling to get a reproducible data example due to the complexity of the list, but the end goal would be to preserve the 80 top level elements, but with each containing the 1 sublist whose 'name' element contains 'My Color'.
Again apologies for the lack of a reproducible example, but a pointer to the correct approach would be appreciated. I'm really struggling with manipulating lists.

R understanding and access a nested object by reading from str

Hi I have a nested object which I think contains tables. I used str to see how it looks like,
> str(test)
Formal class 'CuffData' [package "cummeRbund"] with 5 slots
..# DB :Formal class 'SQLiteConnection' [package "RSQLite"] with 5 slots
.. .. ..# Id :<externalptr>
.. .. ..# dbname : chr "C:/temp/cuffData.db"
.. .. ..# loadable.extensions: logi TRUE
.. .. ..# flags : int 6
.. .. ..# vfs : chr ""
..# tables :List of 6
.. ..$ mainTable : chr "genes"
.. ..$ dataTable : chr "geneData"
.. ..$ expDiffTable : chr "geneExpDiffData"
.. ..$ featureTable : chr "geneFeatures"
.. ..$ countTable : chr "geneCount"
.. ..$ replicateTable: chr "geneReplicateData"
..# filters: list()
..# type : chr "genes"
..# idField: chr "gene_id"
however when I tried to to read the table mainTable all I get is this,
> test#tables$mainTable
[1] "genes"
is there any way I can read what is in this table?
Not sure if I'm even interpreting the str output correctly though. I'm a newbie at this and I'm also wondering if there is a good tutorial on how to interpret objects like these in R? Examples would be even better.
thanks you much in advance.
Ahdee

Resources