3

I searched a lot but did not find any answer to my question, even though I am sure it should not be so difficult. The thread that came closest received no answers (How do I access the name of the variable assigned to the result of a function within the function in R?)

In any case, I am trying to do the following: the function should create two objects, z and doc, and return it using the assigned name, and not the variable name. A short example:

fun.docmerge <- function(x, y, z, crit, typ, doc = checkmerge) {
  mergedat <- paste(deparse(substitute(x)), "+",
                    deparse(substitute(y)), "=", z)
  countdat <- nrow(x)
  check_t1 <- data.frame(mergedat, countdat)
  z <- join(x, y, by = crit, type = typ)
  countdat <- nrow(z)
  check_t2 <- data.frame(mergedat, countdat)
  doc <- rbind(doc, check_t1, check_t2)
  return(list(checkmerge = doc, z = z))
}

results <- fun.docmerge(x = df1, y = df2, z = "df3", crit = c("id"), typ = "left")

Some sample data:

df1 <- structure(list(id = c("XXX1", "XXX2", "XXX3", 
"XXX4"), tr.isincode = c("ISIN1", "ISIN2", 
"ISIN3", "ISIN4")), .Names = c("id", "isin"
), row.names = c(NA, 4L), class = "data.frame")

df2 <- structure(list(id= c("XXX1", "XXX5"), wrong= c(1L, 
1L)), .Names = c("id", "wrong"), row.names = 1:2, class = "data.frame")

checkmerge <- structure(list(mergedat = structure(integer(0), .Label = character(0), class = "factor"), 
    countdat = numeric(0)), .Names = c("mergedat", "countdat"
), row.names = integer(0), class = "data.frame")

The problem is that this returns z as z. However, I want it to be returned as df3 (the name assigned as argument). Is there a way to do that? I could easily solve it to return doc as checkmerge. However, z is dynamic so this would not work.

deca
  • 730
  • 1
  • 8
  • 24
  • Can you provide an example of `df1` and `df2` for the example? – storaged Nov 09 '17 at 12:40
  • I added a reprod example – deca Nov 09 '17 at 12:51
  • Just a hint: You are doing unusual things and I don't find your goal very useful. However, if you want to document joins that lead to a specific data.frame, I'd suggest storing that information in an attribute of the data.frame. – Roland Nov 09 '17 at 13:08
  • @Roland Thanks - this is exactly what I am trying to do. Can you point me to a question or similar where I see how this is done? – deca Nov 09 '17 at 13:12
  • 1
    `help("attr")` is really all you need to read. – Roland Nov 09 '17 at 13:14
  • @Roland The hint is great. So after reading the documentation on `attr` I assume that your suggestion would be to use attributes to document joins as follows: `attr(df1,"join1") <- nrow(df1)` Is this understanding correct? The problem is that then the logs are stored in each object and not centrally to immediately spot problems. Do you have an idea for that? – deca Nov 09 '17 at 13:44
  • 1
    I have no idea what you are trying to do and in particular why you believe to need this. To me it appears that you can get all information by simply reading (and if necessary commenting) your script. – Roland Nov 09 '17 at 14:26

1 Answers1

3

Try this

fun.docmerge <- function(x, y, z, crit, typ, doc = checkmerge) {
  mergedat <- paste(deparse(substitute(x)), "+",
                    deparse(substitute(y)), "=", z)
  countdat <- nrow(x)
  check_t1 <- data.frame(mergedat, countdat)
  z1 <- join(x, y, by = crit, type = typ)
  countdat <- nrow(z1)
  check_t2 <- data.frame(mergedat, countdat)
  doc <- rbind(doc, check_t1, check_t2)
  t1<-list()
  t1[["checkmerge"]]<-doc
  t1[[z]]<-z1
  return(t1)
}
niths4u
  • 440
  • 3
  • 13
  • Thanks! But when calling this function via `results <- fun.docmerge(x = df1, y = df2, z = "df3", crit = c("id"), typ = "left")` I get `Error in temp[[checkmerge]] <- doc : invalid subscript type 'list'` – deca Nov 09 '17 at 12:56
  • updated. if checkmerge is not a variable , then simply put that in quotes. May be if you can give a working set , we can help out. What i understood from your question is that , you want to put the variable value as the variable name . In order to do that t[[x]]<-x ( is like t$"temp1" <- temp1 ( where x<-"temp1") – niths4u Nov 09 '17 at 12:59
  • Thanks, but it is still the same. I added sample data into the question, if you define the function, define the data and evaluate the function as in my comment you get the error message, also with the updated function. – deca Nov 09 '17 at 13:02
  • Is it join or merge? which packages are you using , i am getting error trying to load it – niths4u Nov 09 '17 at 13:06
  • It is join - I am using dplyr and plyr – deca Nov 09 '17 at 13:13
  • 1
    Check now the updated code, but your requirements seems to be strange :). Anyway i will leave it to you. – niths4u Nov 09 '17 at 13:18