How to reference .. .. elements in list in R? - r

How do you access list elements prefaced with double dots .. ..?
I am trying to isolate the $attr part of the html to help in another question into a variable, and then place it another mime variable. x$parts$attr gives me NULL.
Thanks for your help.
EXAMPLE
library(gmailr)
test_email <- mime()
x <- html_body(mime = test_email, body = "I wish I had a nice body")
str(x)
List of 5
$ parts :List of 2
..$ : NULL
..$ :List of 4
.. ..$ parts : list()
.. ..$ header:List of 2
.. .. ..$ MIME-Version: chr "1.0"
.. .. ..$ Date : chr "Sun, 24 May 2015 09:44:59 GMT"
.. ..$ body : chr "I wish I had a nice body"
.. ..$ attr :List of 3 # << HOW do I get this?
.. .. ..$ content_type: chr "text/html"
.. .. ..$ charset : chr "utf-8"
.. .. ..$ encoding : chr "base64"
.. ..- attr(*, "class")= chr "mime"

Try this:
str(x$parts[[2]]$attr)
List of 3
$ content_type: chr "text/html"
$ charset : chr "utf-8"
$ encoding : chr "base64"

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.

unable to create a cluster/plan to a remote server in R

Trying (still) to get a cluster running. I have two Linux (Ubuntu) computers, the server is "laptop". Pretty sure ssh works, with key authentication, I can ssh to "laptop" without a password from command line. ssh_exec_wait works, e.g.,
session <- ssh_connect("laptop")
a <- ssh_exec_wait(session, command = "whoami")
ssh_disconnect(session)
works. I've gone through the doc (A future for R), works fine until I get to cluster futures.
plan(cluster, workers = c("localhost"))
works fine, but
plan(cluster, workers = c("laptop"))
gives the error:
Warning: remote port forwarding failed for listen port 11968
List of 2
$ node_idx: int 1
$ node :List of 5
..$ con : 'sockconn' int 3
.. ..- attr(*, "conn_id")=<externalptr>
..$ host : chr "laptop"
.. ..- attr(*, "localhost")= logi FALSE
..$ rank : int 1
..$ rshlogfile : NULL
..$ session_info:List of 6
.. ..$ r :List of 15
.. .. ..$ platform : chr "x86_64-pc-linux-gnu"
.. .. ..$ arch : chr "x86_64"
.. .. ..$ os : chr "linux-gnu"
.. .. ..$ system : chr "x86_64, linux-gnu"
.. .. ..$ status : chr ""
.. .. ..$ major : chr "3"
.. .. ..$ minor : chr "6.3"
.. .. ..$ year : chr "2020"
.. .. ..$ month : chr "02"
.. .. ..$ day : chr "29"
.. .. ..$ svn rev : chr "77875"
.. .. ..$ language : chr "R"
.. .. ..$ version.string: chr "R version 3.6.3 (2020-02-29)"
.. .. ..$ nickname : chr "Holding the Windsock"
.. .. ..$ os.type : chr "unix"
.. ..$ system :List of 8
.. .. ..$ sysname : chr "Linux"
.. .. ..$ release : chr "5.3.0-46-generic"
.. .. ..$ version : chr "#38~18.04.1-Ubuntu SMP Tue Mar 31 04:17:56 UTC 2020"
.. .. ..$ nodename : chr "laptop"
.. .. ..$ machine : chr "x86_64"
.. .. ..$ login : chr "mark"
.. .. ..$ user : chr "mark"
.. .. ..$ effective_user: chr "mark"
.. ..$ libs : chr [1:4] "/home/mark/R/x86_64-pc-linux-gnu-library/3.6" "/usr/local/lib/R/site-library" "/usr/lib/R/site-library" "/usr/lib/R/library"
.. ..$ pkgs : NULL
.. ..$ pwd : chr "/home/mark"
.. ..$ process:List of 1
.. .. ..$ pid: int 2809
..- attr(*, "class")= chr "SOCKnode"
Error: Initialization of plan() failed, because the test future used for validation failed. The reason was: Unexpected result (of class ‘NULL’ != ‘FutureResult’) retrieved for ClusterFuture future (label = ‘future-plan-test’, expression = ‘NA’): . This suggests that the communication with ClusterFuture worker (‘SOCKnode’ #1) is out of sync.
I assume the first line is the real error message:
Warning: remote port forwarding failed for listen port 11968
Do I need to manually set the port?
In my experience, the warning
Warning: remote port forwarding failed for listen port 11968
is caused by a hanging R process on that port (it could be another process of course but it's likely that the future package created it). If you kill the process on laptop, the port should become available again.
You can check by manually running
ssh -R 11968:localhost:11968 laptop

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