Skip to content

Instantly share code, notes, and snippets.

@BroVic

BroVic/gwdg-arch.R

Last active Sep 29, 2020
Embed
What would you like to do?
Install gWidgets and gWidgetsRGtk2 archives
# Date: 30 July 2020
##################################################################
# 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(Sys.getenv("R_LIBS_USER"), 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'))
.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.2
@ChrisMwene

This comment has been minimized.

Copy link

@ChrisMwene ChrisMwene commented Jul 13, 2020

failing to install gWidgetsRGtk2

Installing 'gWidgets' ... Done
Installing 'gWidgetsRGtk2' ... Failed
Installing 'RQDA' ... Installing 7 packages: RSQLite, gWidgetsRGtk2, igraph, bit, bit64, blob, plogr
Failed

@BroVic

This comment has been minimized.

Copy link
Owner Author

@BroVic BroVic commented Jul 13, 2020

@ChrisMwene Will look into it and revert asap

@BroVic

This comment has been minimized.

Copy link
Owner Author

@BroVic BroVic commented Jul 13, 2020

@ChrisMwene:

Okay, I found a problem. If any of the installed versions of dependencies is old, install_version prompts the user, by default, asking whether to upgrade or not. I've added an argument to disable that (line 15).

I removed the packages I had and was able to install them. If you encounter a problem, you can diagnose by changing quiet to FALSE (on line 14) so that you can see the output of the individual installations.

Cheers.

@ChrisMwene

This comment has been minimized.

Copy link

@ChrisMwene ChrisMwene commented Jul 14, 2020

It worked ... thanks

@BroVic

This comment has been minimized.

Copy link
Owner Author

@BroVic BroVic commented Jul 14, 2020

You're welcome. Happy to help.

@ElHel

This comment has been minimized.

Copy link

@ElHel ElHel commented Jul 27, 2020

Hi!
I think I have the same problem.
This is the result of your script:

‘RGtk2’ is already installed
‘gWidgets’ is already installed
Installing 'gWidgetsRGtk2' ... Failed
Installing 'RQDA' ... Failed

This is what I'm using:
R version 3.6.3 (2020-02-29)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 10 x64 (build 18362)

I have installed all dependencies (RSQLite, igraph, bit, bit64, blob, plogr etc.), but it still doesn't work.
If you can help me, I will be very grateful.

Cheers.

@BroVic

This comment has been minimized.

Copy link
Owner Author

@BroVic BroVic commented Jul 27, 2020

@ElHel

Thanks for your comment.

Check if you have Gtk+ by attempting to load RGtk2 with library() and do so in an interactive R session. I'll look out for your response.

Cheers.

@ElHel

This comment has been minimized.

Copy link

@ElHel ElHel commented Jul 27, 2020

@BroVic

I think i installed it and get no response to library(RGtk2):

library(RGtk2)

If I remember correctly I've installed it by typing this command and being asked if I wanted to install GTK+. I confirmed. But I don't know what a 'interactive R session' means. I'm new to the program and looking forward to do text analysis - no programming skills at all.

Thanks a lot for your help!

@cmadland

This comment has been minimized.

Copy link

@cmadland cmadland commented Jul 27, 2020

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

@BroVic

This comment has been minimized.

Copy link
Owner Author

@BroVic 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

This comment has been minimized.

Copy link
Owner Author

@BroVic 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

This comment has been minimized.

Copy link

@cmadland cmadland commented Jul 27, 2020

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

This comment has been minimized.

Copy link
Owner Author

@BroVic 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

This comment has been minimized.

Copy link

@cmadland cmadland commented Jul 27, 2020

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

This comment has been minimized.

Copy link

@ElHel ElHel commented Jul 27, 2020

I always get this Error:

Error: unexpected symbol in "Rscript gwdg"

Any Idea?

Thanks!

@BroVic

This comment has been minimized.

Copy link
Owner Author

@BroVic 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

This comment has been minimized.

Copy link
Owner Author

@BroVic 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

This comment has been minimized.

Copy link

@ElHel 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

This comment has been minimized.

Copy link
Owner Author

@BroVic 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

This comment has been minimized.

Copy link

@ElHel 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

This comment has been minimized.

Copy link
Owner Author

@BroVic 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

This comment has been minimized.

Copy link

@ElHel ElHel commented Jul 28, 2020

@BroVic

Thanks! This one works!

@BroVic

This comment has been minimized.

Copy link
Owner Author

@BroVic BroVic commented Jul 28, 2020

Great!

@BroVic

This comment has been minimized.

Copy link
Owner Author

@BroVic 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.