R程序中是否有类似MVC的结构来一次更改多个脚本
Is there MVC like structure in R program to change many scripts at once
我在 RStudio 工作,我有一定数量的脚本对应于我们网络所在的每个地区。
每次更新 script1 时,我都必须更新 scripts2 一直到 script24。
这些脚本之间的唯一区别是
- 工作目录
- .csv文件读入数据框
- bbox周围的填充,即f值
这是其中一个的实际代码
library(ggmap)
library(ggplot2)
setwd("d:/GIS/different_directory")
sep <- read.csv("district_number_SEP_assets_csv.csv")
Sub1 <- sep[grep("SEP.12", names(sep))]
sep$newCol <- 100*rowSums(Sub1)/rowSums(sep[4:7])
# create a new grouping variable
Percent_SEP12_Assets <- ifelse(sep[,8] <= 33, "Lower Third", ifelse(sep[,8] >= 66, "Upper Third", "Middle Third"))
Percent_SEP12_Assets <- factor(Percent_SEP12_Assets,
levels = c("Upper Third", "Middle Third", "Lower Third"))
# get the map
bbox <- make_bbox(sep$Longitude, sep$Latitude, f = varies from scripts)
map <- get_map(bbox)
# plot the map and use the grouping variable for the fill inside the aes
ggmap(map) +
geom_point(data=sep, aes(x = Longitude, y = Latitude, color=Percent_SEP12_Assets ), size=5, alpha=0.6) +
scale_color_manual(values=c("green","orange","red"))
必须有更精简的方法来做到这一点。
更多信息
我根据数据点是否被截断来确定f
,并保持f
尽可能少。
script1 中的更改对 script2 等没有影响。每个分区的脚本都是彼此的副本,因此如果我更改 script1,则必须更改 script2。
区号硬编码到文件名中,硬编码到R脚本中。
将所有 csv 文件复制并粘贴到一个文件夹中,例如,一个名为 mywd
的文件夹
#Then make this folder with all files your WD
setwd("d:/GIS/mywd")
# As you correctly noted, writing code the number of times you need to
# run it is not much fun :-)
# `for` loops exist just for this. You write the code only once
# The `loop` then applies that code to as many inputs as you have
# in your case, district CSV's
创建所有学区 csv 文件的列表
dlist <- list.files("mywd", 模式="SEP_assets_csv.csv")
使用 for loop
在 dlist 中的 csv 文件列表上迭代您的代码
for(sep in dlist){
sep <- read.csv("sep")
Sub1 <- sep[grep("SEP.12", names(sep))]
sep$newCol <- 100*rowSums(Sub1)/rowSums(sep[4:7])
# create a new grouping variable
Percent_SEP12_Assets <- ifelse(sep[,8] <= 33, "Lower Third", ifelse(sep[,8] >= 66, "Upper Third", "Middle Third"))
Percent_SEP12_Assets <- factor(Percent_SEP12_Assets,
levels = c("Upper Third", "Middle Third", "Lower Third"))
# get the map
# Note Exclusion of the `f` argument
# Data points are cut off because x, y or Percent_SEP12_Assets are missing
# You MUST have x and y coords to show any point, so any row without x or y must be excluded since its position is not fully described
# If `Percent_SEP12_Assets` is missing, we can show it with a special colour e.g. yellow
bbox <- make_bbox(sep$Longitude, sep$Latitude)
map <- get_map(bbox)
# plot the map and use the grouping variable for the fill inside the aes
(ggmap(map) +
geom_point(data=sep, aes(x = Longitude, y = Latitude,
color=Percent_SEP12_Assets, size=5, alpha=0.6) +
scale_color_manual(values=c("green","orange","red"), na.value="yellow"))
}
还有两件事,请看this and this。
我在 RStudio 工作,我有一定数量的脚本对应于我们网络所在的每个地区。
每次更新 script1 时,我都必须更新 scripts2 一直到 script24。
这些脚本之间的唯一区别是
- 工作目录
- .csv文件读入数据框
- bbox周围的填充,即f值
这是其中一个的实际代码
library(ggmap)
library(ggplot2)
setwd("d:/GIS/different_directory")
sep <- read.csv("district_number_SEP_assets_csv.csv")
Sub1 <- sep[grep("SEP.12", names(sep))]
sep$newCol <- 100*rowSums(Sub1)/rowSums(sep[4:7])
# create a new grouping variable
Percent_SEP12_Assets <- ifelse(sep[,8] <= 33, "Lower Third", ifelse(sep[,8] >= 66, "Upper Third", "Middle Third"))
Percent_SEP12_Assets <- factor(Percent_SEP12_Assets,
levels = c("Upper Third", "Middle Third", "Lower Third"))
# get the map
bbox <- make_bbox(sep$Longitude, sep$Latitude, f = varies from scripts)
map <- get_map(bbox)
# plot the map and use the grouping variable for the fill inside the aes
ggmap(map) +
geom_point(data=sep, aes(x = Longitude, y = Latitude, color=Percent_SEP12_Assets ), size=5, alpha=0.6) +
scale_color_manual(values=c("green","orange","red"))
必须有更精简的方法来做到这一点。
更多信息
我根据数据点是否被截断来确定f
,并保持f
尽可能少。
script1 中的更改对 script2 等没有影响。每个分区的脚本都是彼此的副本,因此如果我更改 script1,则必须更改 script2。
区号硬编码到文件名中,硬编码到R脚本中。
将所有 csv 文件复制并粘贴到一个文件夹中,例如,一个名为 mywd
的文件夹
#Then make this folder with all files your WD
setwd("d:/GIS/mywd")
# As you correctly noted, writing code the number of times you need to
# run it is not much fun :-)
# `for` loops exist just for this. You write the code only once
# The `loop` then applies that code to as many inputs as you have
# in your case, district CSV's
创建所有学区 csv 文件的列表
dlist <- list.files("mywd", 模式="SEP_assets_csv.csv")
使用 for loop
在 dlist 中的 csv 文件列表上迭代您的代码
for(sep in dlist){
sep <- read.csv("sep")
Sub1 <- sep[grep("SEP.12", names(sep))]
sep$newCol <- 100*rowSums(Sub1)/rowSums(sep[4:7])
# create a new grouping variable
Percent_SEP12_Assets <- ifelse(sep[,8] <= 33, "Lower Third", ifelse(sep[,8] >= 66, "Upper Third", "Middle Third"))
Percent_SEP12_Assets <- factor(Percent_SEP12_Assets,
levels = c("Upper Third", "Middle Third", "Lower Third"))
# get the map
# Note Exclusion of the `f` argument
# Data points are cut off because x, y or Percent_SEP12_Assets are missing
# You MUST have x and y coords to show any point, so any row without x or y must be excluded since its position is not fully described
# If `Percent_SEP12_Assets` is missing, we can show it with a special colour e.g. yellow
bbox <- make_bbox(sep$Longitude, sep$Latitude)
map <- get_map(bbox)
# plot the map and use the grouping variable for the fill inside the aes
(ggmap(map) +
geom_point(data=sep, aes(x = Longitude, y = Latitude,
color=Percent_SEP12_Assets, size=5, alpha=0.6) +
scale_color_manual(values=c("green","orange","red"), na.value="yellow"))
}
还有两件事,请看this and this。