我开始使用RStudio笔记本,我仍然试图了解一些工作原理。 我不明白为什么在执行与消息完全无关的代码时会保留并显示某些生成的警告消息。例如,我有一个包含多个块的文档,其中最后一个生成警告
> warnings()
Warning messages:
1: Unknown or uninitialised column: 'perc.goal.met.period'.
2: Unknown or uninitialised column: 'perc.goal.met.period'.
3: Unknown or uninitialised column: 'perc.goal.met.period'.
4: Unknown or uninitialised column: 'perc.goal.met.period'.
5: Unknown or uninitialised column: 'perc.goal.met.period'.
6: Unknown or uninitialised column: 'perc.goal.met.period'.
7: Unknown or uninitialised column: 'perc.goal.met.period'.
8: Unknown or uninitialised column: 'perc.goal.met.period'.
9: Unknown or uninitialised column: 'perc.goal.met.period'.
10: Unknown or uninitialised column: 'perc.goal.met.period'.
11: Unknown or uninitialised column: 'perc.goal.met.period'.
12: Unknown or uninitialised column: 'perc.goal.met.period'.
13: Unknown or uninitialised column: 'perc.goal.met.period'.
14: Unknown or uninitialised column: 'perc.goal.met.period'.
15: Unknown or uninitialised column: 'perc.goal.met.period'.
16: Unknown or uninitialised column: 'perc.goal.met.period'.
17: Unknown or uninitialised column: 'perc.goal.met.period'.
18: Unknown or uninitialised column: 'perc.goal.met.period'.
19: Unknown or uninitialised column: 'perc.goal.met.period'.
20: Unknown or uninitialised column: 'perc.goal.met.period'.
21: Unknown or uninitialised column: 'perc.goal.met.period'.
22: Unknown or uninitialised column: 'perc.goal.met.period'.
23: Unknown or uninitialised column: 'perc.goal.met.period'.
24: Unknown or uninitialised column: 'perc.goal.met.period'.
25: Unknown or uninitialised column: 'perc.goal.met.period'.
26: Unknown or uninitialised column: 'perc.goal.met.period'.
27: Unknown or uninitialised column: 'perc.goal.met.period'.
28: Unknown or uninitialised column: 'perc.goal.met.period'.
29: Unknown or uninitialised column: 'perc.goal.met.period'.
30: Unknown or uninitialised column: 'perc.goal.met.period'.
There were 30 warnings (use warnings() to see them)
我很满意这个警告。但后来,我以为我会在第一个块(我加载它们)中加载一个额外的库。运行该块后,我得到:
```{r echo=F, message=F, warnings=F, include=F}
# Load libraries
library(rgdal)
library(raster)
library(openxlsx)
library(tidyverse)
library(dplyr)
library(magrittr)
library(ggplot2)
library(rasterVis)
```
There were 30 warnings (use warnings() to see them)
如果我看到警告,那就是我之前打过的那些。
为什么我在这里看到他们?我也在与变量perc.goal.met.period
无关的其他块中看到了这一点。如果我看到警告,他们会停止出现一段时间,但在我无法预料的那一刻,他们最终会在某个时刻重新出现。
这种行为有合理的解释吗?非常感谢你的帮助!
答案 0 :(得分:1)
在清除之前,您会看到警告消息。运行警告()函数不会这样做。 要清除警告,您可以执行以下命令:
from scipy.io import wavfile
import librosa
fs, data = wavfile.read(name)
data, fs = librosa.load(name)
但最好的方法是修复代码,以免产生警告。处理它的一种方法是在R中使用assign("last.warning", NULL, envir = baseenv())
。
您也可以使用tryCatch()
功能禁用所有警告,但不建议这样做,因为这样会阻止您查看其中任何警告。
答案 1 :(得分:0)
我的经验是,这发生在RStudio中,而我的代码仍然有错误,即使它没有运行(例如,当我留下一些错误的代码以供以后修改时)。当我删除,更改或转换成相关行的注释时,此行为结束。我猜这是由RStudio解释器引起的。知道使用基数R的人是否有相同的问题将很有趣。