+ Näytä koodi- Piilota koodi
library(OpasnetUtils)
library(DiagrammeR)
objects.latest("Op_en3861",code_name="ecd_create") # splizzeria, ecd_create
## Code for KEKO analysis
cod <- readLines("KEKO_code_all.txt", encoding = "UTF-8")
funs <- grep("^def ", cod) # First row of function
tmp <- funs # Last row of input parameters
for(i in 1:10) {
tmp <- ifelse(grepl(":",cod[tmp]),tmp, tmp+1)
}
tmp2 <- sapply(1:length(funs), FUN=function(i) paste0(cod[funs[i]:tmp[i]],collapse=""))
tmp2 <- gsub("):$","",gsub("^def ","",tmp2))
tmp2 <- data.frame(
Oldid=paste0("Keko.",1:length(funs)),
type = "method",
t(as.data.frame(strsplit(tmp2,"\\("))),
Relation = "uses",
Description = cod[tmp+1],
URL="http://www.ymparisto.fi/keko",
stringsAsFactors = FALSE
)
colnames(tmp2)[3:4] <- c("Item","Object")
rets <- grep("^ +return ",cod) # Return row(s) of function
retfun <- floor(approx(funs,1:length(funs),rets)$y)
retfun <- ifelse(is.na(retfun),length(funs),retfun) # Respective function of return objects
tmp3 <- data.frame(
Oldid=paste0("Keko.", seq(length(funs)+1, length.out=length(rets))),
type="substance",
Item=gsub("]","",gsub("\\[","",gsub("#.*$","",gsub("return","",cod[rets])))),
Relation="is produced by",
Object=tmp2$Item[retfun],
URL="http://www.ymparisto.fi/keko",
stringsAsFactors = FALSE
)
#tmp2 <- orbind( # Problematic because created duplicate nodes
# tmp2,
# data.frame(
# Item = tmp2$Object,
# type = "data",
# stringsAsFactors = FALSE
# )
#)
out <- orbind(tmp2, tmp3)
out <- splizzeria(out, c("Item","Object"), ",")
out$label <- out$Item
out <- ecd_create(out)
out[[1]] <- out[[1]][!grepl("^Keko.",out[[1]]$label),] # Remove redundant nodes created in ecd_create. Why are they even here?
gr2 <- create_graph(
nodes_df = out$nodes_df,
edges_df = out$edges_df
)
render_graph(gr2)
export_graph(gr2, "Keko-laskurin näkemysverkko.svg")
| |