First argument is not an open RODBC channel - r

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...

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

Error in parse_aws_s3_response, Forbidden (http 403)

I a
I'm trying to use aws.s3 library to load data into S3. Below is my code below:
Sys.setenv("AWS_ACCESS_KEY_ID" = ".............",
"AWS_SECRET_ACCESS_KEY" = "...............",
"AWS_DEFAULT_REGION" = "......")
view bucket:
get_bucket(bucket = "....",
check_region = FALSE,
verbose = TRUE)
The code above works and contents of the bucket is returned.
save object to s3:
s3save(data,
object = ".....",
bucket = ".....")
Error return:
List of 4
$ Code : chr "AccessDenied"
$ Message : chr "Access Denied"
$ RequestId: chr ""
$ HostId : chr ""
- attr(*, "headers")=List of 6
..$ x-amz-request-id : chr ""
..$ x-amz-id-2 : chr ""
..$ content-type : chr "application/xml"
..$ transfer-encoding: chr "chunked"
..$ date : chr "Tue, 05 Sep 2017 13:23:42 GMT"
..$ server : chr "AmazonS3"
..- attr(*, "class")= chr [1:2] "insensitive" "list"
- attr(*, "class")= chr "aws_error"
- attr(*, "request_canonical")= chr "GET\n/"| __truncated__
- attr(*, "request_string_to_sign")= car ""| __truncated__
- attr(*, "request_signature")= chr "AWS4-HMAC-SHA256 Credential=|
__truncated__
NULL
Error in parse_aws_s3_response(r, Sig, verbose = verbose) :
Forbidden (HTTP 403).
Any solution to this problem.
It seems like your IAM account is not authorized to do that operation.
If you're the owner of the AWS account, you can go to AWS Service IAM, click on users, choose this IAM user and add the necessary authorizations to it.
I got the same error message when attempting
get_bucket("collidr-api")
All I needed to do was add the region name like so
get_bucket("collidr-api", region="asia-south-01")
Then it worked. I found that info here.
#audiracmichelle, #Prakhar, use the function put_object from aws.s3 library as shown below. The object input argument to function put_object is the name(string) the object should have in s3.
You can first write a serialized object (in this case data) from memory with:
in_mem_obj <- rawConnection(raw(0), "w")
utils::write.table(data, in_mem_obj, sep = "\t", na = "", quote = TRUE,
row.names = FALSE, fileEncoding="UTF-8")
Then save object to S3 bucket:
put_object(rawConnectionValue(in_mem_obj),
object = FILEPATH/FILENAME,
bucket = BUCKET,
headers = c('x-amz-server-side-encryption' = 'AES256'),
key = AWS_ACCESS_KEY_ID,
secret = AWS_SECRET_ACCESS_KEY,
region = AWS_REGION,
check_region = FALSE)

How to reference .. .. elements in list in 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"

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