Skip to content

Commit

Permalink
0
Browse files Browse the repository at this point in the history
  • Loading branch information
wanganlin00 committed Nov 1, 2024
1 parent 0be1ac0 commit 3cf3ab2
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 25 deletions.
12 changes: 11 additions & 1 deletion R_notes_version_0/data_structure.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,17 @@ R中的数据结构包括 **原子向量**(atomic vector)和**泛型向量**

## 属性

在R中任何一个**对象**都具有两个最基本的属性:数据类型和长度,分别可以通过调用 `typeof()``length()` 查看,但无法通过 `attributes()` 查看。
在R中任何一个**对象**都具有属性:

- names,dimnames

- length

- dimensions(例如matrices,arrays)

- class

- 自定义属性

在R中有多种方式查看对象的属性,

Expand Down
2 changes: 1 addition & 1 deletion R_notes_version_0/data_type.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

数据类型是指数据的存储格式,定义了存储在变量中的数据的性质和类别,通常是原子的,无法分解成更简单的类型。

在R中有5 种最基本的数据存储格式,分别是**numeric****integer****double**),**logical****character****complex****raw**
在R中有5 种最基本的数据存储格式base type: **numeric****integer****double**),**logical****character****complex****raw**

## numeric

Expand Down
82 changes: 59 additions & 23 deletions R_notes_version_0/exception_handling.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,7 @@

R中的条件(condition)系统提供了一组成对的工具,提示函数正在发生异常情况,并允许该函数的用户处理它

提示信息

```{r}
print("Running...")
message("Running...")
cat("Running...\n")
warning("Running...")
```

signals conditions
## signals conditions

- `stop()` for errors
- `warning()` for warnings
Expand All @@ -28,14 +16,12 @@ warning("This is what a warning looks like")
message("This is what a message looks like")
```

处理条件信息
- `tryCatch()`
- `withCallingHandlers()`
print("Running...")
cat("Running...\n")
```

## error
### error

停止执行并返回到顶部

Expand All @@ -51,7 +37,7 @@ h <- function() rlang::abort("This is an error!")
h()
```

## warning
### warning

捕获警告并聚合显示

Expand Down Expand Up @@ -83,7 +69,7 @@ fw <- function() {
fw()
```

## message
### message

消息立即显示

Expand All @@ -108,8 +94,9 @@ fm()
- 忽略错误:`try()`,最好是使用[`tryCatch()`](https://rdrr.io/r/base/conditions.html)

```{r error=TRUE}
default <- NULL
try(default=read.csv("possibly-bad-input.csv"), silent = FALSE)
try(log("a"))
try(log("a"),silent = TRUE)
```
- 忽略警告。[`suppressWarnings()`](https://rdrr.io/r/base/warning.html)
Expand Down Expand Up @@ -145,6 +132,11 @@ conditionCall(cnd)

### 退出处理程序

处理条件信息

- `tryCatch()`
- `withCallingHandlers()`

[`tryCatch()`](https://rdrr.io/r/base/conditions.html)定义**exiting**handlers,,通常用于处理错误情况。它允许您覆盖默认的错误行为。

```{r eval=FALSE}
Expand Down Expand Up @@ -318,3 +310,47 @@ my_log <- function(x, base = exp(1)) {
my_log(letters)
my_log(letters)
```

## Debugging

`traceback()`: 函数调用栈

```{r error=TRUE}
lm(y ~ x)
traceback()
```

表示第7次调用函数出现错误。

`debug()` : 标记函数,调用函数时出现错误自动进入browser,输入 `n` 一行一行运行直到出现错误

```{r error=TRUE}
debug(lm)
lm(y ~ x)
# Browse[1]> n
# debug: ret.x <- x
# Browse[1]> n
# debug: ret.y <- y
# Browse[1]> n
# debug: cl <- match.call()
# Browse[1]> n
# debug: mf <- match.call(expand.dots = FALSE)
# Browse[1]> n
# debug: m <- match(c("formula", "data", "subset", "weights", "na.action",
# "offset"), names(mf), 0L)
# Browse[1]> n
# debug: mf <- mf[c(1L, m)]
# Browse[1]> n
# debug: mf$drop.unused.levels <- TRUE
# Browse[1]> n
# debug: mf[[1L]] <- quote(stats::model.frame)
# Browse[1]> n
# debug: mf <- eval(mf, parent.frame())
# Browse[1]> n
# Error in eval(predvars, data, env) : object 'y' not found
undebug(lm)
```

0 comments on commit 3cf3ab2

Please sign in to comment.