Can't get "try-error" class in R - r

I need to check my connection to database, is it expired or not:
when i try to execute query, i get such error
> dbGetQuery(con,"select * from users limit 1")
Error in postgresqlExecStatement(conn, statement, ...) :
RS-DBI driver: (could not run statement: could not receive data from server: Software caused connection abort (0x00002745/10053)
)
NULL
Warning message:
In postgresqlQuickSQL(conn, statement, ...) :
Could not create executeselect * from users limit 1
Okay, i need something like "if error then print("connection expired")".
I tried to solve this problem so:
> class(try(dbGetQuery(con,"select * from users limit 1"), silent = T))
Error in postgresqlExecStatement(conn, statement, ...) :
RS-DBI driver: (could not run statement: could not receive data from server: Software caused connection abort (0x00002745/10053)
)
[1] "NULL"
Warning message:
In postgresqlQuickSQL(conn, statement, ...) :
Could not create executeselect * from users limit 1
And got this error and warning again. And class of that try expressions is "NULL", not "try-error".
But when i do this:
> class(try(log("hi"), silent = T))
[1] "try-error"
I've got no errors and try-error class
Why?
UPD.
Solved this problem via
>options(warn = 2, show.error.messages = F)
And then
class(try(dbGetQuery(con,"select * from users limit 1"), silent = T))
[1] "try-error"

Related

Trying to connect to DB in R but fails with error "Error in if (timezone == "") { : argument is of length zero"

Code:
do.call(
DBI::dbConnect,
custom(
args
)
)
I have tried to set timezone like dbConnect(timezone = "UTC") but still fails.
Connection to the DB

dbutils.fs.mount() works for Python but not for R?

The following code works in a Azure Databricks Python cell:
dbutils.fs.mount(
source = "wasbs://my-container#mystorageaccount.blob.core.windows.net",
mount_point = "/mnt/mount1",
extra_configs = {"fs.azure.account.key.mystorageaccount.blob.core.windows.net":dbutils.secrets.get(scope = "my-scope", key = "storagekey")})
However, if I try to run this code from an Azure Databricks R cell I get an error, can somebody explain what's going on?
dbutils.fs.mount(
source = 'wasbs://my-container#mystorageaccount.blob.core.windows.net',
mountPoint = '/mnt/mount1',
extraConfigs = {'fs.azure.account.key.mystorageaccount.blob.core.windows.net':dbutils.secrets.get(scope = 'my-scope', key = 'storagekey')}
)
Warning in as.list(extraConfigs) : NAs introduced by coercion Warning in as.list(extraConfigs) : NAs introduced by coercion Error in
"fs.azure.account.key.mystorageaccount.blob.core.windows.net":dbutils.secrets.get(scope
= "my-scope", : NA/NaN argument Some( Error in
"fs.azure.account.key.mystorageaccount.blob.core.windows.net":dbutils.secrets.get(scope
= "my-scope", : NA/NaN argument ) Error in "fs.azure.account.key.mystorageaccount.blob.core.windows.net":dbutils.secrets.get(scope
= "my-scope", : NA/NaN argument
The problem is that you're trying to use Python syntax for extraConfigs parameter, but it's incorrect. In R you need to use following syntax for dicts: c(key1="value1", key2="value2")

How to attach two files when sending message to SMTP Server (emayili+Outlook)

I have been setting the emayili package to send email with two attached files from my Outlook account, but so far I have been unable to do it.
The code is as following (with emails and passwords replaced with 'aaa' and 'bbb' for privacy/security):
email <- envelope(
from = "aaa#domain.com",
to = "bbb#domain.com",
subject = subject,
html = body) %>%
attachment('filename1.xlsx') %>%
attachment('filename2.xlsx')
smtp <- server(host = "smtp.mailtrap.io",
port = 587,
username = "********",
password = "*********")
smtp(email, verbose = TRUE)
When I run the code, I get the following result:
> smtp(email, verbose = TRUE)
Sending email to smtp.office365.com:587/.
Error: Request failed after 5 attempts
Run `rlang::last_error()` to see where the error occurred.
In addition: Warning messages:
1: In smtp(email, verbose = TUE) :
​ restarting interrupted promise evaluation
2: In f(...) : restarting interrupted promise evaluation
3: In f(...) : restarting interrupted promise evaluation
4: In f(...) : restarting interrupted promise evaluation
5: In f(...) : restarting interrupted promise evaluation
Any idea what is going wrong?
Also try to use library(Microsoft365R)
outl <- get_personal_outlook()
#your code
# using emayili to create an email with attachments
ey_email <- emayili::envelope(
text="Hello from emayili",
to="user#example.com",
subject="example emayili email") %>%
emayili::attachment("mydocument.docx") %>%
emayili::attachment("mydata.xlsx")
outl$create_email(ey_email)
#your code
Full manual you can find there

R Url function on Mac

When running the following call on Mac I get an error as follows
jsonlite::fromJSON(url(paste("https://fantasy.premierleague.com/api/element-summary/236")))
Error in parse_con(txt, bigint_as_char) : parse error: premature EOF
(right here) ------^
However the following call works correctly, with the only difference being the omission of the url().
jsonlite::fromJSON(paste("https://fantasy.premierleague.com/api/element-summary/236"))
The traceback from the error is as follows:
4: parse_con(txt, bigint_as_char)
3: parseJSON(txt, bigint_as_char)
2: parse_and_simplify(txt = txt, simplifyVector = simplifyVector,
simplifyDataFrame = simplifyDataFrame, simplifyMatrix = simplifyMatrix,
flatten = flatten, ...)
1: jsonlite::fromJSON(url(paste("https://fantasy.premierleague.com/api/element-summary/236")))
I've tried on two separate installations with the same result. I wonder if the line break after the first line of the JSON is being interpreted as EOF....

overriding R's incomplete error messages

When a call exists of multiple lines, a potential error only includes the first line of match.call() resulting in some lost information and an incomplete sentence. A simple example:
#proper error message:
runif(n=1, k=5)
#incomplete error message:
runif(n=1, k={5})
What would be a way to get R to include the full call to the error message (maybe by collapsing the multiple lines or so)? I am mostly interested in using this in a tryCatch setting.
I had a go at investigating the error object in a tryCatch setting via:
tryCatch( runif(n=1,k={5}),
error = function(e) recover() )
And then selected the 4th environment (value[[3]](cond)) to examine e.
I noticed that e$call was:
Browse[1]> e$call
runif(n = 1, k = {
5
})
So it seems that the error message just uses that first line.
You can collapse all the lines together with:
Browse[1]> paste(deparse(e$call),collapse='')
[1] "runif(n = 1, k = { 5})"
So you could try something like:
tryCatch( runif(n=1,k={5}),
error = function(e) {
cat(sprintf('Error in %s: %s\n',
paste(deparse(e$call),collapse=''),
e$message))
} )
But this doesn't fix up the error message itself, just the call leading up to it:
Error in runif(n = 1, k = { 5}): unused argument(s) (k = {
So the 'Error in xxx' is complete, but the 'unused argument(s) xxx' is still not. It's a start, but not all the way there.
I'm not sure how to improve on this (and am also interested to know if it's possible).

Resources