Write in sharepoint List from R - r

I am trying to write inside a sharepoint list from my R script but I keep getting this error :
Error: node stack overflow
Error during wrapup: node stack overflow
I am using the package sharepointr that I found in this github repository : https://github.com/LukasK13/sharepointr
I used the sp_WriteListData but it doesn't seem to work. I tried to execute the function step by step with its code, which can be found here : https://rdrr.io/github/LukasK13/sharepointr/src/R/sharepoint_list_post.R
The function always stops at the last step when I execute this line of code :
sp_request(con, request = request, verb = "POST", body = data).
Does anyone know how to get rid of this error ?
Thank you very much !

Related

Having trouble using rAltmetric in R

Sorry new to stack overflow - but went back to some code using rAltmetric in R (used in in May), but now appears to not be working, have gone back and used some DOI and ISBN from previous comments on rAltmetric to illustrate new error I am receiving (tried also getting package from guthub) - I am wondering if they have closed the window to get into altmetrics
library(rAltmetric)
altmetrics(isbn = "978-3-319-25557-6")
Error: lexical error: invalid char in json text.
The API key you supplied was no
(right here) ------^
In addition: Warning message:
In altmetrics(isbn = "978-3-319-25557-6") : Unauthorized (HTTP 401).
altmetrics(doi = "10.1038/465860a")
Error: lexical error: invalid char in json text.
The API key you supplied was no
(right here) ------^
In addition: Warning message:
In altmetrics(doi = "10.1038/465860a") : Unauthorized (HTTP 401).
Have others recently started having a problem with rAltmetric
Any help welcome.
MalcB
It doesn't appear to be an API key problem. Something is up with their query function.
I created a simplified version of the function that works, here: https://gist.github.com/mathzero/f1d1b3b94f6f76019a5861d1bed7d597
... but haven't had time to identify exactly what the issue is.
In the mean time I suggest raising a query on their github.

Error in gmax(<my_variable>) : object 'my_variable' not found

I'm running the following code, and I get the error mentioned in the title when I run it.
dt.placements[,.(first.received = min(file.dt)
,last.received = max(file.dt)
,SUB_ACCT_NO_SBB=max(SUB_ACCT_NO_SBB)
)
,by=.(SUB_ACCT_NO_SBB,EQP_SERIAL_EQP)]
Can someone please explain to me why I am getting this message and how to fix my code. This code was working for me before I added the 3rd line--I added it to try to take out duplicates in the results.

R: Catch errors and continue execution after logging the stacktrace (no traceback available with tryCatch)

I have many unattended batch jobs in R running on a server and I have to analyse job failures after they have run.
I am trying to catch errors to log them and recover from the error gracefully but I am not able to get a stack trace (traceback) to log the code file name and line number of the R command that caused the error. A (stupid) reproducible example:
f <- function() {
1 + variable.not.found # stupid error
}
tryCatch( f(), error=function(e) {
# Here I would log the error message and stack trace (traceback)
print(e) # error message is no problem
traceback() # stack trace does NOT work
# Here I would handle the error and recover...
})
Running the code above produces this output:
simpleError in f(): object 'variable.not.found' not found
No traceback available
The traceback is not available and the reason is documented in the R help (?traceback):
Errors which are caught via try or tryCatch do not generate a
traceback, so what is printed is the call sequence for the last
uncaught error, and not necessarily for the last error.
In other words: Catching an error with tryCatch does kill the stack trace!
How can I
handle errors and
log the stack trace (traceback) for further examination
[optionally] without using undocumented or hidden R internal functions that are not guaranteed to work in the future?
THX a lot!
Sorry for the long answer but I wanted to summarize all knowledge and references in one answer!
Main issues to be solved
tryCatch "unrolls" the call stack to the tryCatch call so that traceback and sys.calls do no longer contain the full stack trace to identify the source code line that causes an error or warning.
tryCatch aborts the execution if you catch a warning by passing a handler function for the warning condition. If you just want to log a warning you cannot continue the execution as normal.
dump.frames writes the evaluation environments (frames) of the stack trace to allow post-mortem debugging (= examining the variable values visible within each function call) but dump.frames "forgets" to save the workspace too if you set the parameter to.file = TRUE. Therefore important objects may be missing.
Find a simple logging framework since R does not support decent logging out of the box
Enrich the stack trace with the source code lines.
Solution concept
Use withCallingHandlers instead of tryCatch to get a full stack trace pointing to the source code line that throwed an error or warning.
Catch warnings only within withCallingHandlers (not in tryCatch) since it just calls the handler functions but does not change the program flow.
Surround withCallingHandlers with tryCatch to catch and handle errors as wanted.
Use dump.frames with the parameter to.file = FALSE to write the dump into global variable named last.dump and save it into a file together with the global environment by calling save.image.
Use a logging framework, e. g. the package futile.logger.
R does track source code references when you set options(keep.source = TRUE). You can add this option to your .Rprofile file or use a startup R script that sets this option and source your actual R script then.
To enrich the stack trace with the tracked source code lines you can use the undocumented (but widely used) function limitedLabels.
To filter out R internal function calls from stack trace you can remove all calls that have no source code line reference.
Implementation
Code template
Instead of using tryCatch you should use this code snippet:
library(futile.logger)
tryCatch(
withCallingHandlers(<expression>,
error = function(e) {
call.stack <- sys.calls() # is like a traceback within "withCallingHandlers"
dump.frames()
save.image(file = "last.dump.rda")
flog.error(paste(e$message, limitedLabels(call.stack), sep = "\n"))
}
warning = <similar to error above>
}
error = <catch errors and recover as you would do it normally>
# warning = <...> # never do this here since it stops the normal execution like an error!
finally = <your clean-up code goes here>
}
Reusable implementation via a package (tryCatchLog)
I have implemented a simple package with all the concepts mentioned above.
It provides a function tryCatchLog using the futile.logger package.
Usage:
library(tryCatchLog) # or source("R/tryCatchLog.R")
tryCatchLog(<expression>,
error = function(e) {
<your error handler>
})
You can find the free source code at github:
https://github.com/aryoda/tryCatchLog
You could also source the tryCatchLog function instead of using a full blown package.
Example (demo)
See the demo file that provides a lot of comments to explain how it works.
References
Other tryCatch replacements
Logging of warnings and errors with with a feature to perform multiple attempts (retries) at try catch, e. g. for accessing an unreliable network drive:
Handling errors before warnings in tryCatch
withJavaLogging function without any dependencies to other packages which also enriches the source code references to the call stack using limitedLabels:
Printing stack trace and continuing after error occurs in R
Other helpful links
http://adv-r.had.co.nz/Exceptions-Debugging.html
A Warning About warning() - avoid R's warning feature
In R, why does withCallingHandlers still stops execution?
How to continue function when error is thrown in withCallingHandlers in R
Can you make R print more detailed error messages?
How can I access the name of the function generating an error or warning?
How do I save warnings and errors as output from a function?
options(error=dump.frames) vs. options(error=utils::recover)
General suggestions for debugging in R
Suppress warnings using tryCatch in R
R Logging display name of the script
Background information about the "srcrefs" attribute (Duncan Murdoch)
get stack trace on tryCatch'ed error in R
The traceback function can be used to print/save the current stack trace, but you have to specify an integer argument, which is the number of stack frames to omit from the top (can be 0). This can be done inside a tryCatch block or anywhere else. Say this is the content of file t.r:
f <- function() {
x <- 1
g()
}
g <- function() {
traceback(0)
}
When you source this file into R and run f, you get the stack trace:
3: traceback(0) at t.r#7
2: g() at t.r#3
1: f()
which has file name and line number information for each entry. You will get several stack frames originating from the implementation of tryCatch and you can't skip them by specifying a non-zero argument to traceback, yet indeed this will break in case the implementation of tryCatch changes.
The file name and line number information (source references) will only be available for code that has been parsed to keep source references (by default the source'd code, but not packages). The stack trace will always have call expressions.
The stack trace is printed by traceback (no need to call print on it).
For logging general errors, it is sometimes useful to use options(error=), one then does not need to modify the code that causes the errors.

MonetDB embedded R code debugging

In my efforts to work around the issue mentioned here:
MonetDB connect to GO.db within R code that is run in-database
I went ahead and copied the code from WGCNA that I needed to my own package and installed it. Obviously, I now can load the package without any issues (since I didn't need the GO.db part).
However, I seem to run into another issue:
Server says '!Error running R expression. Error message: Error in
.C("corFast", x = as.double(x), nrow = as.integer(nrow(x)), ncolx =
as.integer(ncol(x)), : '.
I indeed wanted to use the faster cor function from WGCNA, but apparently the C call now creates another issue.
Unfortunately, the message is not informative. I already tried to run the query interactively and adding debug to the statement. This did not provide me with more information.
Is there anything that I can do to increase the verbosity, so that I can debug the proces?
I also tried:
options(monetdb.debug.query=F)
This resulted in a bit of extra output prior to the query, but no extra output on the error that occurred.
Using the suggestion of Hannes Muehleisen I added:
options(monetdb.debug.mapi=T)
It does add a little more information, which allowed me to proceed a bit further. I am now stuck with the following error, which seems again truncated.
QQ: 'SELECT * FROM cor_test();' TX: 'sSELECT * FROM cor_test(); ; RX:
'!Error running R expression. Error message: Error in .C("corFast", x
= as.double(x), nrow = as.integer(nrow(x)), ncolx = as.integer(ncol(x)), : ! "corFast" not available for .C() for
package "MRMRF Error in .local(conn, statement, ...) : Unable to
execute statement 'SELECT * FROM cor_test();'. Server says '!Error
running R expression. Error message: Error in .C("corFast", x =
as.double(x), nrow = as.integer(nrow(x)), ncolx = as.integer(ncol(x)),
: '.
Yes this is a known issue where only the first line of the error message is returned. We should fix this. I always use stop(whatever) to return some info from within the UDF.

R, tryCatch error

I am parsing alot of website and I wrote a script that loops thru the thousands of link from a separate file. However, I experienced that sometimes R couldn't load one link and it stops in the middle of loop, leaving many of other urls unparsed. So I tried to use tryCatch, so the script ignores this case and keep parsing next urls. However, I recently experienced that tryCatch generates below error.
gethelp.url = 'http://forums.autodesk.com/t5/Vault-General/bd-p/101'
gethelp.df =tryCatch(htmlTreeParse(gethelp.url, useInternalNodes = T), error = function() next)
Error in value[[3L]](cond) : unused argument (cond)
Calls: withRestarts ... tryCatch -> tryCatchList -> tryCatchOne -> <Anonymous>
Execution halted
The confusing thing is sometimes it works well and sometimes it throws out this error message, even though the same script parses the same urls.
Can anyone give me a guidance how to interpret this error messages? I read the document but i couldn't find much insights.
I think your function has to have cond as an argument – at least that's how I've used tryCatch() in the past, and your error message seems to indicate it as the problem.
Try the following:
gethelp.df =tryCatch(htmlTreeParse(gethelp.url, useInternalNodes = T), error = function(cond) next)
Note that the above line will still throw an error, b/c the example code is not in a loop. So I just replaced next with NA, and it worked fine.
Edit: In response to OP's comment, I suggest trying the following:
gethelp.df =tryCatch(htmlTreeParse(gethelp.url, useInternalNodes = T), error = function(cond)"skip")
if(gethelp.df=="skip"){next}

Resources