OpasnetUtils/Drafts: Difference between revisions

From Opasnet
Jump to navigation Jump to search
(→‎Calculations: semi-working showind saved)
 
(12 intermediate revisions by the same user not shown)
Line 23: Line 23:
=== Calculations ===
=== Calculations ===


'''Functions for ovariable diagnostics
====Expand_index====
 
<rcode name="expand_index" label="Initiate expand_index" embed=1>
# This is code Op_en6007/expand_index in page [[OpasnetUtils/Drafts]]
 
library(OpasnetUtils)
 
#' Copies results from one location to other locations while keeping everything else constant
#' @param ova ovariable to be used
#' @param locations list of relevant indices. Each item is a list that has a name of an existing locations, and the content is a vector of new location names.
#' @return the input ovariable with new rows containing the new locations
 
expand_index <- function(ova, locations) {
  if("ovariable" %in% class(ova)) out <- ova@output else out <- ova
  for(i in names(locations)) {
    for(j in names(locations[[i]])) {
      addpiece <- out[out[[i]]==j,]
      for(k in locations[[i]][[j]]) {
        addpiece[[i]] <- k
        out <- rbind(out,addpiece)
      }
    }
  }
  if("ovariable" %in% class(ova)) ova@output <- out else ova <- out
  return(ova)
}
 
objects.store(expand_index)
cat("Function expand_index stored.\n")
</rcode>
 
====Functions for ovariable diagnostics====
 
showind has problems with get() but this version of code was acceptable [http://en.opasnet.org/en-opwiki/index.php?title=Special:RTools&id=QcfKMkCd2ewUtZqP].
showind has problems with get() but this version of code was acceptable [http://en.opasnet.org/en-opwiki/index.php?title=Special:RTools&id=QcfKMkCd2ewUtZqP].


Line 39: Line 71:
   deptable <- data.frame()
   deptable <- data.frame()
   for(i in ls(name = name)) {
   for(i in ls(name = name)) {
     #    d <- g#et(i)
     d = list(get(i))[[1]]
     if(class(get(i)) == "ovariable") {
     if(class(d) == "ovariable") {
       depind <- list()
       depind <- list()
       if(nrow(get(i)@dependencies)>0) {
       if(nrow(d@dependencies)>0) {
         dep <- paste(get(i)@dependencies$Name, collapse = ", ")
         dep <- paste(d@dependencies$Name, collapse = ", ")
         for(k in get(i)@dependencies$Name){
         for(k in d@dependencies$Name){
           if(!exists(k)) cat(k, "does not exist.\n") else {
           if(!exists(k)) cat(k, "does not exist.\n") else {
             if(class(get(k)) != "ovariable") cat(k, "is not an ovariable.\n") else {
             if(class(get(k)) != "ovariable") cat(k, "is not an ovariable.\n") else {
               #              ko <- g#et(k)
               ko <- list(get(k)@output)[[1]]
              # We don't want to look at all Iters, but forbidden <-get() makes this tricky., so we just disable
               if("Iter" %in% colnames(ko)) ko$Iter <- as.factor(max(as.numeric(as.character(ko$Iter))))
              # this functionality at the moment. koIter should be replaced by ko$Iter where ko <- g#et(k)
               cols <- colnames(ko)
               if("Iter" %in% colnames(get(k)@output)) koIter <- as.factor(max(as.numeric(as.character(get(k)$Iter))))
               cols <- colnames(get(k)@output)
               if(!sources) cols <- cols[!grepl("Source$", cols)]
               if(!sources) cols <- cols[!grepl("Source$", cols)]
               if(!prevresults) cols <- cols[!grepl("Result$", cols)]
               if(!prevresults) cols <- cols[!grepl("Result$", cols)]
               for(l in cols) {
               for(l in cols) {
                 if(l %in% names(depind)) {
                 if(l %in% names(depind)) {
                   depind[[l]] <- union(depind[[l]], unique(get(k)@output[[l]]))
                   depind[[l]] <- union(depind[[l]], unique(ko[[l]]))
                 } else {
                 } else {
                   newind <- list(unique(get(k)@output[[l]]))
                   newind <- list(unique(ko[[l]]))
                   names(newind) <- l
                   names(newind) <- l
                   depind <- c(depind, newind)
                   depind <- c(depind, newind)
Line 69: Line 99:
         dep <- "No dependencies"
         dep <- "No dependencies"
       }
       }
       curcols <- colnames(get(i)@output)
       curcols <- colnames(d@output)
       if(!sources) curcols <- curcols[!grepl("Source$", curcols)]
       if(!sources) curcols <- curcols[!grepl("Source$", curcols)]
       if(!prevresults) curcols <- curcols[!grepl("Result$", curcols)]
       if(!prevresults) curcols <- curcols[!grepl("Result$", curcols)]
       droploc <- character()
       droploc <- character()
       for(m in curcols) {
       for(m in curcols) {
         if(!is.numeric(get(i)@output[[m]])) {
         if(!is.numeric(d@output[[m]])) {
           drops <- setdiff(depind[[m]], unique(get(i)@output[[m]]))
           drops <- setdiff(depind[[m]], unique(d@output[[m]]))
           if(length(drops>0)) {
           if(length(drops>0)) {
             droploc <- paste(
             droploc <- paste(
Line 196: Line 226:
</rcode>
</rcode>


'''Functions for Webropol data
====Functions for Webropol data====


<rcode name="webropol" embed=1>
<rcode name="webropol" embed=1>
Line 268: Line 298:
</rcode>
</rcode>


'''Miscellaneous functions
==== HNH2035 functions ====
 
These are functions that were needed and developed for the [[:op_fi:Hiilineutraali Helsinki 2035]] work.
 
<rcode name="hnh2035" label="Initiate functions for HNH2035" embed=1>
#This is code Op_en6007/hnh2035 on page [OpasnetUtils/Drafts]]
library(OpasnetUtils)
 
# pushIndicatorGraph was moved to package https://github.com/jtuomist/CNH-energy
 
# Colors from the Helsinki theme
colhki <- rep(c("#0072c6","#00d7a7","#c2a251","#9fc9eb","#ffc61e","#009246"),5)
 
#' Remove overlap with happened and scenarios, and then combine lines
#' @param ova ovariable where lines are combined
#' @param measured name for the scenario that contain actual data about measured things
 
combineLines <- function(ova, measured="toteutunut") {
  tst <- unique(ova[ova$Scenario==measured,]$Year) # Years with data
  out <- ova[ova$Scenario==measured | !ova$Year %in% tst,] # Remove scenarios if data
  tmp <- out[out$Scenario==measured & out$Year == max(tst),colnames(out@output)!="Scenario"]
  tmp <- tmp * Ovariable(
    output=data.frame(
      Scenario=setdiff(unique(out$Scenario),measured),
      Result=1
    ),
    marginal=c(TRUE,FALSE)
  )
  out <- combine(tmp,out, name=ova@name)
  return(out)
}
 
objects.store(combineLines, colhki)
cat("Function combineLines and colour vector colhki stored.\n")
</rcode>
 
====Miscellaneous functions====


<rcode name="miscellaneous" embed=1>
<rcode name="miscellaneous" embed=1>
#This is code Op_en6007/webropol on page [OpasnetUtils/Drafts]]
#This is code Op_en6007/miscellaneous on page [OpasnetUtils/Drafts]]


library(OpasnetUtils)
library(OpasnetUtils)
Line 755: Line 821:
</rcode>
</rcode>


'''Functions for GIS data
====Functions for GIS data====


<rcode name="gis" embed=1>
<rcode name="gis" embed=1>
Line 1,039: Line 1,105:
objects.store(MyPointKML, ova2spat, MyRmap, MyPlotKML)
objects.store(MyPointKML, ova2spat, MyRmap, MyPlotKML)
cat("Functions MyPointKML, ova2spat, MyRmap, MyPlotKML stored.\n")
cat("Functions MyPointKML, ova2spat, MyRmap, MyPlotKML stored.\n")
</rcode>
==== Timelineplot ====
<rcode name="timelineplot" label="Initiate function timelineplot" embed=1>
# This is code Op_en6007/timelineplot on page [[OpasnetUtils/Drafts]]
library(OpasnetUtils)
#' @description plots timeline for an ovariable or data frame.
#' @param ova an ovariable
#' @param noshow character vector of Work items that are not shown on graph.
timelineplot <- function(ova, noshow=c("Ulos","Out")) {
  require(ggplot2)
  resn <- paste0(ova@name,"Result")
  if(class(ova)=="ovariable") out <- ova@output else out <- ova
  colnames(out)[colnames(out)==resn] <- "Time"
  # Make sure that data is ordered correcly.
  out <- out[order(out$Person, out$Time) , ]
  out$Row <- 1:nrow(out)
 
  # Make endpoint for each task.
  b <- cbind(
    out[!colnames(out) %in% c("Work","Row")],
    Work=c(NA,as.character(out$Work)[1:(nrow(out)-1)]),
    Row =c(NA,out$Row[1:(nrow(out)-1)])
  )
  b$Work[match(unique(b$Person),b$Person)] <- NA
  out <- rbind(out,b)
  out <- na.omit(out[!out$Work %in% noshow , ])
  if(is.numeric(out$Time)) out$Time <- as.POSIXct(out$Time, origin="1970-01-01 00:00:00")
  pl <- ggplot(out, aes(x=Time,y=Person,colour=Work,group=Row))+geom_line(size=5)
  return(pl)
}
objects.store(timelineplot)
cat("Function timelineplot stored.\n")
</rcode>
</rcode>



Latest revision as of 10:37, 6 March 2021



Question

Which functions are so useful that they should be taken into OpasnetUtils package? This page contains draft function which will be included when they are good enough and found important.

Answer

Call the objects stored by this code from another rode with this command:

objects.latest("Op_en6007", code_name = "answer") # Old version that fetches all objects, depreciated and not updated.
objects.latest("Op_en6007", code_name = "diagnostics") # Functions for ovariable and model diagnostics: ovashapetest, showLoctable, binoptest
objects.latest("Op_en6007", code_name = "webropol") # Functions for operating with Webropol data
objects.latest("Op_en6007", code_name = "miscellaneous") # Functions for various tasks
objects.latest("Op_en6007", code_name = "gis") # Functions for ovariable, KML and Googl maps interactions

Rationale

Calculations

Expand_index

+ Show code

Functions for ovariable diagnostics

showind has problems with get() but this version of code was acceptable [1].

+ Show code

Functions for Webropol data

+ Show code

HNH2035 functions

These are functions that were needed and developed for the op_fi:Hiilineutraali Helsinki 2035 work.

+ Show code

Miscellaneous functions

+ Show code

Functions for GIS data

+ Show code

Timelineplot

+ Show code

See also

References


Related files

<mfanonymousfilelist></mfanonymousfilelist>