Skip to content
Browse files

New functions:

* get.installed.R.folders - Returns folder names with R installations
* copy.packages.between.libraries - Copies all packages from one library folder to another

Updated functions:
* update.R  - major update!!!
* ask.user.for.a.row - new "questions_text" parameter

More documentation for functions.
  • Loading branch information...
1 parent e65a2c4 commit c0db04a01c06cbcbb7105ba7e422aa9eda326a3c @talgalili committed
View
4 NAMESPACE
@@ -1,4 +1,8 @@
+export(check.for.updates.R)
+export(copy.packages.between.libraries)
+export(create.global.library)
export(file.name.from.url)
+export(get.installed.R.folders)
export(install.git)
export(install.GitHub)
export(install.MikTeX)
View
23 NEWS
@@ -1,3 +1,24 @@
+installR 0.6
+------------
+
+NEW FUNCTIONS ADDED:
+ * get.installed.R.folders - Returns folder names with R installations
+ * copy.packages.between.libraries - Copies all packages from one library folder to another
+
+UPDATED FUNCTIONS:
+ * update.R - major update. Now the user can copy his packages from the old R version to the new version.
+ * ask.user.for.a.row - new "questions_text" parameter
+
+OTHER NOTES:
+ * Updated the NEWS
+ * More documentation
+
+TODO for future releases:
+ * uninstall.R() for the old version of R (including the library folder)
+ * Better integration with the "global library" strategy
+
+
+
installR 0.3-0.5
----------------
@@ -20,7 +41,7 @@ OTHER NOTES:
* Updated the description file
* Changed the name of the package from installR to installr.
* Added a README.md
- * Added dcoumentation to all the functions in install.r (via roxygen2)
+ * Added documentation to all the functions in install.r (via roxygen2)
installR 0.2
View
236 R/create.global.library.r
@@ -43,7 +43,13 @@
-
+#' @title Creates a global library folder
+#' @description Creates a global library folder (above the folder R is currently installed in)
+#' @param global_library_folder the path of the new global library folder to create. If missing, will be set to R_path/R/library. (for example: "C:/Program Files/R/library")
+#' @return TRUE/FALSE if we created a new folder or not.
+#' @export
+#' @examples
+#' create.global.library()
create.global.library <- function(global_library_folder)
{
# global_library_folder = "C:/Program Files/R/library"
@@ -75,6 +81,9 @@ create.global.library <- function(global_library_folder)
+
+
+
xx.global.library <- function(
copy_packges_to_global_library_folder = F, # should be T if this is the first time you are running this (either from the old or the new version of R you have just installed).
# copy_packages_from_old_R_installation = F, # should be T if you are running this for the first time, from a new installation of R, after having an old installation of R (where all of your old packages are)
@@ -274,6 +283,231 @@ xx.global.library <- function(
+Old.R.RunMe <- function(global.library.folder = NULL, quit.R = NULL)
+{
+ # global.library.folder = "C:/Program Files/R/library"
+ # global.library.folder is null then if we assume it is of the shape: # "C:/Program Files/R/library"
+
+ if(is.null(global.library.folder))
+ {
+ # finding the parent directoy of R (into which we will add the library directory)
+ if(grepl("/", R.home(), fixed = T))
+ { R_parent_lib <- paste(head(strsplit(R.home(), "/", fixed = T)[[1]], -1), collapse = "/") }
+ if(grepl("\\", R.home(), fixed = T))
+ { R_parent_lib <- paste(head(strsplit(R.home(), "\\", fixed = T)[[1]], -1), collapse = "/") }
+ # if global.library.folder isn't defined, then we assume it is of the form: "C:\\Program Files\\R\\library"
+ global.library.folder <- paste(R_parent_lib, "/library", sep = "")
+ }
+
+ # checking that the global lib folder exists - and if not -> create it.
+
+ if(!file.exists(global.library.folder))
+ { # If global lib folder doesn't exist - create it.
+ dir.create(global.library.folder)
+ cat(paste("The path:" , global.library.folder, "Didn't exist - and was now created.","\n"))
+ } else {
+ cat(paste("The path:" , global.library.folder, "already exist. (no need to create it)","\n"))
+ }
+
+
+ cat("-----------------------","\n")
+ cat("I am now copying packages from old library folder to:","\n")
+ cat(global.library.folder,"\n")
+ cat("-----------------------","\n")
+ flush.console() # refresh the console so that the user will see the massage
+
+ # Copy packages from current lib folder to the global lib folder
+ list.of.dirs.in.lib <- NULL
+ number_of_lib_dir <- length(.libPaths())
+ for(i in seq_len(number_of_lib_dir))
+ {
+ # this is good in case we already used some externel library folder, and we wish to move it...
+ list.of.dirs.in.lib <- c(list.of.dirs.in.lib ,
+ paste( .libPaths()[i],"/", list.files(.libPaths()[i]),sep = "")
+ )
+ }
+ # list.files(.libPaths())
+ # list.of.dirs.in.lib <- paste( paste(R.home(), "\\library\\", sep = ""),
+ # list.files(paste(R.home(), "\\library\\", sep = "")),
+ # sep = "")
+
+ folders.copied <- file.copy(from = list.of.dirs.in.lib, # copy folders
+ to = global.library.folder,
+ overwrite = TRUE,
+ recursive =TRUE)
+
+
+
+ cat("Success.","\n")
+ cat(paste("We finished copying all of your packages (" , sum(folders.copied), "packages ) to the new library folder at:"),"\n")
+ cat(global.library.folder,"\n")
+ cat("-----------------------","\n")
+
+
+
+ # To quite R ?
+ if(is.null(quit.R))
+ {
+ cat("Can I close R? y(es)/n(o) (WARNING: your enviornment will *NOT* be saved)","\n")
+ answer <- readLines(n=1)
+ } else {
+ answer <- quit.R
+ }
+ if(tolower(answer)[1] == "y") quit(save = "no")
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+New.R.RunMe <- function (global.library.folder = NULL,
+ quit.R = F,
+ del.packages.that.exist.in.home.lib = F,
+ update.all.packages = T)
+
+{
+
+
+ if(is.null(global.library.folder))
+ {
+ # finding the parent directoy of R (into which we will add the library directory)
+ if(grepl("/", R.home(), fixed = T))
+ { R_parent_lib <- paste(head(strsplit(R.home(), "/", fixed = T)[[1]], -1), collapse = "/") }
+ if(grepl("\\", R.home(), fixed = T))
+ { R_parent_lib <- paste(head(strsplit(R.home(), "\\", fixed = T)[[1]], -1), collapse = "/") }
+ # if global.library.folder isn't defined, then we assume it is of the form: "C:\\Program Files\\R\\library"
+ global.library.folder <- paste(R_parent_lib, "/library", sep = "")
+ }
+
+
+ # checking that the global lib folder exists - and if not -> create it. (happens if it is the first time running New.R.RunMe)
+ if(!file.exists(global.library.folder))
+ { # If global lib folder doesn't exist - create it.
+ dir.create(global.library.folder)
+ cat(paste("The path to the Global library (" , global.library.folder, ") Didn't exist - and was now created."),"\n")
+ } else {
+ cat(paste("The path to the Global library (" , global.library.folder, ") already exist. (NO need to create it)"),"\n")
+ }
+
+ flush.console() # refresh the console so that the user will see the massage
+
+
+
+
+
+ # Based on:
+ # help(Startup)
+ # checking if "Renviron.site" exists - and if not -> create it.
+ Renviron.site.loc <- paste(R.home(), "\\etc\\Renviron.site", sep = "")
+ if(!file.exists(Renviron.site.loc))
+ { # If "Renviron.site" doesn't exist (which it shouldn't be) - create it and add the global lib line to it.
+ cat(paste("R_LIBS='",global.library.folder, "'\n",sep = "") ,
+ file = Renviron.site.loc)
+ cat(paste("The file:" , Renviron.site.loc, "Didn't exist - we created it and added your 'Global library link' (",global.library.folder,") to it.\n"))
+ } else {
+ cat(paste("The file:" , Renviron.site.loc, "existed and we could NOT add some lines! make sure you add the following line by yourself:","\n"))
+ cat(paste("R_LIBS=",global.library.folder,"\n", sep = "") )
+ cat(paste("To the file:",Renviron.site.loc,"\n"))
+ }
+
+
+
+ # Setting the global lib for this session also
+ .libPaths(global.library.folder) # This makes sure you don't need to restart R so that the new Global lib settings will take effect in this session also
+ # .libPaths(new="C:/PROGRA~1/R/library")
+ # .libPaths()
+ # This line could have also been added to:
+
+ # /etc/Rprofile.site
+
+ # and it would do the same thing as adding "Renviron.site" did
+
+ cat("Your library paths are: ","\n")
+ cat(.libPaths(),"\n")
+ flush.console() # refresh the console so that the user will see the massage
+
+
+
+
+
+ if(del.packages.that.exist.in.home.lib)
+ {
+ cat("We will now delete package from your Global library folder that already exist in the local-install library folder","\n")
+ flush.console() # refresh the console so that the user will see the massage
+ package.to.del.from.global.lib <- paste( paste(global.library.folder, "/", sep = ""),
+ list.files(paste(R.home(), "\\library\\", sep = "")),
+ sep = "")
+ number.of.packages.we.will.delete <- sum(list.files(paste(global.library.folder, "/", sep = "")) %in% list.files(paste(R.home(), "\\library\\", sep = "")))
+ deleted.packages <- unlink(package.to.del.from.global.lib , recursive = TRUE) # delete all the packages from the "original" library folder (no need for double folders)
+
+ cat(paste(number.of.packages.we.will.delete,"Packages where deleted."),"\n")
+ }
+
+
+
+ if(update.all.packages)
+ {
+ # Based on:
+ # http://cran.r-project.org/bin/windows/base/rw-FAQ.html#What_0027s-the-best-way-to-upgrade_003f
+ cat("We will now update all your packges \n")
+ flush.console() # refresh the console so that the user will see the massage
+ update.packages(checkBuilt=TRUE, ask=FALSE)
+ }
+
+ # To quite R ?
+
+ if(quit.R) quit(save = "no")
+
+}
+
+
+
+
+
+
+
+
+
+
+
+# Run this in the old R version:
+
+#Old.R.RunMe()
+
+# It will:
+
+# 1. Create a new global library folder (if it doesn't exist)
+
+# 2. Copy to the new "global library folder" all of your packages from the old R installation
+
+
+
+# Run this in the New R version:
+
+#New.R.RunMe()
+
+# It will:
+
+# 1. Create a new global library folder (if it doesn't exist)
+
+# 2. Premenantly point to the Global library folder
+
+# 3. Make sure that in the current session - R points to the "Global library folder"
+
+# 4. Delete from the "Global library folder" all the packages that already exist in the local library folder of the new R install
+
+# 5. Update all packages.
+
View
19 R/install.r
@@ -143,16 +143,25 @@ check.integer <- function(N){
#' @details
#' This function is used in \code{installr} when we are not sure what version of the software to download, or when various actions are available for the user to choose from.
#' If the user doesn't give a valid row number, the function repeats its questions until a valid row number is chosen (or the user escapes)
-#' @param TABLE a data.frame table with rows from which we wish the user to choose a row.
-#' @param header_text the text the users sees (often a question) - explaining which row he should choose from
+#' @param TABLE a data.frame table with rows from which we wish the user to choose a row. If TABLE is not a data.frame, it will be coerced into one.
+#' @param header_text the text the users sees (often a question) as a title for the printed table - explaining which row he should choose from
+#' @param questions_text the question the users see after the printing of the table - explaining which row he should choose from
#' @return The row number the user has choosen from the data.frame table.
#' @source On how to ask the user for input: \link{http://stackoverflow.com/questions/5974967/what-is-the-correct-way-to-ask-for-user-input-in-an-r-program}
#' @examples
#' version_table <- data.frame(versions = c("devel", "V 1.0.0", "V 2.0.0"))
#' installr:::ask.user.for.a.row(version_table)
-ask.user.for.a.row <- function(TABLE, header_text = "Possible versions to download (choose one)") {
+ask.user.for.a.row <- function(TABLE,
+ header_text = "Possible versions to download (choose one)",
+ questions_text = "Please review the table of versions from above, \n and enter the row number of the file-version you'd like to install: ") {
# http://stackoverflow.com/questions/5974967/what-is-the-correct-way-to-ask-for-user-input-in-an-r-program
# based on code by Joris Meys
+ if(class(TABLE) != "data.frame") {
+ TABLE <- as.data.frame(TABLE)
+ colnames(TABLE)[1] <- "Choose:"
+ }
+
+ rownames(TABLE) <- seq_len(nrow(TABLE))# makes sure that the table's row names are in the "rownames"
correct_input <- F
nrow_TABLE <- nrow(TABLE)
@@ -160,14 +169,14 @@ ask.user.for.a.row <- function(TABLE, header_text = "Possible versions to downlo
while(!correct_input){# n is the row number from the user
cat("=============",header_text,"====================\n")
print(TABLE)
- ROW_id <- readline("Please review the table of versions from above, \n and enter the row number of the file-version you'd like to install: ")
+ ROW_id <- readline(questions_text)
ROW_id <- as.numeric(ROW_id)
correct_input <-
!is.na(ROW_id) && # don't check other condition if this is not met.
check.integer(ROW_id) & # is integer AND
ROW_id >= 1 & # make sure our ROW_id is within range (between 1 and the number of rows in the table)
ROW_id <= nrow_TABLE
- if(!correct_input) cat("Wrong input: Please enter a valid number (integer, between 1 to the number of rows) \n for the row number for the file you'd like to install\n")
+ if(!correct_input) cat("Wrong input: Please enter a valid number (integer, between 1 to the number of rows) \n for the row number of your choice\n")
# if(is.na(n)){break} # breaks when hit enter
}
View
336 R/update.R.r
@@ -1,3 +1,50 @@
+#' @title Checks if there is a newer version of R
+#' @description Fetches the latest (not development!) R version and compares it with your currently installed R version (the version of the R session from which you are running this function).
+#' @param notify_user if to print to you (the user) what is the latest version and what version you are currently using.
+#' @param page_with_download_url the URL of the page from which R can be downloaded.
+#' @return TRUE/FALSE - if there is a newer version of R to install or not.
+#' @export
+#' @examples
+#' check.for.updates.R()
+#' # Possible output:
+#' # There is a newer version of R for you to download!
+#' # You are using R version: 2.15.0
+#' # And the latest R version is: 2.15.3
+#' # [1] TRUE
+check.for.updates.R <- function(notify_user = T,
+ page_with_download_url = "http://cran.rstudio.com/bin/windows/base/") {
+ page <- readLines(page_with_download_url, warn = FALSE)
+ pat <- "R-[0-9.]+-win";
+ target_line <- grep(pat, page, value = TRUE);
+ m <- regexpr(pat, target_line);
+ latest_R_version <- regmatches(target_line, m)
+ latest_R_version <- gsub(pattern="R-|-win" ,"", latest_R_version) # remove junk text
+
+ current_R_version <- paste(R.version$major, R.version$minor, sep=".")
+
+ # Turn the version character into a number
+ latest_R_version_long <- turn.version.to.number(latest_R_version)
+ current_R_version_long <- turn.version.to.number(current_R_version)
+
+ there_is_a_newer_version <- current_R_version_long < latest_R_version_long # TRUE = there IS a need to update (since the latest version is higher then what we currently have)
+
+ if(notify_user) {
+ if(there_is_a_newer_version) {
+ cat("There is a newer version of R for you to download!\n")
+ cat("You are using R version: ", current_R_version, "\n")
+ cat("And the latest R version is: ", latest_R_version, "\n")
+ } else {# we are not using the latest R version - now what...
+ # tell it to the user:
+ cat("No need to update - you are using the latest R version: ", R.version$version.string , "\n")
+ }
+ }
+
+ there_is_a_newer_version
+}
+
+
+
+
#' @title Downloads and installs the latest R version
#' @description Fetches the latest (not development!) R version
#' @details
@@ -28,7 +75,15 @@ install.R <- function(page_with_download_url = "http://cran.rstudio.com/bin/wind
# R.version$version.string
}
-turn.version.to.number <- function(version_with_dots) {
+
+
+
+#' @title Turns version to number (for 1 value only)
+#' @param version_with_dots A character value - of the version of R (for example 2.15.2)
+#' @return A "number" representation of the version (for example: 2015002)
+#' @examples
+#' turn.version.to.number1("2.15.2")
+turn.version.to.number1 <- function(version_with_dots) {
# version_with_dots is a character of the form xx.xx.xx
version_with_dots_numbers <- as.numeric(strsplit(version_with_dots, "\\.")[[1]] )
version_with_dots_major <- version_with_dots_numbers[1]
@@ -41,9 +96,31 @@ turn.version.to.number <- function(version_with_dots) {
version_with_dots_long
}
+
+
+#' @title Turns version to number (for a vector of values)
+#' @param version_with_dots A character vector - of the version of R (for example 2.15.2)
+#' @return A vector of "numbers" representing the versions (for example: 2015002). The names of the vector is the original version character.
+#' @examples
+#' turn.version.to.number(c("2.15.2", "2.15.2"))
+turn.version.to.number <- function(version_with_dots) {
+ if(length(version_with_dots) > 1) {
+ return(sapply(version_with_dots, turn.version.to.number1))
+ } else {# we have just one element in this vector
+ return(turn.version.to.number1(version_with_dots) )
+ }
+}
+
+
# turn.version.to.number("2.15.11") # ---> 2015011
# number_to_dots = 2015011
+#' @title Turns a vector of version-numbers back to version-character
+#' @param number_to_dots A numeric vector - of the number-version of R
+#' @return A vector of "numbers" representing the versions (for example: 2015002). The names of the vector is the original version character.
+#' @examples
+#' turn.number.version(turn.version.to.number(c("2.15.2", "2.15.2")))
+#' turn.number.version(2015011) # "2.15.11"
turn.number.version <- function(number_to_dots) {
# I'm not really using this...
number_to_dots_1 <- trunc(number_to_dots/1000000)
@@ -52,65 +129,191 @@ turn.number.version <- function(number_to_dots) {
number_to_dots_3 <- number_to_dots - number_to_dots_2*1000
paste(number_to_dots_1, number_to_dots_2, number_to_dots_3, sep =".")
}
-# turn.number.version(2015011) # "2.15.11"
-check.for.updates.R <- function(notify_user = T,
- page_with_download_url = "http://cran.rstudio.com/bin/windows/base/") {
- page <- readLines(page_with_download_url, warn = FALSE)
- pat <- "R-[0-9.]+-win";
- target_line <- grep(pat, page, value = TRUE);
- m <- regexpr(pat, target_line);
- latest_R_version <- regmatches(target_line, m)
- latest_R_version <- gsub(pattern="R-|-win" ,"", latest_R_version) # remove junk text
+
+
+
+
+
+
+
+
+
+
+
+#' @title Returns folder names with R installations
+#' @description
+#' The function finds the folders where there are R installations. This is important for deciding what to uninstall, and where from and to to move libraries.
+#' This function ignores installations of R-devel at this point.
+#' Also, this function is based on only looking at the folders above the current installation of R. If there are other isntallations of R outside the above folder, they will not be listed.
+#' @param sort_by_version should the returned vector be sorted by the version number? (default is yes - so that the first element is of the newest version of R) should the user be given the option to choose between which two libraries to copy the packages? If FALSE (default), the folders are copied from the before-newest R installation to the newest R installation.
+#' @param add_version_to_name should the version number be added to the vector of folders? (default is yes)
+#' @return Returns a character vector (possibly named, possibly sorted) of the folders where there are R installations.
+#' @export
+#' @seealso \link{copy.packages.between.libraries}
+#' @examples
+#' get.installed.R.folders() # returns the sorted and named vector of folder names where R is installed (in different versions). The first element is the folder of the newest version of R.
+#' get.installed.R.folders(F, F) # returns the folder names where R is installed (in different versions) - no sorting of the folder names was performed
+get.installed.R.folders <- function(sort_by_version = T, add_version_to_name = T) {
+ # get the parent folder of the current R installation
+ R_parent_folder <- paste(head(strsplit(R.home(), "/|\\\\")[[1]], -1), collapse = "/") # the strsplit is seperating the path whether it is / or \\ (but since \\ is a problem, I need to cancel it with \\\\)
+ items_in_R_parent_folder <- list.files(R_parent_folder)
+ ss_R_subfolders_in_R_parent_folder <- grepl("R-[0-9]+.[0-9]+.[0-9]+$", items_in_R_parent_folder)
+ # notice the use of $ at the end of the regex
+ # Good regex syntax http://laurikari.net/tre/documentation/regex-syntax/
+ R_subfolders <- items_in_R_parent_folder[ss_R_subfolders_in_R_parent_folder]
- current_R_version <- paste(R.version$major, R.version$minor, sep=".")
+ R_folders <- file.path(R_parent_folder, R_subfolders) # a vector with the full path folders of all of the R installations (Assuming they are all on the same folder)
+ R_folders_versions <- gsub("R-", "", R_subfolders)
+ R_folders_versions_number <- turn.version.to.number(R_folders_versions)
- # Turn the version character into a number
- latest_R_version_long <- turn.version.to.number(latest_R_version)
- current_R_version_long <- turn.version.to.number(current_R_version)
+ if(add_version_to_name) names(R_folders) <- R_folders_versions
- there_is_a_newer_version <- current_R_version_long < latest_R_version_long # TRUE = there IS a need to update (since the latest version is higher then what we currently have)
+ if(sort_by_version) {
+ ss_order_R_folders_by_version <- order(R_folders_versions_number,decreasing=T)
+ R_folders <- R_folders[ss_order_R_folders_by_version]
+ } # else - if not - just return the order of the folders as they where extracted
+ return(R_folders)
+}
+
+
+
+
+
+#' @title Copies all packages from one library folder to another
+#' @export
+#' @description
+#' Copies all packages from one folder to another. This function is used if we wish to either:
+#' \itemize{
+#' \item Upgrade R to a new version - and copy all of the packages from the old R installation to the new one.
+#' \item Move to a global library system - and wanting to copy all of packages from the local library folder to the global one
+#' }
+#' It takes into account that we don't want to copy packages which have "high" importance (such as MASS, boot, graphics, utils, rpart, Matrix and more GREAT packages...) to the new library folder.
+#' Also, it assumes that within an R installation, the packages are located inside the "library" folder.
+#' @param from a character vector for the location of the old library folder FROM which to copy files from.
+#' @param to a character vector for the location of the old library folder TO which to copy files to.
+#' @param ask should the user be given the option to choose between which two libraries to copy the packages? If FALSE (default), the folders are copied from the before-newest R installation to the newest R installation. This the overrides "from" and "to" parameters.
+#' @param keep_old should the packages be COPIED to the new library folder, thus KEEPing the old package as they are? Or should they be removed?
+#' @param do_NOT_override_packages_in_new_R default TRUE If FALSE, then If a package exists in both the "from" and "to" library folders - it would copy to "to" the version of the package from "from". (this parameter should rarely be FALSE)
+#' @return TRUE if it copied (moved) packages, and FALSE if it did not.
+#' @seealso \link{get.installed.R.folders}
+#' @examples
+#' copy.packages.between.libraries(ask = T) # it will ask you from what R version to copy the packages into which R version. Since (do_NOT_override_packages_in_new_R = T) the function will make sure to NOT override your newer packages.
+#' # copy.packages.between.libraries(ask = T, keep_old = F) # As before, but this time it will MOVE (instead of COPY) the packages. e.g: erase them from their old location.
+copy.packages.between.libraries <- function(from, to, ask =F,keep_old = T, do_NOT_override_packages_in_new_R = T) {
- if(notify_user) {
- if(there_is_a_newer_version) {
- cat("There is a newer version of R for you to download!\n")
- cat("You are using R version: ", current_R_version, "\n")
- cat("And the latest R version is: ", latest_R_version, "\n")
- } else {# we are not using the latest R version - now what...
- # tell it to the user:
- cat("No need to update - you are using the latest R version: ", R.version$version.string , "\n")
- }
+ installed_R_folders <- get.installed.R.folders()
+ installed_R_folders_TABLE <-data.frame("R_version" = names(installed_R_folders) , Folder = installed_R_folders)
+
+ if(ask) {
+ ask_again <- T
+ while(ask_again){# n is the row number from the user
+ ss_R_folder_from <- ask.user.for.a.row(installed_R_folders_TABLE,
+ "From: Choose an R version/folder from which to copy the package library",
+ "FROM: Write the row number of the R version/folder FROM which to copy (or move) the package library (and then press enter):\n")
+ cat("\nThank you\n")
+ ss_R_folder_to <- ask.user.for.a.row(installed_R_folders_TABLE,
+ "To: Choose an R version/folder from INTO which the packages from your old R installations will be copied TO",
+ "TO: Write the row number of the R version/folder INTO which to copy (or move) the package library (and then press enter):\n")
+ cat("\nThank you\n")
+
+ from <- installed_R_folders[ss_R_folder_from]
+ to <- installed_R_folders[ss_R_folder_to]
+
+ DECISION_text <- paste("You've chosen to move your packages from: ", from, " to: ", to)
+ ask_again_12 <- ask.user.for.a.row(c("yes", "no"), "Are you sure?",paste(DECISION_text, " - Is this you final decision? \n(for 'yes' press 1, and for 'no' press 2)\n" ))
+ ask_again <- ifelse(ask_again_12==1, F, T) # if we need to ask
+ }
}
- there_is_a_newer_version
+ if(missing(to)) to <- installed_R_folders[1] # copy inTO the newest R
+ if(missing(from)) from <- installed_R_folders[2] # copy FROM the one version before newest R
+
+ # the libraries
+ from_library <- file.path(from , "library")
+ to_library <- file.path(to , "library")
+
+ # their packages
+ packages_in_the_from_library <- list.files(from_library)
+ packages_in_the_to_library <- list.files(to_library) # used for "packages_to_NOT_move" if do_NOT_override_packages_in_new_R==T
+
+ # which packages do we want to keep and which do we want to move?
+ packages_to_NOT_move <- unname(installed.packages(priority = "high")[,"Package"]) # some context on "high" packages: http://stackoverflow.com/questions/9700799/what-is-difference-between-r-base-and-r-recommended-packages
+ if(do_NOT_override_packages_in_new_R) packages_to_NOT_move <- c(packages_to_NOT_move, packages_in_the_to_library )
+ ss_packages_to_NOT_move_from <- packages_in_the_from_library %in% packages_to_NOT_move
+ ss_packages_to_YES_move_from <- !ss_packages_to_NOT_move_from # just so there would be no confusion
+ # the final list of packages to move:
+ packages_to_YES_move_from <- packages_in_the_from_library[ss_packages_to_YES_move_from]
+ paths_of_packages_to_copy_from <- file.path(from_library, packages_to_YES_move_from)
+
+
+ if(length(packages_to_YES_move_from)==0) {
+ cat("No packages to copy. Goodbye :) \n")
+ return(F)
+ }
+
+ # COPY old packages to the new global folder:
+ cat("-----------------------","\n")
+ cat("I am now copying ", length(packages_to_YES_move_from) ," packages from:", from_library, " ; into: ", to_library)
+ cat("-----------------------","\n")
+ flush.console() # refresh the console so that the user will see the massage
+ packages_to_YES_move_from
+ folders.copied <- file.copy(from = paths_of_packages_to_copy_from, # copy folders
+ to = to_library,
+ overwrite = TRUE,
+ recursive =TRUE)
+ cat("=====================","\n")
+ cat("Done. We finished copying all the packages to the new location\n")
+
+ if(!keep_old) {
+ cat("Next: we will remove the packages from the old R installation ('FROM') \n")
+ deleted_packages <- unlink(paths_of_packages_to_copy_from , recursive = TRUE) # delete all the packages from the "original" library folder (no need for double folders)
+ cat("Done. The old packages were deleted.\n")
+ }
+
+ return(TRUE)
}
-##' Checks for the latest R version, and if there is a newer version of R - downloads and installs it.
-##'
-##'
-##' @param ask should the user be asked if to download R or not (default is TRUE). If not, and the latest version of R is newer than what is currently installed - then R would be installed without asking the user for permission. Of course the installation part itself (the running of the .exe file) is dependent on the user.
-##' @param notify_user if to tell the user what version he has and what is the latest version (default is TRUE)
-##' @param browse_news if TRUE (and if there is a newer version of R) - it opens the browser to the NEWS of the latest version of R, for the user to read through
-##' @return a TRUE/FALSE value on whether or not R was updated.
-##' @export
-##' @examples
-##' update.R()
-##'
-update.R <- function(ask = T, notify_user=T, browse_news) {
+
+
+#' @title Checks for the latest R version, and if there is a newer version of R - downloads and installs it.
+#' @description This function performs the following steps:
+#' \itemize{
+#' \item Check what is the latest R version. If the current installed R version is up-to-date, the function ends (and returns FALSE)
+#' \item If a newer version of R is available, the user is asked if to review the NEWS of the latest R version - in order to decide if to install the newest R or not.
+#' \item If the user wishes to - the function will download and install it. (you will need to press the "next" buttons on your own)
+#' \item Once the installation is done, you should press "any-key", and the function will proceed with copying all of your packages from your old (well, current) R installation, into your newer R installation.
+#' \item You can then erase all of the packages in your old R installation.
+#' \item After your packages are moved (and the old ones possibly erased), you will get the option to update all of your packages in the new version of R.
+#' \item Lastely - you can close the current session of your old R.
+#' }
+#' @param browse_news if TRUE (and if there is a newer version of R) - it opens the browser to the NEWS of the latest version of R, for the user to read through
+#' @param install_R TRUE/FALSE - if to install a new version of R (if one is available). If missing (this is the default) - the user be asked if to download R or not.Of course the installation part itself (the running of the .exe file) is dependent on the user.
+#' @param copy_packages TRUE/FALSE - if to copy your packages from the old version of R to the new version of R. If missing (this is the default) - the user will be asked for his preference (he should say yes, unless he is using a global library folder).
+#' @param update_packages TRUE/FALSE - if to update your packages in the new version of R (all packages will be updated without asking confirmation per package) If missing (this is the default) - the user will be asked for his preference (he should say yes, unless he is using a global library folder). This is done by calling the Rscript in the new R.
+#' @param keep_old_packages - if the keep the packages in the library of the old R installation. If missing (this is the default) - the user will be asked for his preference (he should say yes, unless he is using a global library folder).
+#' @param quit_R TRUE/FALSE - if to quite R after the installation and package copying or not. If missing (this is the default) - the user is asked what to do.
+#' @param print_R_versions if to tell the user what version he has and what is the latest version (default is TRUE)
+#' @return a TRUE/FALSE value on whether or not R was updated.
+#' @export
+#' @examples
+#' update.R()
+#'
+update.R <- function(browse_news, install_R, copy_packages, keep_old_packages, update_packages, quit_R, print_R_versions=T) {
# this function checks if we have the latest version of R
# IF not - it notifies the user - and leaves.
# If there is a new version - it offers the user to download and install it.
- there_is_a_newer_version_of_R <- check.for.updates.R(notify_user)
+ there_is_a_newer_version_of_R <- check.for.updates.R(print_R_versions)
- if(!there_is_a_newer_version_of_R) return(F) # if we have the latest version - we might as well stop now...
+ if(!there_is_a_newer_version_of_R | !install_R) return(F) # if we have the latest version - we might as well stop now...
# else - there_is_a_newer_version_of_R==T
# should we ask?
- if(ask) {
+ if(missing(install_R)) {
# since there is a newer version - do you want to see the latest NEWS?
if(missing(browse_news)) {
@@ -124,7 +327,58 @@ update.R <- function(ask = T, notify_user=T, browse_news) {
}
# if we got this far, the user wants to install the latest version of R (and his current version is old)
- cat("Installing the newest version of R, pleaes wait...\n")
+ cat("Installing the newest version of R, pleaes wait for the installer file to download and run, and be sure to click 'next' as needed...\n")
install.R()
+
+
+ if(missing(copy_packages)) {
+ ask.user.for.a.row(c("Yes"), "Did you finish running the installer for the new R?", "Press 1 (and Enter) if the installation of R is finished:")
+ to_copy_packages <- readline("Do you wish to copy your packages from the older version of R to the newer version of R? (y/n): ")
+ copy_packages <- ifelse(tolower(to_copy_packages) == "y", T, F)
+ }
+
+
+ if(copy_packages) {
+ # should we keep the old packages?
+ if(missing(keep_old_packages)) {
+ to_keep_old_packages <- readline("Once your packages are copied to the new R, \ndo you wish to KEEP the packages from the library in the OLD R installation? \n(if 'n' is choosen - you will erase your packages in the old R version) (y/n): ")
+ keep_old_packages <- ifelse(tolower(to_keep_old_packages) == "n", F, T) # let's be safe...
+ }
+ # copy (or MOVE)
+ copy.packages.between.libraries(keep_old=keep_old_packages)
+ }
+
+ # should we update_packages?
+ if(missing(update_packages)) {
+ to_update_packages <- readline("Do you wish to update your packages in the newely installed R? (y/n): ")
+ update_packages <- ifelse(tolower(to_update_packages) == "y", T, F)
+ }
+
+ if(update_packages) {
+ new_Rscript_path <- file.path(get.installed.R.folders()[2], "bin/Rscript.exe")
+ update_packages_expression <- paste(new_Rscript_path, ' -e " options(repos=structure(c(CRAN=\'http://cran.rstudio.com/\'))); update.packages(checkBuilt=TRUE, ask=F) "')
+ # update_packages_expression <- paste(new_Rscript_path, ' -e "date()"')
+ # update_packages_expression <- paste(new_Rscript_path, ' -e "print(R.version)"')
+ shell(update_packages_expression)
+ }
+
+
+ # should we turn R off?
+ if(missing(quit_R)) {
+ to_quit_R <- readline("Do you wish to quit R (your workspace will NOT be saved)? (y/n): ")
+ quit_R <- ifelse(tolower(to_copy_packages) == "y", T, F)
+ }
+ if(quit_R) quit(save = "no")
+
+
return(TRUE)
}
+
+
+
+
+
+
+uninstall.R <- function() {
+ # notice that running the uninstall of R does not remove the old library folder!
+}
View
13 man/ask.user.for.a.row.Rd
@@ -7,14 +7,21 @@
}
\usage{
ask.user.for.a.row(TABLE,
- header_text = "Possible versions to download (choose one)")
+ header_text = "Possible versions to download (choose one)",
+ questions_text = "Please review the table of versions from above, \n and enter the row number of the file-version you'd like to install: ")
}
\arguments{
\item{TABLE}{a data.frame table with rows from which we
- wish the user to choose a row.}
+ wish the user to choose a row. If TABLE is not a
+ data.frame, it will be coerced into one.}
\item{header_text}{the text the users sees (often a
- question) - explaining which row he should choose from}
+ question) as a title for the printed table - explaining
+ which row he should choose from}
+
+ \item{questions_text}{the question the users see after
+ the printing of the table - explaining which row he
+ should choose from}
}
\value{
The row number the user has choosen from the data.frame
View
34 man/check.for.updates.R.Rd
@@ -0,0 +1,34 @@
+\name{check.for.updates.R}
+\alias{check.for.updates.R}
+\title{Checks if there is a newer version of R}
+\usage{
+ check.for.updates.R(notify_user = T,
+ page_with_download_url = "http://cran.rstudio.com/bin/windows/base/")
+}
+\arguments{
+ \item{notify_user}{if to print to you (the user) what is
+ the latest version and what version you are currently
+ using.}
+
+ \item{page_with_download_url}{the URL of the page from
+ which R can be downloaded.}
+}
+\value{
+ TRUE/FALSE - if there is a newer version of R to install
+ or not.
+}
+\description{
+ Fetches the latest (not development!) R version and
+ compares it with your currently installed R version (the
+ version of the R session from which you are running this
+ function).
+}
+\examples{
+check.for.updates.R()
+# Possible output:
+# There is a newer version of R for you to download!
+# You are using R version: 2.15.0
+# And the latest R version is: 2.15.3
+# [1] TRUE
+}
+
View
35 man/copy.packages.between.folders.Rd
@@ -0,0 +1,35 @@
+\name{copy.packages.between.folders}
+\alias{copy.packages.between.folders}
+\title{Copies all packages from one folder to another}
+\usage{
+ copy.packages.between.folders(keep_old = T, ask = F)
+}
+\arguments{
+ \item{ask}{should the user be given the option to choose
+ between which two libraries to copy the packages? If
+ FALSE (default), the folders are copied from the
+ before-newest R installation to the newest R
+ installation.}
+
+ \item{keep_old}{should the packages be copied to the new
+ library folder, thus KEEPing the old package as they are?
+ Or should they be removed?}
+}
+\value{
+ TRUE/FALSE if we created a new folder or not.
+}
+\description{
+ Copies all packages from one folder to another. This
+ function is used if we wish to either: \itemize{ \item
+ Upgrade R to a new version - and copy all of the packages
+ from the old R installation to the new one. \item Move to
+ a global library system - and wanting to copy all of
+ packages from the local library folder to the global one
+ } It takes into account that we don't want to copy
+ packages which have "high" importance (such as MASS,
+ rpart, Matrix and more...) to the new library folder.
+}
+\examples{
+copy.packages.between.folders()
+}
+
View
57 man/copy.packages.between.libraries.Rd
@@ -0,0 +1,57 @@
+\name{copy.packages.between.libraries}
+\alias{copy.packages.between.libraries}
+\title{Copies all packages from one library folder to another}
+\usage{
+ copy.packages.between.libraries(from, to, ask = F,
+ keep_old = T, do_NOT_override_packages_in_new_R = T)
+}
+\arguments{
+ \item{from}{a character vector for the location of the
+ old library folder FROM which to copy files from.}
+
+ \item{to}{a character vector for the location of the old
+ library folder TO which to copy files to.}
+
+ \item{ask}{should the user be given the option to choose
+ between which two libraries to copy the packages? If
+ FALSE (default), the folders are copied from the
+ before-newest R installation to the newest R
+ installation. This the overrides "from" and "to"
+ parameters.}
+
+ \item{keep_old}{should the packages be COPIED to the new
+ library folder, thus KEEPing the old package as they are?
+ Or should they be removed?}
+
+ \item{do_NOT_override_packages_in_new_R}{default TRUE If
+ FALSE, then If a package exists in both the "from" and
+ "to" library folders - it would copy to "to" the version
+ of the package from "from". (this parameter should rarely
+ be FALSE)}
+}
+\value{
+ TRUE if it copied (moved) packages, and FALSE if it did
+ not.
+}
+\description{
+ Copies all packages from one folder to another. This
+ function is used if we wish to either: \itemize{ \item
+ Upgrade R to a new version - and copy all of the packages
+ from the old R installation to the new one. \item Move to
+ a global library system - and wanting to copy all of
+ packages from the local library folder to the global one
+ } It takes into account that we don't want to copy
+ packages which have "high" importance (such as MASS,
+ boot, graphics, utils, rpart, Matrix and more GREAT
+ packages...) to the new library folder. Also, it assumes
+ that within an R installation, the packages are located
+ inside the "library" folder.
+}
+\examples{
+copy.packages.between.libraries(ask = T) # it will ask you from what R version to copy the packages into which R version. Since (do_NOT_override_packages_in_new_R = T) the function will make sure to NOT override your newer packages.
+# copy.packages.between.libraries(ask = T, keep_old = F) # As before, but this time it will MOVE (instead of COPY) the packages. e.g: erase them from their old location.
+}
+\seealso{
+ \link{get.installed.R.folders}
+}
+
View
23 man/create.global.library.Rd
@@ -0,0 +1,23 @@
+\name{create.global.library}
+\alias{create.global.library}
+\title{Creates a global library folder}
+\usage{
+ create.global.library(global_library_folder)
+}
+\arguments{
+ \item{global_library_folder}{the path of the new global
+ library folder to create. If missing, will be set to
+ R_path/R/library. (for example: "C:/Program
+ Files/R/library")}
+}
+\value{
+ TRUE/FALSE if we created a new folder or not.
+}
+\description{
+ Creates a global library folder (above the folder R is
+ currently installed in)
+}
+\examples{
+create.global.library()
+}
+
View
41 man/get.installed.R.folders.Rd
@@ -0,0 +1,41 @@
+\name{get.installed.R.folders}
+\alias{get.installed.R.folders}
+\title{Returns folder names with R installations}
+\usage{
+ get.installed.R.folders(sort_by_version = T,
+ add_version_to_name = T)
+}
+\arguments{
+ \item{sort_by_version}{should the returned vector be
+ sorted by the version number? (default is yes - so that
+ the first element is of the newest version of R) should
+ the user be given the option to choose between which two
+ libraries to copy the packages? If FALSE (default), the
+ folders are copied from the before-newest R installation
+ to the newest R installation.}
+
+ \item{add_version_to_name}{should the version number be
+ added to the vector of folders? (default is yes)}
+}
+\value{
+ Returns a character vector (possibly named, possibly
+ sorted) of the folders where there are R installations.
+}
+\description{
+ The function finds the folders where there are R
+ installations. This is important for deciding what to
+ uninstall, and where from and to to move libraries. This
+ function ignores installations of R-devel at this point.
+ Also, this function is based on only looking at the
+ folders above the current installation of R. If there
+ are other isntallations of R outside the above folder,
+ they will not be listed.
+}
+\examples{
+get.installed.R.folders() # returns the sorted and named vector of folder names where R is installed (in different versions). The first element is the folder of the newest version of R.
+get.installed.R.folders(F, F) # returns the folder names where R is installed (in different versions) - no sorting of the folder names was performed
+}
+\seealso{
+ \link{copy.packages.between.libraries}
+}
+
View
24 man/turn.number.version.Rd
@@ -0,0 +1,24 @@
+\name{turn.number.version}
+\alias{turn.number.version}
+\title{Turns a vector of version-numbers back to version-character}
+\usage{
+ turn.number.version(number_to_dots)
+}
+\arguments{
+ \item{number_to_dots}{A numeric vector - of the
+ number-version of R}
+}
+\value{
+ A vector of "numbers" representing the versions (for
+ example: 2015002). The names of the vector is the
+ original version character.
+}
+\description{
+ Turns a vector of version-numbers back to
+ version-character
+}
+\examples{
+turn.number.version(turn.version.to.number(c("2.15.2", "2.15.2")))
+turn.number.version(2015011) # "2.15.11"
+}
+
View
22 man/turn.version.to.number.Rd
@@ -0,0 +1,22 @@
+\name{turn.version.to.number}
+\alias{turn.version.to.number}
+\title{Turns version to number (for a vector of values)}
+\usage{
+ turn.version.to.number(version_with_dots)
+}
+\arguments{
+ \item{version_with_dots}{A character vector - of the
+ version of R (for example 2.15.2)}
+}
+\value{
+ A vector of "numbers" representing the versions (for
+ example: 2015002). The names of the vector is the
+ original version character.
+}
+\description{
+ Turns version to number (for a vector of values)
+}
+\examples{
+turn.version.to.number(c("2.15.2", "2.15.2"))
+}
+
View
21 man/turn.version.to.number1.Rd
@@ -0,0 +1,21 @@
+\name{turn.version.to.number1}
+\alias{turn.version.to.number1}
+\title{Turns version to number (for 1 value only)}
+\usage{
+ turn.version.to.number1(version_with_dots)
+}
+\arguments{
+ \item{version_with_dots}{A character value - of the
+ version of R (for example 2.15.2)}
+}
+\value{
+ A "number" representation of the version (for example:
+ 2015002)
+}
+\description{
+ Turns version to number (for 1 value only)
+}
+\examples{
+turn.version.to.number1("2.15.2")
+}
+
View
67 man/update.R.Rd
@@ -2,29 +2,70 @@
\alias{update.R}
\title{Checks for the latest R version, and if there is a newer version of R - downloads and installs it.}
\usage{
- update.R(ask = T, notify_user = T, browse_news)
+ update.R(browse_news, install_R, copy_packages,
+ keep_old_packages, update_packages, quit_R,
+ print_R_versions = T)
}
\arguments{
- \item{ask}{should the user be asked if to download R or
- not (default is TRUE). If not, and the latest version of
- R is newer than what is currently installed - then R
- would be installed without asking the user for
- permission. Of course the installation part itself (the
- running of the .exe file) is dependent on the user.}
-
- \item{notify_user}{if to tell the user what version he
- has and what is the latest version (default is TRUE)}
-
\item{browse_news}{if TRUE (and if there is a newer
version of R) - it opens the browser to the NEWS of the
latest version of R, for the user to read through}
+
+ \item{install_R}{TRUE/FALSE - if to install a new version
+ of R (if one is available). If missing (this is the
+ default) - the user be asked if to download R or not.Of
+ course the installation part itself (the running of the
+ .exe file) is dependent on the user.}
+
+ \item{copy_packages}{TRUE/FALSE - if to copy your
+ packages from the old version of R to the new version of
+ R. If missing (this is the default) - the user will be
+ asked for his preference (he should say yes, unless he is
+ using a global library folder).}
+
+ \item{update_packages}{TRUE/FALSE - if to update your
+ packages in the new version of R (all packages will be
+ updated without asking confirmation per package) If
+ missing (this is the default) - the user will be asked
+ for his preference (he should say yes, unless he is using
+ a global library folder). This is done by calling the
+ Rscript in the new R.}
+
+ \item{keep_old_packages}{- if the keep the packages in
+ the library of the old R installation. If missing (this
+ is the default) - the user will be asked for his
+ preference (he should say yes, unless he is using a
+ global library folder).}
+
+ \item{quit_R}{TRUE/FALSE - if to quite R after the
+ installation and package copying or not. If missing (this
+ is the default) - the user is asked what to do.}
+
+ \item{print_R_versions}{if to tell the user what version
+ he has and what is the latest version (default is TRUE)}
}
\value{
a TRUE/FALSE value on whether or not R was updated.
}
\description{
- Checks for the latest R version, and if there is a newer
- version of R - downloads and installs it.
+ This function performs the following steps: \itemize{
+ \item Check what is the latest R version. If the current
+ installed R version is up-to-date, the function ends (and
+ returns FALSE) \item If a newer version of R is
+ available, the user is asked if to review the NEWS of the
+ latest R version - in order to decide if to install the
+ newest R or not. \item If the user wishes to - the
+ function will download and install it. (you will need to
+ press the "next" buttons on your own) \item Once the
+ installation is done, you should press "any-key", and the
+ function will proceed with copying all of your packages
+ from your old (well, current) R installation, into your
+ newer R installation. \item You can then erase all of the
+ packages in your old R installation. \item After your
+ packages are moved (and the old ones possibly erased),
+ you will get the option to update all of your packages in
+ the new version of R. \item Lastely - you can close the
+ current session of your old R. }
}
\examples{
update.R()

0 comments on commit c0db04a

Please sign in to comment.
Something went wrong with that request. Please try again.