蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行判断链、边界与 switch

R基础 R。已在项目独立的 Windows x64 R 4.6.2 环境验证。decisions.R
decisions.R57 行
emit <- function(key, values) {
  cat(key, "=", paste(values, collapse = ","), "\n", sep = "")
}
classify <- function(count) {
  if (count >= 18) {
    "high"
  } else if (count >= 12) {
    "medium"
  } else {
    "low"
  }
}
emit("integer", c(is.integer(24L), is.integer(24)))
installed <- c("wind", "rain")
emit("membership", c(if ("rain" %in% installed) "available" else "missing",
                     if ("heat" %in% installed) "available" else "missing"))
emit("chain", c(classify(19), classify(14), classify(11)))
skipped <- withVisible(if (FALSE) 1L)
emit("no_else", c(is.null(skipped$value), skipped$visible))
error_from <- function(expr) tryCatch({force(expr); FALSE}, error = function(e) TRUE)
emit("conditions", c(error_from(if (NA) 1L), error_from(if (logical()) 1L),
                     error_from(if (c(TRUE, FALSE)) 1L),
                     error_from(parse(text = "if (TRUE) { // invalid }"))))

emit("numeric_switch", c(switch(3, "north", "east", "south"),
                         switch(2.9, "north", "east", "south"),
                         is.null(switch(4, "north", "east", "south"))))
emit("named_switch", c(switch("east", east = "station-2", west = "station-1"),
                       is.null(switch("ea", east = "station-2")),
                       switch("other", east = "station-2", "unassigned")))
emit("switch_rules", c(switch("a", a = "first", a = "second"),
                       switch("spring", spring =, summer = "wet"),
                       switch("ok", ok = "safe", stop("unselected")),
                       error_from(switch("unknown", "first", "second"))))

png("decision-flow.png", width = 720, height = 420)
par(mar = c(1, 1, 2.4, 1))
plot.new()
plot.window(xlim = c(0, 10), ylim = c(0, 7))
title("Decision order for one observation count", col.main = "#17354b")
node <- function(x, y, text_value, fill) {
  rect(x - 1.45, y - 0.42, x + 1.45, y + 0.42, col = fill, border = "#315770")
  text(x, y, text_value, col = "#17354b", cex = 1.1)
}
node(3, 6, "count >= 18?", "#e1eef6")
node(7, 6, "high (19)", "#dff0da")
node(3, 3.8, "count >= 12?", "#e1eef6")
node(7, 3.8, "medium (14)", "#dff0da")
node(3, 1.6, "low (11)", "#dff0da")
arrows(4.45, 6, 5.55, 6, length = 0.08)
arrows(4.45, 3.8, 5.55, 3.8, length = 0.08)
arrows(3, 5.58, 3, 4.22, length = 0.08)
arrows(3, 3.38, 3, 2.02, length = 0.08)
text(c(5, 5), c(6.32, 4.12), "yes", cex = 0.92)
text(c(3.37, 3.37), c(4.96, 2.76), "no", cex = 0.92)
invisible(dev.off())
本例需在 基础 R。已在项目独立的 Windows x64 R 4.6.2 环境验证。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:基础 R。已在项目独立的 Windows x64 R 4.6.2 环境验证。

入口文件:projects/r-types-and-decisions/decisions.R

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 decisions.R,或下载右侧课程工程包。
  2. 确认本地已准备 基础 R。已在项目独立的 Windows x64 R 4.6.2 环境验证。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:R

课程关联工程包(登记关联29章) · 含当前章节或实例 · mbskycom202612a317.zip

下载课程关联工程包:R

课程关联工程包(登记关联2章) · 含当前章节或实例 · mbskycom202612a328.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习