Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not find function "lc_lct" #62

Open
Robinmoon26 opened this issue Jul 2, 2023 · 3 comments
Open

Could not find function "lc_lct" #62

Robinmoon26 opened this issue Jul 2, 2023 · 3 comments

Comments

@Robinmoon26
Copy link

After I run the function of accrual_plot_predict, it said:

Error in lc_lct(accrual_df, overall, name_overall) :
could not find function "lc_lct"

@aghaynes
Copy link
Member

aghaynes commented Jul 2, 2023

Thanks for the report... could you provide a reprex? See e.g. https://reprex.tidyverse.org for an easy way to produce one

@Robinmoon26
Copy link
Author

Thanks for the report... could you provide a reprex? See e.g. https://reprex.tidyverse.org for an easy way to produce one

Failed to use the reprex, let me show the code I run:

library(accrualPlot)
library(reprex)

data(accrualdemo)
df <- accrual_create_df(accrualdemo$date)

accrual_plot_predict<-function(accrual_df,
                               target,
                               overall=TRUE,
                               name_overall=attr(accrual_df, "name_overall"),
                               fill_up=TRUE,
                               wfun=function(x) seq(1 / nrow(x), 1, by = 1/nrow(x)),
                               col.obs=NULL,
                               lty.obs=1,
                               col.pred="red",
                               lty.pred=2,
                               pch.pred=8,
                               pos_prediction=c("out","in","none"),
                               label_prediction=NULL,
                               cex_prediction=1,
                               format_prediction="%B %d, %Y",
                               show_center=TRUE,
                               design=1,
                               center_label="Centers",
                               center_legend=c("number","strip"),
                               targetc=NA,
                               center_colors=NULL,
                               center_legend_text_size=0.7,
                               ylim=NA,xlim=NA,
                               ylab="Recruited patients",
                               xlabformat="%d%b%Y",
                               xlabn=5,
                               xlabminn= xlabn %/% 2,
                               xlabpos=NA,
                               xlabsrt=45,
                               xlabadj=c(1,1),
                               xlabcex=1,
                               mar=NA,
                               legend.list=NULL,
                               ...,
                               center_start_dates=NULL) {
  
  
  pos_prediction<-match.arg(pos_prediction)
  center_legend<-match.arg(center_legend)
  
  tmp <- lc_lct(accrual_df,
                overall,
                name_overall)
  accrual_df <- tmp$accrual_df
  lc <- tmp$lc
  lct <- tmp$lct
  overall <- tmp$overall
  
  if (!is.na(sum(mar))) {
    stopifnot(length(mar)==4)
  }
  
  if (length(target)!=1) {
    #separate prediction
    if (length(target)!=length(accrual_df)) {
      stop("length of target has to correspond to length of accrual_df")
    } else {
      target<-check_name(target, names(accrual_df))
    }
  }
  
  preddate<-TRUE
  if (is.Date(target)) {
    preddate<-FALSE
    check_date(target)
  }
  
  if (is.null(col.obs)) {
    if (lc==1) {
      col.obs="black"
    } else {
      col.obs<-gray.colors(lc)
    }
    
  }
  
  if (is.null(label_prediction)) {
    if (preddate) {
      label_prediction<-"Predicted end date: "
    } else {
      label_prediction<-"Predicted sample size: "
    }
  }
  
  #predictions
  #&&&&&&&&&&
  
  tmp <- pred_fn(accrual_df,
                 fill_up,
                 wfun,
                 lc,
                 overall,
                 target,
                 name_overall)
  
  if (preddate) {
    end_date <- tmp$end_date
    edate <- tmp$edate
    targetm <- target
  } else {
    end_date <- target
    edate <- max(target)
    targetm <- unlist(tmp$end_date)
  }
  
  adf <- tmp$adf
  
  
  #plot scaling
  #&&&&&&&&&&
  
  alim<-ascale(accrual_df,xlim=xlim,ylim=ylim,ni=xlabn,min.n=xlabminn,addxmax=edate,addymax=targetm)
  
  # modification of ylim if design==3
  if (show_center) {
    if (lc>1 | !is.null(center_start_dates)) {
      if (design==3) {
        if (alim[["ylim"]][1]==0) {
          alim[["ylim"]][1]<--max(targetm)/15
        }
      }
    }
  }
  
  
  #margin
  #&&&&&&&&&&
  
  if (is.na(sum(mar))) {
    
    #mar<-c(5.1,4.1,2.0,1.0)
    mar<-c(5.1,4.1,4.1,2.1)
    
    #centers
    if (show_center) {
      if (lc>1 | !is.null(center_start_dates)) {
        #if (center_legend=="strip") {
        #mar[4]<-2.5
        #}
        if (design==1)
          mar[1]<-6.5
      }
    }
    
  }
  
  
  #plot raw data
  #&&&&&&&&&&
  
  oldpar <- par(mar=mar)
  on.exit(par(oldpar))
  
  plot(0,type="n",ylim=alim[["ylim"]],xlim=alim[["xlim"]],
       axes=FALSE,xlab="",ylab=ylab,...)
  box()
  
  #xlabel:
  xlabsl<-format(alim[["xlabs"]], xlabformat)
  axis(side=1,at=alim[["xlabs"]],labels=rep("",length(alim[["xlabs"]])),...)
  if (is.na(xlabpos)) {
    xlabpos<-par("usr")[3]-(par("usr")[4]-par("usr")[3])/30
  }
  text(x=alim[["xlabs"]],y=xlabpos,srt=xlabsrt,labels=xlabsl,xpd=TRUE,adj=xlabadj,cex=xlabcex)
  
  #ylabel:
  axis(side=2,las=1,...)
  
  #only overall
  if (lc==1 | (overall & length(target)==1)) {
    lines(Cumulative~Date,data=adf,type="s",col=col.obs,lty=lty.obs)
    lp<-adf[which.max(adf$Date),]
    lines(x=c(lp$Date,end_date),y=c(lp$Cumulative,targetm),col=col.pred,lty=lty.pred)
    points(x=end_date,y=targetm,pch=pch.pred,col=col.pred,xpd=TRUE)
    
    #prediction text
    if (preddate) {
      pred_text<-paste0(label_prediction,format(end_date, format_prediction))
    } else {
      pred_text<-paste0(label_prediction,round(targetm,digits=0))
    }
    
    if (pos_prediction!="none") {
      if (pos_prediction=="in") {
        legend("topleft",pred_text,bty="n",cex=cex_prediction)
      } else {
        text(x=par("usr")[1],y=par("usr")[4],adj=c(0,-1), xpd=TRUE,pred_text,cex=cex_prediction)
      }
    }
    
  } else {
    #for each site:
    targetm<-mult(targetm,length(adf))
    col.obs<-mult(col.obs,length(adf))
    lty.obs<-mult(lty.obs,length(adf))
    col.pred<-mult(col.pred,length(adf))
    lty.pred<-mult(lty.pred,length(adf))
    pch.pred<-mult(pch.pred,length(adf))
    
    for (k in 1:length(adf)) {
      
      lines(Cumulative~Date,data=adf[[k]],type="s",col=col.obs[k],lty=lty.obs[k])
      lp<-adf[[k]][which.max(adf[[k]]$Date),]
      lines(x=c(lp$Date,end_date[[k]]),y=c(lp$Cumulative,targetm[k]),col=col.pred[k],lty=lty.pred[k])
      points(x=end_date[[k]],y=targetm[k],pch=pch.pred[k],col=col.pred[k],xpd=TRUE)
    }
    if (preddate) {
      lna<-paste0(names(adf),": ",format(do.call("c",end_date), format_prediction))
    } else {
      lna<-paste0(names(adf),": ",round(targetm, digits=0))
    }
    
    if(!is.null(legend.list)) {
      ll<-legend.list
      #defaults if not given:
      vlist<-c("x","legend","ncol","col","lty","bty","y.intersp","seg.len")
      obslist<-list("topleft",lna,1,col.obs,lty.obs,"n",0.85,1.5)
      for (d in 1:length(vlist)) {
        if (is.null(ll[[vlist[d]]])) {
          ll[[vlist[d]]]<-obslist[[d]]
        }
      }
    } else {
      ll<-list(x = "topleft",legend = lna,ncol=1,col=col.obs,lty=lty.obs,bty="n",y.intersp=0.85,seg.len=1.5)
    }
    do.call("legend",ll)
    
  }
  
  
  
  #plot centers info
  #&&&&&&&&&&
  
  if (show_center) {
    if (lc>1 | !is.null(center_start_dates)) {
      
      plot_center(accrual_df=accrual_df,
                  center_start_dates=center_start_dates,
                  overall=overall,name_overall=name_overall,
                  lc=lc,lct=lct,design=design,
                  center_legend=center_legend,center_colors=center_colors,targetc=targetc,
                  center_label=center_label,center_legend_text_size=center_legend_text_size)
    }
    
  }
}
accrual_plot_predict(accrual_df=df,target=300)

@aghaynes
Copy link
Member

aghaynes commented Jul 4, 2023

many of the functions you use in there are internal to the package (e.g. pred_fn, check_date to name two from the first few lines). Why are you not just using the function from within the package? If there is some customisation that you've done, we could consider incorporating it as an option perhaps.

To use internal functions, you would have to prefix the function name with the packagename: e.g. accrualPlot:::lc_lct(...), but this is not recommended behaviour in R generally as there is no guarantee that internal functions will behave consistently through time (updates to packages might modify the behaviour of internal functions to provide new functionality or if a new/better method has become available, this is why they're internal rather than exported...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants