Home > Back-end >  R askYesNo function: print a variable which is a list strings
R askYesNo function: print a variable which is a list strings

Time:01-11

I'm a complete newbie in programming with R and stuck at what I believe is actually a very simple question. I've borrowed some code snippets and put them together and everything seems to work, just printing of the package names which has to be installed from GitHub doesn't..

How can I print a Variable which is a list together with strings in the askyesno function. I tried {},[] and doubled them, tried "" and .format as in python, nothing worked. In the following my Code, please help :)

not_installed = my_packages[!(my_packages %in% installed.packages()[ , "Package"])]
if(length(not_installed)) install.packages(not_installed)

if(length(not_installed != installed.packages()))
still_not_installed = list(not_installed)
Ask = askYesNo("$still_not_installed   cannot be install from CRAN. \n Load from GitHub?", 
               default = TRUE, prompts = getOption("askYesNo"), gettext(c("Yes", "No", "Cancel")))

if(Ask == TRUE)        
  p_load_gh("muschellij2/aal", "taiyun/corrplot/blob/master/R/corrplot-package.R",
             install = TRUE, dependencies = TRUE)

Do you think this is a proper solution to search for not installed packages and load them?

CodePudding user response:

Your approach to checking for install is not ideal in itself. It will not detect if a package is missing dependencies for example. We can use require which automatically checks if the package is actually usable. Then we can just build the message with paste. I assume you will also paste the package into your p_load_gh function, but I don't know the syntax of that particular one.

my_packages <- c("test","test2")
for(p in my_packages)
{
  tryCatch(test <- require(p,character.only=T), 
                warning=function(w) return())
  if(!test)
  {
    print(paste("Package", p, "not found. Installing Package!"))
    install.packages(p)
  }
  tryCatch(test <- require(p,character.only=T), 
                warning=function(w) return())
  if(!test)
  {
    Ask = askYesNo(paste("Package", p," not installable from CRAN. \n Load from GitHub?", default = TRUE, prompts = getOption("askYesNo"), gettext(c("Yes", "No", "Cancel")))
   if(Ask) p_load_gh("muschellij2/aal", "taiyun/corrplot/blob/master/R/corrplot-package.R",
             install = TRUE, dependencies = TRUE)
  }
}

CodePudding user response:

You can build a message string with paste. toString will nicely concatenate and comma-separate a vector, and then we can paste than on to the rest of your message:

Ask = askYesNo(
  msg = paste(toString(still_not_installed),
              "cannot be install from CRAN. \n Load from GitHub?"), 
  default = TRUE,
  prompts = getOption("askYesNo"),
  gettext(c("Yes", "No", "Cancel"))
)

I think you've got a bigger issue. Line 1: You get the subset of my_packages that are not installed, good. Line 2: you try to install them, fine. Line 3: this is bad. != does element-wise comparison - you're testing if the first not_installed package is not equal to the first installed package (alphabetically), then comparing the second to the second, etc. And then you're testing if the resulting boolean vector has any length---which it will. Instead I would suggest updating the not_installed list, just repeat Line 1 to get the update the list of uninstalled packages. And you don't need to list() them, keep them as a character vector.

Also, we should nest the attempted if(Ask == TRUE) inside the if() of needing github packages at all.

not_installed = my_packages[!(my_packages %in% installed.packages()[ , "Package"])]
if(length(not_installed)) install.packages(not_installed)

still_not_installed = my_packages[!(my_packages %in% installed.packages()[, "Package"])]
if(length(still_not_installed)) {
  Ask = askYesNo(
    msg = paste(toString(still_not_installed),
                "cannot be install from CRAN. \n Load from GitHub?"), 
    default = TRUE,
    prompts = getOption("askYesNo"),
    gettext(c("Yes", "No", "Cancel"))
  )
  if(Ask == TRUE) {       
    # This code could still be improved, it assumes if we get to
    # this point that both packages are missing, but it might 
    # only be one of them...
    p_load_gh(
      "muschellij2/aal", 
      "taiyun/corrplot/blob/master/R/corrplot-package.R",
      install = TRUE, dependencies = TRUE)
    )
  }
}
  •  Tags:  
  • Related