0

I have written a Shiny App which runs perfectly in my local machine. I have used RJDBC to connect to the DB2 database in IBM Cloud. The code is as follows.

#Load RJDBC
dyn.load('/Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home/lib/server/libjvm.dylib')
# dyn.load('/Users/parthamajumdar/Documents/Solutions/PriceIndex/libjvm.dylib')
library(rJava)
library(RJDBC)

As the path is hard coded, I copied the file libjvm.dylib to the Project directory and pointed to that. When I do this, R gives a fatal error.

I remove the absolute path and replaced with "./libjvm.dylib" and deployed the application on ShinyApp.io website. When I run the program, it gives a fatal error.

#Values for you database connection
dsn_driver = "com.ibm.db2.jcc.DB2Driver"
dsn_database = "BLUDB"            # e.g. "BLUDB"
dsn_hostname = "dashdb-entry-yp-lon02-01.services.eu-gb.bluemix.net"   # e.g. replace <yourhostname> with your hostname, e.g., "Db2 Warehouse01.datascientstworkbench.com"
dsn_port = "50000"                # e.g. "50000" 
dsn_protocol = "TCPIP"            # i.e. "TCPIP"
dsn_uid = "<UID>"              # e.g. userid
dsn_pwd = "<PWD>"            # e.g. password

#Connect to the Database
#jcc = JDBC("com.ibm.db2.jcc.DB2Driver", "/Users/parthamajumdar/lift-cli/lib/db2jcc4.jar");
jcc = JDBC("com.ibm.db2.jcc.DB2Driver", "db2jcc4.jar");
jdbc_path = paste("jdbc:db2://",  dsn_hostname, ":", dsn_port, "/", dsn_database, sep="");
conn = dbConnect(jcc, jdbc_path, user=dsn_uid, password=dsn_pwd)

Similarly, I copied the file "db2jcc4.jar" to my local project directory. If I point to the local project directory for this file in my local machine, the program works. However, when I deploy on ShinyApp.io, it gives fatal error.

Request your please letting me know what I need to do so that the application runs properly on the ShinyApp.io website.

The error is as follows when I run the application from Shiny server:

Attaching package: ‘lubridate’

The following object is masked from ‘package:base’:

    date

Loading required package: nlme
This is mgcv 1.8-23. For overview type 'help("mgcv-package")'.
Error in value[[3L]](cond) : 
  unable to load shared object '/srv/connect/apps/ExpenseAnalysis/Drivers/libjvm.dylib':
  /srv/connect/apps/ExpenseAnalysis/Drivers/libjvm.dylib: invalid ELF header
Calls: local ... tryCatch -> tryCatchList -> tryCatchOne -> <Anonymous>
Execution halted
rene
  • 41,474
  • 78
  • 114
  • 152
Partha
  • 47
  • 2
  • 8
  • Edit your question to *show* the fatal-error details . Also, the Db2 jdbc driver needs two files to communicate with Unix/Linux/Windows versions of Db2. The other file is db2jcc_license_cu.jar. – mao Apr 24 '18 at 08:30
  • Dear Sir, When I use this file from my app directory, the program aborts. Also, in the Shiny Server, the program aborts. – Partha Apr 24 '18 at 19:32
  • The problem is solved. Thank you Sir for your patience and help. I ran this command: sudo R CMD javareconf. Then I ran install.packages("rJava"). Then I commented out the line containing dyn.load(). Lastly, I moved db2jcc4.jar to a subdirectory. Now, the application runs perfectly. – Partha Apr 24 '18 at 21:00

1 Answers1

1

What works for me is the following and it is independent of OS.

  1. Create your own R package that contains the file you need somewhere in the extdata folder. As an example, your package could be yourpackage and the file would be something like extdata/drivers/mydriver.lib. Typically this would be stored at this location inst/extdata/drivers. See http://r-pkgs.had.co.nz/inst.html for details.

  2. Store this package on github and if you want privacy you will need to work out how to grant an access token.

  3. Use the devtools package to install it. The command would be something like this, devtools::install_github("you/yourpackage", auth_token = "youraccesstoken"). Do this once before deploying to Shiny.io. Ensure that you also do library(yourpackage). The package submission process will work out that it needs to fetch from Github.

  4. Use the following R code to find the file. system.file('extdata/drivers/mydriver.lib, package='yourpackage'). This will give you the full path to the file and you can use it.

Andrew Chisholm
  • 6,362
  • 2
  • 22
  • 41
  • Thanks for your guidance. I made the following code: install.packages("devtools") library("devtools") devtools::install_github("klutometis/roxygen") library(roxygen2) setwd("/Users/parthamajumdar/Documents/Solutions") create("DriverLoader") devtools::install_github("partha6369/DriverLoader", auth_token = "") library("DriverLoader") system.file('extdata/drivers/libjvm.dylib', package='DriverLoader') It returns an empty string. – Partha Apr 24 '18 at 10:14
  • I reckon you need to move the driver to `inst/extdata/drivers` on the physical machine where you build the R package. – Andrew Chisholm Apr 24 '18 at 11:43
  • Dear Sir, I moved it the inst directory. Now, it does not give any output. Please advice. > library("DriverLoader") > system.file('extdata/drivers/libjvm.dylib', package='DriverLoader') > – Partha Apr 24 '18 at 14:56
  • You need to create a folder called `inst/extdata/drivers` in the package definition and move the file to there. You will then need to rebuild the package. – Andrew Chisholm Apr 24 '18 at 16:31
  • I'm assuming you are using RStudio to create the R package by the way. – Andrew Chisholm Apr 24 '18 at 16:41
  • Dear Sir, I followed your steps. Now, when I create the package and upload to Git and then fetch the path, it works. However, when I use this in my Shiny App, it says that there is no such package. What need doing please? Thanks in advance. Regards, Partha – Partha Apr 24 '18 at 19:25
  • Dear Sir, I added this code: install.packages("./pkg/DriverLoaderPro_0.1.0.tar.gz",repos=NULL, type="source") library("DriverLoaderPro") JavaLib <- DriverLoaderPro::getLibDir() print(JavaLib) dyn.load(JavaLib) Now, it is getting the directory properly, However, the program still aborts. What needs doing? Please advice. Regards, Partha – Partha Apr 24 '18 at 19:41
  • I think it is not getting from Git. Instead it is getting the file from local directory. That seems to be the problem. How can I get this file from Git? – Partha Apr 24 '18 at 19:42
  • Use `devtools::install_github` as described in step 3. I can see your repository on Github and it does not need a token since it is public. You do need to create the correct folder structure - i.e. `inst/extdata/drivers` and push that to Github. – Andrew Chisholm Apr 24 '18 at 20:41
  • The problem is solved. Thank you Sir for your patience and help. I ran this command: sudo R CMD javareconf. Then I ran install.packages("rJava"). Then I commented out the line containing dyn.load(). Lastly, I moved db2jcc4.jar to a subdirectory. Now, the application runs perfectly. – Partha Apr 24 '18 at 20:59