How to put a delay inside a function in R?

Asked

Viewed 63 times

1

Hello! I would like to put a delay within a function that I am creating to join N-graphics. I realized that in some cases, my function does not expect the eval(parse(text=paste())) generate all lists, and already jumps to the next line of code within the function. ( I use the eval(parse(text)))to generate sequences of executable lines, because sometimes I need to return to an information within an object, which due to lack of knowledge of mine, I cannot accomplish using some loop).

Unfortunately, the function that results in the error is very long, and the error only appears when using the complete database, so I created a generic function to try to explain, where would enter the delay.

Packages used:

pacs<-{
  pacotes<-c("ggplot2","ggpmisc","ggpubr")
    eval(parse(text=paste('if(!require(',pacotes,')){install.packages("',pacotes,'")}',sep="")))
}

Database

base<-structure(list(FAT1=c("T19","T19","T19","T19","T19","T19",
"T19","T19","T19","T19","T19","T19","T19","T19","T19",
"T19","T19","T19","T19","T19","T19","T19","T19","T19"
),VARY=c(0.626,0.299,0.658,0.954,0.52,0.445,0.61,0.649,
0.348,0.599,0.338,0.295,0.589,0.449,0.522,0.76,0.664,
0.416,0.522,0.512,0.353,0.623,0.563,0.249),VARX=c(0.631,
0.3,0.635,0.933,0.514,0.466,0.572,0.653,0.348,0.605,
0.345,0.273,0.597,0.457,0.516,0.748,0.684,0.432,0.532,
0.517,0.351,0.633,0.581,0.227)),class="data.frame",row.names=c(NA,
24L))

Function:

plota.tudo<-function(base,N){
  minha.formula<-y~I(x)+I(x^2)+I(x^3)

  eq<-function(minha.formula){
    stat_poly_eq(formula = minha.formula,eq.with.lhs = "italic(hat(Y))~`=`~",aes(label = paste(..eq.label.., ..rr.label..,  sep =  "*plain(\",\")~")), parse = TRUE)}

  eval(parse(text=paste('graf',1:N,'<-ggplot(base,aes(x=VARX,y=VARY))+geom_point()+geom_smooth(formula=minha.formula,method = "lm",se=FALSE,color="black",span=.8)+eq(minha.formula)',sep="")))
  # Local para haver o atraso!!! 
  # pois eu preciso que ele crie todos os arquivos de graficos
  eval(parse(text=paste('grafico<-annotate_figure(ggarrange(graf1,',paste(paste('graf',2:N,sep=""),c(rep(",",N-2),""),collapse=" ",sep=""),
                        ', common.legend = T),',
                        'left = text_grob("VARIAVEL X",rot=90,size = 12),',
                        'bottom = text_grob("VARIAVEL Y",size=12))',sep="")))
  print(grafico)}

# plota.tudo(base,N=quantidade de graficos a ser gerada)

plota.tudo(base,10)
  • About the code, I would check the need to assign the graphics as mid-object dynamically and not just return them in a list with purrr::map() or something like

  • The situations we really need to use eval(parse(text = ...)) are very rare

  • 2

    See if aes_string() cannot help you uncomplicate the code

1 answer

1


The function that includes an interval between the execution of one command and another in the R is Sys.sleep(). The argument passed is time, in seconds, that the R must "sleep".

plota.tudo<-function(base,N){
  minha.formula<-y~I(x)+I(x^2)+I(x^3)

  eq<-function(minha.formula){
    stat_poly_eq(formula = minha.formula,eq.with.lhs = "italic(hat(Y))~`=`~",aes(label = paste(..eq.label.., ..rr.label..,  sep =  "*plain(\",\")~")), parse = TRUE)}

  eval(parse(text=paste('graf',1:N,'<-ggplot(base,aes(x=VARX,y=VARY))+geom_point()+geom_smooth(formula=minha.formula,method = "lm",se=FALSE,color="black",span=.8)+eq(minha.formula)',sep="")))
  # Local para haver o atraso!!! 
  # pois eu preciso que ele crie todos os arquivos de graficos
  Sys.sleep(1)

  eval(parse(text=paste('grafico<-annotate_figure(ggarrange(graf1,',paste(paste('graf',2:N,sep=""),c(rep(",",N-2),""),collapse=" ",sep=""),
                        ', common.legend = T),',
                        'left = text_grob("VARIAVEL X",rot=90,size = 12),',
                        'bottom = text_grob("VARIAVEL Y",size=12))',sep="")))
  print(grafico)
}

I did not test its function, since it was not the subject of the question, only includes the interval of a second in the indicated location.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.