我正在写一个需要时间的函数。如果时间格式不正确,我想停止执行该函数。然而,当我使用tryCatch时,尽管消息显示,结果仍然保存,函数完成。
这是我有的东西
date_bad <- "date"
date_good <- "2022-03-25"
tryCatch(
date_check <- lubridate::ymd(date),
warning = function(w) { stop("Hi! date is not in the YYYY-MM-DD format.") }
有没有人有解决Jupyter with R问题的办法?此错误消息不会在Python或Julia中出现,并且会在我不打印对象时打印出来。 没有错误地工作:print(x) 与错误一起工作:x ERROR while rich displaying an object: Error in withCallingHandlers({: No repr_* for mimetype application/geo+json in repr::mime2repr
Traceback:
1. FUN(X[[i]], ...)
2. tryCatch(withCallingHandlers({
.
我有一个函数fun,它经常产生警告并偶尔抛出错误。我试图使用tryCatch记录警告和错误,就像中所建议的那样。如何同时存储警告和错误?
下面是一个最小的设置:
# Function for warnings, errors.
fun <- function(i) {
# Print warnings as they come in.
options(warn = 1)
# Issue warning.
warning(paste("Warn.", i))
# Stop.
if(i == 3) { stop(paste(
我想压制一个经常出现在coxph分析中的警告。警告"Loglik在变量1之前收敛;系数可能是无限的“是众所周知的过于敏感的,我还有其他方法--检查结果。我的第一次尝试是在调用处理程序中封装coxph:
coxMW <- function(...){
withCallingHandlers(coxph(...),
warning=function(w) {
if (grepl("coefficient may be infinite", w$message))
我有一个包含一些样本数据的csv文件(aprox 1000行)。使用read.table读取csv时 read.table(csv_File,header = FALSE, sep=",",na.strings = '') 我收到了一个错误, Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec, :
line 515 did not have 5 elements 有没有办法通过使用tryCatch和withCallingHandlers来打印此
如何允许\Sexpr中的错误
我有一份针织品。本文档的一小部分涉及无法共享的文件。因此,每当对依赖于正在读取的文件的某个对象调用\Sexpr{a}时,它都会返回一个错误。相反,我希望\Sexpr打印它遇到了一个错误。
例如,
\documentclass{article}
\usepackage{xcolor} % for red
\begin{document}
<<>>=
x <- 1
@
The value of $x$ is \Sexpr{x}
<<>>=
a <- scan("secret_file.txt
问题
我试图在R中写一个循环,其中我预期会出现一些错误。我没有停止循环,而是试图找到一种方法来捕获错误消息,并将该信息包含在输出中并继续循环。
关于数据的注意事项:数据来自NOAA网站,它是南方涛动指数数据。第二个集合是平凡的(data2),它的出现仅仅是为了产生一个错误。
下面是我试图创建的循环类型的一个稍微微不足道的例子。从web获取一些数据,对其执行一些操作,将其存储(作为df),然后获取更多的数据,执行相同的操作,并(通过rbind)将其附加到第一个数据:
data_spec <- c("data")
df <- c()
for (i in data_spe
R没有分类错误,只是(大部分情况下) error和warning,所以确定错误是否应该被捕获或传递取决于程序员。在python方面,我一直很欣赏捕捉特定错误并传递其他错误的能力,如py3教程中关于处理异常的一个例子:
import sys
try:
f = open('myfile.txt')
s = f.readline()
i = int(s.strip())
except OSError as err:
print("OS error: {0}".format(err))
except ValueError:
pri
op <- options(warn=0) #although doesn't work for any value of warn
assign("last.warning", NULL, envir = baseenv())
thisDoesntWork<- function() {
warning("HEY, this is definitely a warning!")
cat(paste("number of warnings:",length(warnings())))
}
>thisDoesnt
我知道如何引发错误:
check_negative <- function(x) {
if (x > 0) {
stop("No way can x be positive.")
}
x
}
check_negative(5)
#> Error in check_negative(5) : No way can x be positive.
由于tryCatch的目的是处理这样的错误,但是tryCatch并不是严格意义上的,所以我想知道是否有一种方法可以在发生check_negative(5)时总是引发错误,即使它被包装在tryCatch
使用withCallingHandlers().无法捕捉到S4泛型函数的计算参数时生成的警告
说明withCallingHandlers的正常行为:
### simple function that sends a warning
send_warning <- function() {
warning('send_warning')
}
send_warning()
# Warning message:
# In send_warning() : send_warning
### the warning can be caught by withCallingHa
我有一个更多的链接到pdf文件,我想下载使用download.file在一个for循环。我的解决方案工作正常,但是当它遇到错误时它就停止了(许多文件不工作)。我想在我的download.file函数中添加一个功能,它告诉R,如果下载的文件会产生错误,就跳过一个文件,并打印一条带有出现错误的页面名称的消息。
我发现在这种情况下,tryCatch可能是一个很好的解决方案,但我并不完全确定将它放在哪里(我尝试过许多方法,但都没有成功)。
这是我的代码:
for (i in length(files) {
# Reads the html links
html <- read_html(