0

I am trying to make plots using a function in which arguments are values of dataframes.

seniorPlot <- function(validityDate, seniorTotal, color){
  par(new = T)
  plot(validityDate,seniorTotal,
       type = "l",
       lwd = 2.5,
       xlim = c(date_debut$validityDate,date_fin$validityDate),
       ylim = c(1,nmax$seniorTotal),
       col = color,
       xlab = "",
       ylab = "",
       xaxt = "n",
       yaxt = "n",
       bty = "n",
  )
}

For the purpose, the threee arguments of the function are results several dataframes 'seniorList' and the seniors df named with the senior initials ('AM', 'FB', 'GM'…)

To describe, you can see the senior dataframes below:

seniorList <- data.frame(seniorValidation=c('AM', 'FB', 'GM'),seniorTotal=c(72, 154, 137))
AM <- data.frame(validationDate=c('2022-01-25', '2022-01-26'), color=c('brown','brown')
FB <- data.frame(validationDate=c('2022-01-20', '2022-01-30'), color=c('green','green')
GM <- data.frame(validationDate=c('2022-01-24', '2022-01-28'), color=c('blue','blue')

Obviously, there is more than 3 lines in the seniorList, so I want to do a loop, using the value of the first column of seniorList (ie. the senior name) to call the write dataframe.

And, here is the issue : how can I convert the result of noquote(paste(noquote(seniorList[i,1]),'$validityDate', sep = '')) to the result '2022-01-25' (if i = 1)

for (i in 1:nrow(seniorList)) {
seniorPlot(validityDate = noquote(paste(noquote(seniorList[i,1]),'$validityDate', sep = '')),
              seniorTotal = noquote(paste(noquote(seniorList[i,1]),'$seniorTotal', sep = '')),
              color = noquote(paste(noquote(seniorList[i,1]),'$color', sep = '')))
}

Thank you for your help, I hope my english is easy to understand.

noquote(paste(noquote(seniorList[i,1]),'$validityDate', sep = '')) give AM$validityDate and not its result

a6tole
  • 13
  • 2
  • Put your data.frames into the list. Or, even better, combine everything into one data.frame. – Roland Dec 06 '22 at 11:39
  • you can also use `with` to refer to names defined within a `list` or a `data.frame`. – Stefano Barbi Dec 06 '22 at 11:43
  • @Roland, can I plot several curves according to different values in a df ? I mean, I have the global df with all the seniors, the validaty date but how can I plot one curve per senior ? – a6tole Dec 06 '22 at 11:50
  • @DaveArmstrong : Error in deparse… unused argument – a6tole Dec 06 '22 at 11:51
  • 3
    @DaveArmstrong That's bad practice and unnecessary. It would be better to use `get` if OP insists on going forward with their current approach, which is also not good practice but not as bad as `eval(parse())`.. – Roland Dec 06 '22 at 11:51
  • @a6tole Do you know how to subset a data.frame programmatically? – Roland Dec 06 '22 at 11:52
  • It looks like your actual goal is a line plot? Is there a reason why you don't use ggplot2? – Roland Dec 06 '22 at 11:55
  • @Roland to subset, you mean newDF <- subset(seniorList, seniorList$seniorValidation == 'AM') ? ggplot2: yes I want to make several lines (like a survival curve) and I have no idea on how to use ggplot2 but I am curious! – a6tole Dec 06 '22 at 11:59
  • @Roland, you're right. Just to close the loop on this, [here](https://stackoverflow.com/questions/13649979/what-specifically-are-the-dangers-of-evalparse) is the SO answer on why `eval(parse())` is bad. I do still use it occasionally in a pinch, but probably shouldn't be promoting that solution. My apologies. I deleted the comment that suggested it. – DaveArmstrong Dec 06 '22 at 13:29

1 Answers1

0

It's unclear what exactly the plot should look like but this shows how use "tidy" data easily with ggplot2.

DF <- rbind(cbind(AM, data.frame(seniorTotal = 72, seniorValidation = "AM")),
            cbind(FB, data.frame(seniorTotal = 154, seniorValidation = "GB")),
            cbind(GM, data.frame(seniorTotal = 137, seniorValidation = "GM")))
DF$validationDate <- as.Date(DF$validationDate)
#this is how your data should look like:
print(DF)

library(ggplot2)
ggplot(data = DF, aes(x = validationDate, y = seniorTotal, color = seniorValidation)) +
  geom_line()
Roland
  • 127,288
  • 10
  • 191
  • 288
  • It is really nice with ggplot. I confess that each senior df are extracted from a global df so it is much easier to work from the global dataframe. – a6tole Dec 06 '22 at 13:09