Skip to content

Instantly share code, notes, and snippets.

@BroVic
Last active August 11, 2022 23:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save BroVic/7771e1e86df35f6410a3f586ea1ef6c6 to your computer and use it in GitHub Desktop.
Save BroVic/7771e1e86df35f6410a3f586ea1ef6c6 to your computer and use it in GitHub Desktop.
Install gWidgets and gWidgetsRGtk2 archives
# IMPORTANT UPDATE!!!
# THIS SCRIPT STANDS ARCHIVED AS IT HAS BEEN OVERTAKEN BY EVENTS
# IN THE SPACE, NOTABLY THE ARCHIVING OF RGTK2 ON CRAN
# Created: 30 Jul 2020
# Updated: 03 Jan 2022
##################################################################
# gwdg-arch.R: Installation of current and archived packages #
# upon which the RQDA package depends. The key #
# packages mentioned are RGtk2, gWidgets, #
# #
# HOW TO USE THIS SCRIPT: #
# This file should be run at the command line or in an #
# interactive R session i.e. in the R console, as follows: #
# * Windows command line (CMD or PowerShell) #
# Rscript.exe gwdg-arch.R [--args verbose] #
# #
# * R console #
# source('gwdg-arch.R') #
# #
# IMPORTANT NOTICE!!! #
# Please BE aware that if Gtk+ is not properly installed, this #
# script will stop and require the user to open an R session #
# to install it. Once this is done, this script should be run #
# again to complete the installation of the packages that #
# directly or indirectly depend on it. #
# #
##################################################################
local({
# ---
cArg <- commandArgs(trailingOnly = TRUE)
cArg <- cArg[length(cArg)]
# ---
## Provides the index to CRAN directory
cran.index <- function() {
c("https://cran.r-project.org")
}
## Returns the address to RStudio's CRAN mirror
rstudio <- function() {
c('https://cran.rstudio.com')
}
local_gtk_bin_path <- function(pkg = 'RGtk2') {
gtkdir <- file.path(.libPaths()[1], pkg, 'gtk')
file.path(gtkdir, .Platform$r_arch)
}
shellQuiet <- TRUE
if (length(cArg) != 0) {
if (cArg == "verbose")
shellQuiet <- FALSE
}
## Installs initial packages required by the script.
## What makes these ones special is that they are
## current package versions from CRAN and they are
## downloaded as binaries.
## @param cranry A character vector of packages.
.install_init <- function(cranbry) {
stopifnot(is.character(cranbry))
tryCatch({
notInstalled <-
cranbry[!cranbry %in% .packages(all.available = TRUE)]
install.packages(notInstalled, repos = rstudio(), quiet = !shellQuiet)
}, error = function(e) {
stop(sprintf(
"Initialization failed. Install %s",
paste(cranbry, collapse = ', ')
))
})
}
## Checks the availability of Rtools on Windows (v35)
.check_buildtools <- function() {
if (!devtools::has_devel()) {
if (.Platform$OS.type == 'windows') {
toolsUrl <-
file.path(cran.index(),
"bin",
.Platform$OS.type,
"Rtools/history.html")
errBuildtools <-
sprintf("Build tools were not found. Please visit %s to install.",
toolsUrl)
stop(errBuildtools, call. = TRUE)
}
}
}
.install_init(c('devtools', 'cairoDevice', 'igraph'))
.check_buildtools()
## Installs a given CRAN archive
## @param name Name of the package
## @param ver The package version
inst <- function(name, ver) {
rgtk2 <- "RGtk2"
archOpts <- "--no-multiarch"
isRGtk2 <- name == rgtk2
pkgExists <- quote(name %in% .packages(all.available = TRUE))
if (isRGtk2) {
msgRGtk2 <-
list(
line1 = "Installing 'RGtk2'. If it fails, use `install.packages` in R console ... ",
line2 = "Run `library(RGtk2)` in R to install Gtk+. Then, rerun this script."
)
# Custom error condition
abortRgtk2 <- function() {
msg <-
sprintf("Could not install %s. Try doing so in R console", rgtk2)
stop(msg, call. = FALSE)
}
## Install RGtk2
if (!eval(pkgExists)) {
message(msgRGtk2$line1, appendLF = !shellQuiet)
tryCatch({
install.packages(
rgtk2,
repos = rstudio(),
INSTALL_opts = archOpts,
quiet = shellQuiet,
verbose = shellQuiet
)
message("Done") # Per RGtk2/R/zzz.R, Gtk+ can only be installed interactively.
},
error = function(e) {
message("Failed")
abortRgtk2()
},
warning = function(w) {
wrnmsg <- "cannot remove prior installation of package 'RGtk2'"
if (conditionMessage(w) == wrnmsg)
abortRgtk2()
},
finally = return(message(msgRGtk2$line2)))
}
}
## Avoid repeat installations via an early return
## If we're dealing with RGtk2, just stop the script
## and install Gtk+ interactively, if it is required.
if (eval(pkgExists)) {
message(sQuote(name), " is already installed")
if (isRGtk2) {
if (!dir.exists(local_gtk_bin_path(rgtk2))) { # Consider: Directory may be a dud.
message(msgRGtk2$line2)
stop('Execution stopped.', call. = FALSE)
}
}
return()
}
## Grab the package archives as desired.
## When installing them, the following are considered:
## => asking for upgrade interrupts installation
## => install only one of 32- or 64-bit, not both
## But first, if RGtk2 is not present, there's no
## point trying to install packages that depend on it.
isRgtkDep <- (name == 'gWidgetsRGtk2' || name == 'RQDA')
rgtkNotReady <- !(rgtk2 %in% .packages(all.available = TRUE) &&
dir.exists(local_gtk_bin_path(rgtk2)))
if (isRgtkDep && rgtkNotReady) {
message(sQuote(name), " was not installed because RGtk2 is not ready")
return()
}
tryCatch({
msg <- sprintf("Installing '%s' ... ", name)
message(msg, appendLF = !shellQuiet)
if (!isRGtk2)
devtools::install_version(
name,
ver,
repos = cran.index(),
quiet = shellQuiet,
upgrade = "never",
INSTALL_opts = archOpts
)
message("Done")
},
error = function(e) {
message("Failed")
})
} # end inst()
pkgversions <- c(RGtk2 = "2.20.36",
gWidgets = '0.0-54.2',
gWidgetsRGtk2 = '0.0-86',
RQDA = '0.3-1')
invisible(Map(inst, names(pkgversions), pkgversions))
})
# Used successfully with R 4.0.3
@cmadland
Copy link

I presume this will not work on macOS. is that correct?

@BroVic
Copy link
Author

BroVic commented Jul 27, 2020

@ElHel

An interactive session is when you run R code in the R console i.e. the one that usually has this prompt >.

If you confirmed that you installed Gtk+ installed, then try running this script again. By design, it is not supposed to work if Gtk+ is not properly installed. Unfortunately, the creators of RGtk2 did not make it possible to automatically install Gtk+ via scripts like this one.

When running it again, use Rscript gwdg-arch.R --args verbose so that if it fails, the internal errors will be displayed and you can share that for clarity.

Best!

@BroVic
Copy link
Author

BroVic commented Jul 27, 2020

@cmadland

It was written for Windows, but it can be repurposed for Mac with little difficulty. I don't have a Mac nearby, so can't test it out. If you want to try it out I would suggest the following:

  1. Install Gtk+ independently
  2. Remove/disable lines 11 - 15.
  3. Make sure you have the relevant R package build tools for Mac.
  4. Instead of Rscript.exe I believe you would have to use R CMD BATCH gwdg-arch.R

@cmadland
Copy link

so, I tried it as is, and the only error was

‘RGtk2’ is already installed
 Error: Load 'RGtk2' in R to install its dependencies and then rerun. 

@BroVic
Copy link
Author

BroVic commented Jul 27, 2020

@cmadland

That error occured because RGtk2 could not find Gtk+. Try opening R itself and loading RGtk2 in the R console with library(). I don't know how it works on Mac; on Windows it would bring up a dialog, asking you to install Gtk+.

@cmadland
Copy link

I get that dialog, install Gtk+, installation runs successfully, restart R, and the whole thing repeats. I get the same dialog to install Gtk+

@ElHel
Copy link

ElHel commented Jul 27, 2020

I always get this Error:

Error: unexpected symbol in "Rscript gwdg"

Any Idea?

Thanks!

@BroVic
Copy link
Author

BroVic commented Jul 27, 2020

@ElHel

It looks like you're trying to run that command in the R console. It won't work there. Use the terminal/shell. What I asked you to run inside R was only library(RGtk2).

@BroVic
Copy link
Author

BroVic commented Jul 27, 2020

@cmadland:

Honestly, given my limited knowledge of Mac, if I were in your shoes, I would install Gtk+ separately.

@ElHel
Copy link

ElHel commented Jul 27, 2020

this is what I get from the terminal:

C:\Users\admin\Documents>Rscript gwdg-arch.R --args verbose
Fatal error: cannot open file 'gwdg-arch.R': No such file or directory

Do you have a idea? If not I will look for another program, this is taking so much time and I really don't know what I'm doing there...

I appreciate your herlp!

@BroVic
Copy link
Author

BroVic commented Jul 27, 2020

@ElHel

You're going to have to run it from the folder where you've saved the script gwdg-arch.R or else Rscript won't be able to find it. If you're not sure how, open the folder in your file explorer, type cmd in the address bar and hit ENTER. The shell should open in that very directory.

@ElHel
Copy link

ElHel commented Jul 27, 2020

@BroVic

Thanks a lot for your help. I'll start searching for an alternative now, since my confusion is growing by the moment. I don't want to spend another day trying to install RQDA :(

Have a nice day!

@BroVic
Copy link
Author

BroVic commented Jul 27, 2020

@ElHel:

As far as alternatives go, there's a fork of the RQDA project that has been developed using the updates that made RQDA to be archived in the first place. I've not tried it, but there are some good reviews. I chose to maintain this version because that solution is not guaranteed to got to CRAN.

Here it is: https://github.com/JanMarvin/RQDA

Regards,

@ElHel
Copy link

ElHel commented Jul 28, 2020

@BroVic

Thanks! This one works!

@BroVic
Copy link
Author

BroVic commented Jul 28, 2020

Great!

@BroVic
Copy link
Author

BroVic commented Jul 28, 2020

FWIW, I located the issue with this code, something I alluded to in an earlier comment:

By default, in x64 systems, it attempts to install both 32- and 64-bit compatible package versions. However, when installing Gtk+ (as @cmadland did) it will only install that version specific to the existing configuration. Thus, when you try to install any of the RGtk2-dependent packages (like RQDA), the 32-bit version of it, lacks a corresponding 32-bit Gtk+ to work with. So it keeps trying to install it, as a result of the recursive function .onLoad in RGtk/R/zzz.R.

To solve this issue, we now pass the R CMD INSTALL option --no-multiarch and only a single platform-specific version of the packages are now installed all through.

@Ma-Se-collab
Copy link

Hi! Thanks for this script!
I tried it with a colleague and get the following issues:

Your system is ready to build packages!
‘RGtk2’ is already installed
Installing 'gWidgets' ... Warning: unable to access index for repository https://cran.r-project.org/src/contrib:
  impossible d'ouvrir l'URL 'https://cran.r-project.org/src/contrib/PACKAGES'
Failed
Installing 'gWidgetsRGtk2' ... Failed
Installing 'RQDA' ... Installing 3 packages: fastmap, cachem, gWidgets
Failed
Warning message:
1 components of `...` were not used.

We detected these problematic arguments:
* `INSTALL_opts`

Did you misspecify an argument? 

--> Sorry some parts are in French, can translate if needed.
I am new to all this...

We also tried the suggestion you shared in a comment - https://github.com/JanMarvin/RQDA (followed the steps for "Installation for Windows users from source package") and also got some issues here, not sure if you can help there too?

Error in value[[3L]](cond) : 
  Package ‘memoise’ version 1.1.0 cannot be unloaded:
 Error in unloadNamespace(package) : l'espace de noms ‘memoise’ est importé par ‘RSQLite’, ‘devtools’ et ne peut, donc, pas être déchargé

> devtools::install_github("RQDA/RQDA", INSTALL_opts = "--no-multiarch")
Skipping install of 'RQDA' from a github remote, the SHA1 (d5ce7ba3) has not changed since last install.
  Use `force = TRUE` to force installation
Warning message:
1 components of `...` were not used.

We detected these problematic arguments:
* `INSTALL_opts`

Did you misspecify an argument? 

@BroVic
Copy link
Author

BroVic commented Feb 5, 2021

Could you please run sessionInfo( ) and post the output?

@Ma-Se-collab
Copy link

Here it is:
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=French_France.1252 LC_CTYPE=French_France.1252
[3] LC_MONETARY=French_France.1252 LC_NUMERIC=C
[5] LC_TIME=French_France.1252

attached base packages:
[1] stats graphics grDevices utils datasets methods base

loaded via a namespace (and not attached):
[1] compiler_4.0.3

@BroVic
Copy link
Author

BroVic commented Feb 8, 2021

Hi @Ma-Se-collab,

I edited the script somewhat because when I tested it I ran into a similar error. The API must have changed somewhat and it was installing igraph package as source files. The error was due to a compilation failure which, I think, is linked to the option chosen to install for only ONE architecture. The script now installs igraph separately as binary and it works for me. We have similar sessionInfo()s.

At any rate, if you run into an issue with the script, add the --verbose flag so that one can see the source of the error more clearly.

@Ma-Se-collab
Copy link

Hi @BroVic,
Many thanks it worked!!!

@BroVic
Copy link
Author

BroVic commented Feb 8, 2021

You're welcome!

@poh58
Copy link

poh58 commented Feb 16, 2021

Hi BroVic,
Thanks for the script.

Error: .onLoad failed in loadNamespace() for 'pkgload', details:
call: NULL
error: package ‘testthat’ does not have a namespace
Called from: runHook(".onLoad", env, package.lib, package)

Do you have a idea? I appreciate your help!

Poh

run sessionInfo( )

output

R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:
[1] LC_COLLATE=English_Malaysia.1252 LC_CTYPE=English_Malaysia.1252 LC_MONETARY=English_Malaysia.1252
[4] LC_NUMERIC=C LC_TIME=English_Malaysia.1252

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] gWidgets_0.0-54.2 RGtk2_2.20.36

loaded via a namespace (and not attached):
[1] prettyunits_1.1.1 ps_1.5.0 rprojroot_2.0.2 withr_2.4.1 crayon_1.4.1 assertthat_0.2.1
[7] R6_2.5.0 rlang_0.4.10 cachem_1.0.1 cli_2.3.0 remotes_2.2.0 callr_3.5.1
[13] ellipsis_0.3.1 desc_1.2.0 tools_4.0.3 glue_1.4.2 processx_3.4.5 fastmap_1.1.0
[19] compiler_4.0.3 pkgbuild_1.2.0 memoise_2.0.0

@BroVic
Copy link
Author

BroVic commented Feb 16, 2021

Hi @poh58

pkgload is a dependency of devtools, so perhaps you should install devtools again. In any case, I just released a package to help with this installation. Perhaps you might like to try it out. https://github.com/BroVic/RQDAassist

@poh58
Copy link

poh58 commented Feb 20, 2021

@BroVic
Thank you for the advise. The package is now running smoothly on my system!
Cheers
Poh

@BroVic
Copy link
Author

BroVic commented Feb 21, 2021

@poh58

Fantastic! Cheers.

@alxoka
Copy link

alxoka commented Mar 9, 2021

Thank you for your contribution trying to get this to work.

I received the following output error inputting into the R studio terminal:

$ Rscript gwdg-arch.R --args verbose
Error in get(genname, envir = envir) : object 'testthat_print' not found
Your system is ready to build packages!
'RGtk2' is already installed
'gWidgets' is already installed
Installing 'gWidgetsRGtk2' ...
Downloading package from url: https://cran.r-project.org/src/contrib/Archive/gWidgetsRGtk2/gWidgetsRGtk2_0.0-86.tar.gz
Installing package into 'C:/Users/me/Documents/R/win-library/4.0'
(as 'lib' is unspecified)
* installing *source* package 'gWidgetsRGtk2' ...
** package 'gWidgetsRGtk2' successfully unpacked and MD5 sums checked
** using staged installation
** R
** inst
** byte-compile and prepare package for lazy loading
Error: (converted from warning) package 'RGtk2' was built under R version 4.0.3
Execution halted
ERROR: lazy loading failed for package 'gWidgetsRGtk2'
* removing 'C:/Users/me/Documents/R/win-library/4.0/gWidgetsRGtk2'
Failed
Installing 'RQDA' ...
Downloading package from url: https://cran.r-project.org/src/contrib/Archive/RQDA/RQDA_0.3-1.tar.gz
Installing package into 'C:/Users/me/Documents/R/win-library/4.0'(as 'lib' is unspecified)
ERROR: dependency 'gWidgetsRGtk2' is not available for package 'RQDA'
* removing 'C:/Users/me/Documents/R/win-library/4.0/RQDA'
Failed

@BroVic
Copy link
Author

BroVic commented Mar 10, 2021

Hi @alxoka,

Did you install Gtk+ by running library(RGtk2)? I also noticed that your terminal has a bash prompt? What OS are you running - perhaps you could provide me with the output of sessionInfo() so I can take a closer look and try to reproduce your error.

@alxoka
Copy link

alxoka commented Mar 10, 2021

Hello,

EDIT: I fixed it! Thank you!! By checking library(RGtk2) I realized my R version 4.0.2 was below what RGtk2 was built on (4.0.3). I updated R to 4.0.4, loaded library(RGtk2) in R studio, then Rscript gwdg-arch.R --args verbose in the terminal of R Studio and it worked!


I think I installed Gtk+ and ran library(RGtk2)

Here is my sessionInfo(). I am using Windows 10 x64. I'm not too sure what is the difference between bash and non bash prompt or the significance of that.

> library(RGtk2)
> sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets 
[6] methods   base     

other attached packages:
[1] RGtk2_2.20.36

loaded via a namespace (and not attached):
[1] compiler_4.0.2 tools_4.0.2   
> 

Additionally,

R version 4.0.2 (2020-06-22) -- "Taking Off Again"
Copyright (C) 2020 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

@BroVic
Copy link
Author

BroVic commented Mar 11, 2021

Fantastic!

The bash prompt is found in the default system shell for major Linux distributions (Bourne-again shell). But you said you ran it in the R Studio terminal. So on Windows 10, your terminal is probably running on Git-Bash or Windows Subsystem for Linux (WSL), if installed.

Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment