IBM Data Science Experience (DSX): Using ibmdbR on RStudio - dashdb

I’ve created a connection to Db2 Warehouse on Cloud: dashDB for Analytics-t1 / Database: BLUDB. I’ve given ‘dashdb connect’ as the connection name.
Then I've selected Tools / RStudio. In RStudio, I've run the following lines. The error message below.
Code snippet:
library(ibmdbR)
con <- idaConnect('BLUDB','','')
#Close the connection
idaClose(con)
Output:
con <- idaConnect('BLUDB','','')
Warning messages:
1: In RODBC::odbcDriverConnect("DSN=BLUDB", believeNRows = FALSE) : [RODBC] ERROR: state 08001, code -30082, message [unixODBC][IBM][CLI Driver] SQL30082N Security processing failed with reason "17" ("UNSUPPORTED FUNCTION"). SQLSTATE=08001
2: In RODBC::odbcDriverConnect("DSN=BLUDB", believeNRows = FALSE) : ODBC connection failed

Your code snippet will only work as is if you run it in the RStudio from the DB2 Warehouse console. If you launch RStudio within DSX you need to configure connectivity. The following worked for me:
install.packages("ibmdbR")
library(ibmdbR)
dsn_driver <- "BLUDB"
dsn_database <- "BLUDB"
dsn_hostname <- "..."
dsn_port <- "50000"
dsn_protocol <- "TCPIP"
dsn_uid <- "..."
dsn_pwd <- "..."
con_path <- paste(dsn_driver,";DATABASE=",dsn_database,";HOSTNAME=",dsn_hostname,";PORT=",dsn_port,";PROTOCOL=",dsn_protocol,";UID=",dsn_uid,";PWD=",dsn_pwd,sep="")
ch <-idaConnect(con_path)
idaInit(ch)
idaShowTables()
Replace "..." with your credentials and you should be good to go.
I followed the instructions from the video named "Connect to dashDB in RStudio" on this page: https://datascience.ibm.com/docs/content/analyze-data/rstudio-overview.html and found the following documentation: https://datascience.ibm.com/blog/dashdb-r-dsx/

Related

R Error: "attempt to use zero-length variable name" when using "Preview on save"

It seems there is a problem on "Preview on Save" option in RStudio 2022.07.1 Build 554.
The code below is saved as "tmp1.Rmd". I can run chunks, and I CAN use Preview button (it works). But I can NOT save with "Preview on Save" option set.
When I change YAML Header output from output: html_notebook to output: html_document the error stops (I saw in this question).
Is this an RStudio bug?
The code:
---
title: "R Notebook"
output: html_notebook
---
# VE
```{r}
x=3
```
end

Run a app from terminal and send a password

I want to launch a shiny application from a terminal to avoid blocking the rstudio console.
My application uses the ssh package to connect to a remote machine.
When I launch the application from rstudio a small window opens to ask me for my password but when I launch the application from the terminal i get an error message.
$ "Rscript.exe" -e "shiny::runApp('app')"
Loading required package: shiny
Warning: package 'shiny' was built under R version 3.5.3
Warning: package 'ssh' was built under R version 3.5.3
Linking to libssh v0.8.6
Password callback did not return a string value
Erreur : Authentication with ssh server failed
Stopped
app.R
library(shiny)
library(ssh)
ssh.session <- ssh::ssh_connect(host = host)
cat("*** Logging in of the session ***")
# Define UI for application that draws a histogram
ui <- fluidPage()
server <- function(input, output) {
onStop(function() {
ssh_disconnect(ssh.session)
cat("*** Logging out of the session ***")
})
}
# Run the application
shinyApp(ui = ui, server = server)
I don't know about your security necessity in your use case...
What you could'd do (if its ok for you to specify a password while calling Rscript), is giving the password as an extra parameter when calling RScript and then forwarding it to ssh_connect(). You could also do the same with host variable. Im calling this R file via:
Rscript --vanilla home/user/R/app.R "password"
I'm not running shiny::runApp, because the line shinyApp(ui=ui, server=server) will automatically start up the shiny application, allowing us to connect to ssh beforehand.
In the app.R file we forward the arguments to ssh_connect.
app.R
library(shiny)
library(ssh)
args <- commandArgs(trailingOnly = TRUE)
host="host"
ssh.session <- ssh::ssh_connect(host = host,passwd = )
cat("*** Logging in of the session ***")
# Define UI for application that draws a histogram
ui <- fluidPage()
server <- function(input, output) {
onStop(function() {
ssh_disconnect(ssh.session)
cat("*** Logging out of the session ***")
})
}
# Run the application
shinyApp(ui = ui, server = server)

Connect to MySQL from shinyapps.io

I have a working Shiny App that queries a remote MySQL database via pool that I can run on my local machine.
The MySQL server has whitelisted shinyapps.io IP addresses.
When I deploy it to shinyapps.io, I get this error:
.global
library(shiny)
library(DBI)
library(pool)
library(DT)
pool <- dbPool(
drv = RMySQL::MySQL(),
dbname = "gw_observatory",
host = "sage.metro.ucdavis.edu",
username = "gw_observatory",
password = "password"
)
onStop(function() {
poolClose(pool)
})
.server
shinyServer(function(input, output, session) {
output$data_table <- renderDataTable({
DT::datatable(pool %>% tbl("small_data") %>% collect())
})
.ui
shinyUI(
fluidPage(
mainPanel(DT::dataTableOutput("data_table"))
)
)
UPDATE: It works now. My system admin added a port, and that fixed the issue.
For anyone down the line that is running into this issue, there are the steps I recommend:
make sure your shinyapp works locally
if you get deployment errors on shinyapps.io, make sure:
your database has whitelisted shinyappsio IP addresses
your host is an external, public IP or URL and not an internal one
some institutions have very guarded firewalls. Try adding a port to your database.
I hope this helps someone down the line!
pool <- dbPool(
drv = RMySQL::MySQL(),
dbname = "some_name",
host = "123.45.678.901",
username = "some_username",
password = "password",
port = 1234567
)

Failed to detect version from SPARK_HOME or SPARK_HOME_VERSION

I'm trying to follow a tutorial for using spark from RStudio on DSX, but I'm running into the following error:
> library(sparklyr)
> sc <- spark_connect(master = "CS-DSX")
Error in spark_version_from_home(spark_home, default = spark_version) :
Failed to detect version from SPARK_HOME or SPARK_HOME_VERSION. Try passing the spark version explicitly.
I took the above code snippet from the connect to spark dialog in RStudio:
So I took a look at SPARK_HOME:
> Sys.getenv("SPARK_HOME")
[1] "/opt/spark"
Ok, Lets check that dir exists:
> dir("/opt")
[1] "ibm"
I'm guessing this is the cause of the problem?
NOTE: there are a few similar questions on stackoverflow, but none of them are about IBM's Data Science Experience (DSX).
Update 1:
I tried the following:
> sc <- spark_connect(config = "CS-DSX")
Error in config$spark.master : $ operator is invalid for atomic vectors
Update 2:
An extract from my config.yml. Note that I have many more spark services in my, I've just pasted the first one:
default:
method: "shell"
CS-DSX:
method: "bluemix"
spark.master: "spark.bluemix.net"
spark.instance.id: "7a4089bf-3594-4fdf-8dd1-7e9fd7607be5"
tenant.id: "sdd1-7e9fd7607be53e-39ca506ba762"
tenant.secret: "xxxxxx"
hsui.url: "https://cdsx.ng.bluemix.net"
Note that my config.yml was generated for me.
Update 3:
My .Rprofile looks like this:
# load sparklyr library
library(sparklyr)
# setup SPARK_HOME
if (nchar(Sys.getenv("SPARK_HOME")) < 1) {
Sys.setenv(SPARK_HOME = "/opt/spark")
}
# setup SparkaaS instances
options(rstudio.spark.connections = c("CS-DSX","newspark","cleantest","4jan2017","Apache Spark-4l","Apache Spark-3a","ML SPAAS","Apache Spark-y9","Apache Spark-a8"))
Note that my .Rprofile was generated for me.
Update 4:
I uninstalled sparklyr and restarted the session twice. Next I tried to run:
library(sparklyr)
library(dplyr)
sc <- spark_connect(config = "CS-DSX")
However, the above command hung. I stopped the command and checked the version of sparklyr which seems to be ok:
> ip <- installed.packages()
> ip[ rownames(ip) == "sparklyr", c(0,1,3) ]
Package Version
"sparklyr" "0.4.36"
You cannot use master parameter to connect to bluemix spark service if that is the intent since your kernels are defined in config.yml file, you should be using config parameter instead to connect.
config.yml is loaded up with your available kernel information(spark instances).
Apache Spark-ic:
method: "bluemix"
spark.master: "spark.bluemix.net"
spark.instance.id: "41a2e5e9xxxxxx47ef-97b4-b98406426c07"
tenant.id: "s7b4-b9xxxxxxxx7e8-2c631c8ff999"
tenant.secret: "XXXXXXXXXX"
hsui.url: "https://cdsx.ng.bluemix.net"
Please use config
sc <- spark_connect(config = "Apache Spark-ic")
as suggested in tutorial:-
http://datascience.ibm.com/blog/access-ibm-analytics-for-apache-spark-from-rstudio/
FYI,
By Default, you are connected to , i am working on finding how to change version with config parameter.
> version <- invoke(spark_context(sc), "version")
print(version)
[1] "2.0.2"
Thanks,
Charles.
I had the same issue and fix it as follows:
go to C:\Users\USER_NAME\AppData\Local/spark/ and delete everything you'll find in the directory
Then, in the R console run:
if (!require(shiny)) install.packages("shiny");
library(shiny)
if (!require(sparklyr)) install.packages("sparklyr");
library(sparklyr)
spark_install()

APNS issue with django

I'm using the following project for enabling APNS in my project:
https://github.com/stephenmuss/django-ios-notifications
I'm able to send and receive push notifications on my production app fine, but the sandbox apns is having strange issues which i'm not able to solve. It's constantly not connecting to the push service. When I do manually the _connect() on the APNService or FeedbackService classes, I get the following error:
File "/Users/MyUser/git/prod/django/ios_notifications/models.py", line 56, in _connect
self.connection.do_handshake()
Error: [('SSL routines', 'SSL3_READ_BYTES', 'sslv3 alert handshake failure')]
I tried recreating the APN certificate a number of times and constantly get the same error. Is there anything else i'm missing?
I'm using the endpoints gateway.push.apple.com and gateway.sandbox.push.apple.com for connecting to the service. Is there anything else I should look into for this? I have read the following:
Apns php error "Failed to connect to APNS: 110 Connection timed out."
Converting PKCS#12 certificate into PEM using OpenSSL
Error Using PHP for iPhone APNS
Turns out Apple changed ssl context from SSL3 to TLSv1 in development. They will do this in Production eventually (not sure when). The following link shows my pull request which was accepted into the above project:
https://github.com/stephenmuss/django-ios-notifications/commit/879d589c032b935ab2921b099fd3286440bc174e
Basically, use OpenSSL.SSL.TLSv1_METHOD if you're using python or something similar in other languages.
Although OpenSSL.SSL.SSLv3_METHOD works in production, it may not work in the near future. OpenSSL.SSL.TLSv1_METHOD works in production and development.
UPDATE
Apple will remove SSL 3.0 support in production on October 29th, 2014 due to the poodle flaw.
https://developer.apple.com/news/?id=10222014a
I have worked on APN using python-django, for this you need three things URL, PORT and Certificate provided by Apple for authentication.
views.py
import socket, ssl, json, struct
theCertfile = '/tmp/abc.cert' ## absolute path where certificate file is placed.
ios_url = 'gateway.push.apple.com'
ios_port = 2195
deviceToken = '3234t54tgwg34g' ## ios device token to which you want to send notification
def ios_push(msg, theCertfile, ios_url, ios_port, deviceToken):
thePayLoad = {
'aps': {
'alert':msg,
'sound':'default',
'badge':0,
},
}
theHost = ( ios_url, ios_port )
data = json.dumps( thePayLoad )
deviceToken = deviceToken.replace(' ','')
byteToken = deviceToken.decode('hex') # Python 2
theFormat = '!BH32sH%ds' % len(data)
theNotification = struct.pack( theFormat, 0, 32, byteToken, len(data), data )
# Create our connection using the certfile saved locally
ssl_sock = ssl.wrap_socket( socket.socket( socket.AF_INET, socket.SOCK_STREAM ), certfile = theCertfile )
ssl_sock.connect( theHost )
# Write out our data
ssl_sock.write( theNotification )
# Close the connection -- apple would prefer that we keep
# a connection open and push data as needed.
ssl_sock.close()
Hopefully this would work for you.