rstudio - Is there MVC like structure in R program to change many scripts at once -
i working in rstudio , have number of scripts corresponds each district our network in.
everytime make update script1, have make update scripts2 way script24.
only difference between these scripts are
- working directory
- .csv file read data frame
- the padding around bbox, i.e. f value
here actual code of 1 of them
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 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")) # map bbox <- make_bbox(sep$longitude, sep$latitude, f = varies scripts) map <- get_map(bbox) # plot map , use grouping variable fill inside 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"))
there must more streamlined way this.
more info
i determine f
based on whether data points cut off or not , keep f
lowest number possible.
changes in script1 have no effect on script2, etc. scripts copies of each other each district such if change script1, must change script2.
district number hard-coded file name, , hard-coded r script.
copy & paste csv's in 1 folder, say, 1 called mywd
#then make folder files wd setwd("d:/gis/mywd") # correctly noted, writing code number of times need # run not fun :-) # `for` loops exist this. write code once # `loop` applies code many inputs have # in case, district csv's
create list of district csv files
dlist <- list.files("mywd", pattern="sep_assets_csv.csv")
iterate code on list of csv files in dlist using for loop
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 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")) # map # note exclusion of `f` argument # data points cut off because x, y or percent_sep12_assets missing # must have x , y coords show point, row without x or y must excluded since position not described # if `percent_sep12_assets` missing, can show special colour e.g. yellow bbox <- make_bbox(sep$longitude, sep$latitude) map <- get_map(bbox) # plot map , use grouping variable fill inside 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")) }
Comments
Post a Comment