R sink(): Error, cannot split the message connection - r

I am trying to log the errors and warnings of an R script into an external file. At the same time I want to be able to see the errors and warnings in the console in RStudio (useful to develop and debug). I am trying to use the following code:
logfile <- file("my_file_path", open="wt")
sink(logfile, type="message", split = TRUE)
But when I try to split the message connection using the funciton sink() I get the following error:
Error in sink(logfile, type = "message", split = TRUE) :
cannot split the message connection
Is there any workaround or alternative solution?
Thanks

So, I tried using split = T in sink.
But, it's not doing what we want it to do. It's either redirecting output to log file or throwing an error which you pointed and is not printing errors or warning messages to RStudio console.
There's a work around to your problem which might solve your problem.
I tried using this:-
# path to your log file
file_path <- "path/documents/log/log.txt"
# open a connection to your log file
file_con <- file(file_path, open = "a")
## capture warning messages and errors to log file
sink(file_con, type = "message")
## to get error and warning message
sum(a)
warning("this is a warning message. please ignore")
## revert output back to the console and close the file connection
sink(type = "message")
close(file_con)
# get all the errors and warnings from log file
readLines(file_path)
It gave an output to console:-
[1] "Error: object 'a' not found"
[2] "Warning message:"
[3] "this is a warning message. please ignore "
So, the above piece of code diverted the error and warning message to log file and printed it in console too.
You can use sink normally and then use readLines to print your error and warning messages to console.

Related

Using tryCatchLog with futile.logger: write to file instead of console

I am using tryCatchLog and want to send all warnings and error messages to a log file. I do not want any output to the console.
In the documentation for tryCatchLog I came across this code snippet:
library(futile.logger)
# log to a file (not the console which is the default target of futile.logger).
# You could also redirect console output into a file if start your R > script with a shell script using Rscript!
flog.appender(appender.file("my_app.log"))
The vignette for tryCatchLog includes the following code snippet to change the logging behavior and send errors to a file instead of the console:
library(futile.logger)
flog.appender(appender.file("app.log"))
flog.threshold(ERROR) # TRACE, DEBUG, INFO, WARN, ERROR, FATAL
try(log(-1)) # the warning will not be logged!
This suggests to me that I can simply re-direct the messages to a log file using flog.apppender(appender.file()). However, instead of writing to a file I get the following output on the console:
NULL
NULL
Warning in log(-1) : NaNs produced
[1] NaN
The vignette for tryCatchLog provides this code example in the Best Practice section:
library(futile.logger)
library(tryCatchLog)
options(keep.source = TRUE) # source code file name and line number tracking
options("tryCatchLog.write.error.dump.file" = TRUE) # dump for post-mortem analysis
flog.appender(appender.file("my_app.log")) # to log into a file instead of console
flog.threshold(INFO) # TRACE, DEBUG, INFO, WARN, ERROR, FATAL
tryCatchLog(source("your_main_script.R"))
Adapting the last line of code to tryCatchLog(log("this will produce an error")) to make the example easier, I am still not writing to a log file but to the console:
NULL
NULL
Error in log("this will produce an error") : non-numeric argument to mathematical function
Looking at the documentation of futile.logger and examples on stackoverflow also did not help me. Based on them, I thought the following should write the error message to a file. I am using try() as a stand-in for more involved versions of tryCatchLog() to make sure that it is not an issue with tryCatchLog.
library(futile.logger)
flog.appender(appender.file(file.path(getwd(),'logs.txt')))
flog.threshold(WARN) # TRACE, DEBUG, INFO, WARN, ERROR, FATAL
try(log(-1)) # this will create a warning
try(log("this will create an error")) # this will create an error
The command neither creates a log file nor appends an existing one.
Instead, flog.appender() and flog.threshold() return NULL (to the console). And the warning and error message are also printed to the console. Presumably, I am missing something when linking the logger to the file (hence the NULL return value?).
How can I redirect all warnings and errors caught by tryCatchLog to a file (with futile.logger) without ANY output to the console?
The output of try() has nothing to do with futile.logger().
If you configure a file appender in futile.logger you still have to write your log output using the flog.* functions (that's how logging works):
library(futile.logger)
flog.info("this is an info log output")
# INFO [2021-12-08 19:51:13] this is an info log output
flog.warn("this is an warning log output")
# WARN [2021-12-08 19:51:14] this is an warning log output
flog.error("this is an error log output")
# ERROR [2021-12-08 19:51:14] this is an error log output
I guess what you want is kinda output redirection of the standard or error output (eg. via capture.output) but this function does not add logging information like a timestamp and does not support severity level management ("info", "warn", "error"...).
If you want to use try*() in combination with logging you can use the CRAN package tryCatchLog which does exactly what you want (and supports futile.logger too):
library(tryCatchLog)
tryLog(log(-1)) # this will create a warning
tryLog(log("this will create an error")) # this will create an error

RStudio not reading data frame and getting the connection stuck?

I'm starting with R and I'm trying to load a data frame that I have on my PC.
It's a txt, tab delimited file. I already tested the file by copying and pasting it in Excel, so I know it's properly formatted.
However, when using
myData <- read.table("mydata.txt", header = TRUE, sep = "\t")
myData
All I get is an error message saying
Error: object 'myData' not found
But mydata.txt is located in the same folder where the R file is located at.
Also, notice it's not saying "file not found", if I change the file name to some gibberish like "dsfkad.txt", it will throw an error saying no such file with that name was found.
Furthermore, it seems that the connection with the file gets stuck after you run the command once. If I try to run the command again, all I get in the console is
>
No error output or anything, just the angled bracket.
If I do a change in the code and run it again, it then shows the error
Error in file(file, "rt") : cannot open connection
So it seems like RStudio is not actually closing the file connection after trying to read it and its throwing errors when anything changes, which seems like very abnormal behavior for an IDE.

Reading CSV from URL previously worked, now returning error

I have a program that I intended to rerun regularly with minimal code-manipulation. The code below previously ran succcessfully, but it stopped working. I thought, perhaps the server was down, but when I pasted the URL on my browser, it initiated a download of the csv file. So I think I'm missing something...
nyc_temp_data <- read.csv("https://www.ncei.noaa.gov/access/services/data/v1?dataset=daily-summaries
&dataTypes=TMAX&stations=USW00094728&startDate=2014-01-01&endDate=2020-05-01&includeAttributes=true&units=standard&format=csv")
When I ran it today, I get the following error:
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open URL 'https://www.ncei.noaa.gov/access/services/data/v1?dataset=daily-summaries
&dataTypes=TMAX&stations=USW00094728&startDate=2014-01-01&endDate=2020-05-01&includeAttributes=true&units=standard&format=csv': HTTP status was '400 '
Sub-optimal solution but gets the job done:
# Store the url: csv_url => character vector
csv_url <- "https://www.ncei.noaa.gov/access/services/data/v1?dataset=daily-summaries
&dataTypes=TMAX&stations=USW00094728&startDate=2014-01-01&endDate=2020-05-01&includeAttributes=true&units=standard&format=csv"
# Store the full filepath to the desired output location / filename:
# output_fpath => character vector
output_fpath <- paste0(getwd(), "/nyc_temp_data.csv")
# Download the url and store it at the filepath: nyc_temp_data.csv => stdout
download.file(csv_url, output_fpath)
# Read in the csv from the file path. nyc_temp_data => data.frame
nyc_temp_data <- read.csv(output_fpath)

Why there is database connection issue in RNCEP package

I am trying to use "RNCEP" package in R studio. I ran following code
install.packages("RNCEP", dependencies=TRUE)
library(RNCEP)
wx.extent <- NCEP.gather(variable= 'air', level=850, months.minmax=c(8,9),
years.minmax=c(2006,2007), lat.southnorth=c(50,55), lon.westeast=c(0,5),
reanalysis2 = FALSE, return.units = TRUE)
I got error messages as:
trying URL
'http://www.esrl.noaa.gov/psd/thredds/dodsC/Datasets/ncep.reanalysis/pressure/air.2006.nc.das'
Content length 660 bytes
Error in NCEP.gather.pressure(variable = variable, months.minmax =
months.minmax, :
There is a problem connecting to the NCEP database with the
information provided.
Try entering
http://www.esrl.noaa.gov/psd/thredds/dodsC/Datasets/ncep.reanalysis/pressure/air.2006.nc.das
into a web browser to obtain an error message.
In addition: Warning messages:
1: In
download.file(paste("http://www.esrl.noaa.gov/psd/thredds/dodsC/Datasets/ncep.reanalysis",
: cannot open URL
'http://www.cfauth.com/?cfru=aHR0cDovL3d3dy5lc3JsLm5vYWEuZ292L3BzZC90aHJlZGRzL2RvZHNDL0RhdGFzZXRzL25jZXAucmVhbmFseXNpcy9wcmVzc3VyZS9haXIuMjAwNi5uYy5kYXM=':
HTTP status was '401 Unauthorized'
Please suggest me the correct syntax to download NCEP data.
Thanks
Sam

How to test standalone R scripts which are part of a package using testthat

I am building a R package, RE and I would like to package a standalone script with it to be placed in RE/exec/wrapper.R. I have written a test function for testthat which works just fine when run from the command line using devtools::test("RE"), but fails when run using devtools::check("RE").
The relevant code portion that fails is:
cmd <- "Rscript \"C:\\RE\\exec\\wrapper.R\" base print \"{\\\"x\\\": 2}\""
rv <- system(cmd, intern = FALSE, ignore.stderr = FALSE, show.output.on.console = FALSE)
When run as part of "test" everything runs fine and system returns 0 as it should. When run as part of "check", system gives out the error message given below and returns 1.
Error in file(filename, "r", encoding = encoding) : cannot open the connection
Calls: local ... eval.parent -> eval -> eval -> eval -> eval -> source -> file
In addition: Warning message:
In file(filename, "r", encoding = encoding) : cannot open file 'startup.Rs': No such file or directory
Execution halted
I have:
Checked that the file exists (it does in both cases)
Tried tryCatch. No error are caught, and system just returns a 1
This makes me think that it is an issue with system and R CMD CHECK.
The answer is in https://github.com/r-lib/testthat/issues/144 . The environment variable R_TESTS must be unset via Sys.setenv("R_TESTS" = "") in the top level test script (package_root/tests/testthat.R), or system will not run correctly when called from R CMD CHECK. I am leaving the question up, because it took me several hours of experimenting and searching before stumbling on the answer.

Resources