Shiny App Connect dbPool through ODBC or specific driver - r

I'm struggling to connect my shiny application to one of the database we use in our company. I've succesfully connected to Azure / Mongo / SQL Server databases but now I've got a SAP SQL Anywhere 17 database to connect to.
Not surprisingly there's no specific connection to that database provided in the R Drivers (https://www.rstudio.com/products/drivers/).
Now I can solve this in two ways I believe, our IT department is convinced that a generic ODBC connection should work, or I have to get the specific SQL Anywhere drivers installed on my shiny app somehow.
For both solutions I can't find much online. If I search for generic ODBC connection the recommendations go to FreeTDS which is in the RODBC package, which then might not work together with Pool (according to what I've read).
Searching how to install specific drivers on a shiny application is also not bringing me much.

Try using
options(java.parameters="-Xmx8g")
library(RJDBC)
drv <- JDBC("com.microsoft.sqlserver.jdbc.SQLServerDriver", "drivers/jdbc/mssql-jdbc-8.4.1.jre8.jar", identifier.quote="`")
processingStart = Sys.time()
conn <- dbConnect(drv,
"jdbc:sqlserver://SERVER_NAME;databaseName=DATABASE_NAME",
user = "USER_NAME",
"PASSWORD"
)

Related

Truncation of database name and columns while connecting to SQL database from R

I am on macOS Catalina (Version 10.15.1), running R 3.5.0. I am running SQL server on Docker locally. For connecting to the server, I am using odbc:
con <- dbConnect(odbc(),
Driver = "Simba SQL Server ODBC Driver",
Server = "localhost",
UID = 'SA',
PWD = 'XXXXXXXX',
database = dbname)
I am able to connect to the server, however the names of all the databases therein are getting truncated to just the first letter as shown here:
Subsequently all the character columns are also showing only the first letter.
I had a look at this, but just can't figure out why this is happening. For starters, the names of the databases itself, as shown in the RStudio Connections pane, are getting truncated.
I am able to connect to the databases using Azure Data Studio and see all the columns correctly.
I had a look at this and turns out this was exactly what I was facing. Followed the steps mentioned here to resolve the issue.

Connecting to an Azure SQL Server Data Warehouse from R on a Mac - See random names instead of tables

I'm trying to connect to an Azure SQL Server (12.00.1900) from R on a Mac, using Microsoft's unixodbc SQL Server drivers (17).
I get a connection, but instead of seeing the 12 or so tables that live in the database, dbListTables returns 442 tables, all with nonsensical names, beginning with 'Csoe', 'Ote', and ending in 'xlshm_idad'. Instead of seeing the single schema that lives in the database, I see cin_1mro__e, IFRAINSHM, and s, none of which have any tables in them.
Note that when I use an ordinary SQL visualization app, that doesn't use the MS drivers, I'm able to see the tables and their content properly.
In addition, the RSQLServer package gets a working connection and sees the tables correctly, but isn't compatible with dplyr semantics.
Can anyone help or advise? I've looked for third party SQL Server unixodbc drivers for Mac, and I can't find any.
Until I see more info from OP, I'll leave as my answer the general recommendation to use R's odbc package. Assuming the correct drivers are installed, connection is configured correctly in odbc.ini, and assuming trusted_connection=yes is used in the same, then connecting from R is as simple as:
library(odbc)
dbConn <- dbConnect(odbc(), dsn = "myDSN")
if trusted connection is not on then you just need to pass uid and pwd arguments.
Also, it may be the case OP that you did not install freeTDS, so try (replace with equivalent for package manager you're using):
brew install freetds --with-unixodbc
This gives you the libtdsodbc.so driver. Make sure the DSN points to this.

R integration with asp.net and ssas

Maybe someone knows if it's possible to send data from ms sql database to R server, so it could calculate some columns and send them back to ms sql again?
I am not that much familiar with R server's integration and I am cautious that is not even possible. If this is not possible, maybe it would be possible to send them from asp.net mvc 5 using integration from .net library, but I do not think that is a good solution because the data can have more then 500k rows so it would be extremely slow.
Querying a database from a server running R requires three things:
Network security that allows you to communicate between the machines
Drivers installed on the R server
Configurations that allow you to connect to the database from R
In general, it is best to have your IT/Ops team take care of the Networking security and the installation of drivers, since these are things that they likely have security procedures around. We recommend using the RStudio Professional Drivers, which are easy to install and designed to work with our products.
Then, when it comes to the connection from R to the database, we recommend using the odbc package, which is a DBI compliant interface to using ODBC drivers. You can acquire the latest stable version from CRAN with install.packages("odbc").
In general, a connection looks something like this:
library(odbc)
con <- dbConnect(odbc(),
Driver = "SQLServer",
Server = "mysqlhost",
Database = "mydbname",
UID = "myuser",
PWD = rstudioapi::askForPassword("Database password")
Port = 1433)
The rstudioapi::askForPassword function will prompt the user for a password and reduce the need to store passwords in code. For more options on securing credentials, there is a dedicated article on the topic. Note that there is also support for DSNs:
# Using a DSN
con <- dbConnect(odbc::odbc(), "mydbalias")
for other reference please visit this url.
Hope it helps.

Connect with R/RStudio to an Oracle DB using Wallet

H, I'm quite a newbie on this.
I have a Unix machine with R/RStudio and Python installed.
I need to connect to an Oracle DB in a secure way.
I have created a wallet with mkstore given by Oracle and with Python and cx_Oracle library I have no problems in connecting to this DB, but how can I afford the same result with R?
There's an example in the ROracle doc:
## Create connection authenticated with external credentials.
con <- dbConnect(drv, username ="", password="", external_credentials = TRUE)
You'll probably need to pass dbname too.

Connect to a named SQL Server 2016 instance from R

I'm trying to connect to a SQL Server 2016 database in RStudio. I'm using RStudio on my laptop. I could remote in to the server and install RStudio there if it were absolutely necessary, but working locally has massive advantages so I would really prefer that if it were possible. Connection with the server goes through a VPN (FortiClient) that I have running on my laptop.
On this server, there are two SQL Server instances. One is a SQL Server 2012 edition, which is the default instance and hence not named - it used to be the only instance on this server. The other one is the 2016 edition. This instance was set up more recently in order to use the R integration capabilities new to SQL Server 2016. Because the server already had a default instance, this instance had to be named and it hence is called DEVR.
When I access the instances in SSMS and click 'Properties', the name of the default instance is DWH-ACC and the 2016 instance is called DWH-ACC\DEVR.
This is the code I'm running in RStudio to test my connection:
server <- "[IP-ADDRESS]\\DWH-ACC\\DEVR"
databaseName <- "Database"
user <- "user"
pwd <- "password"
sqlShareDir <- "C:\\Dir"
sqlWait <- TRUE
sqlConsoleOutput <- FALSE
sampleDataQuery <- "SELECT TOP 10 FROM [dbo].[Table]"
cc <- RxInSqlServer(server = server, databaseName = databaseName, user = user, password = pwd, shareDir = sqlShareDir, wait = sqlWait, consoleOutput = sqlConsoleOutput)
rxSetComputeContext(cc)
inDataSource <- RxSqlServerData(sqlQuery = sampleDataQuery, server=server, databaseName=databaseName, user=user, password=pwd, stringsAsFactors=TRUE, rowsPerRead=500)
rxGetVarInfo(data = inDataSource)
I've tried several options for the server specification, among which [IP-ADDRESS]\\DEVR and [IP-ADDRESS]/DEVR, which both do not work either. This is the error I get when I run the code:
[Microsoft][ODBC SQL Server Driver][DBNETLIB]The SQL-Server does not exist or permission has been denied.
Could not open data source.
ODBC Error in SQLDisconnect
(Message translated from Dutch, by the way - this may not be the exact error text in the English version of the software)
When I try simply the IP address as my server connection string, I get a different error that seems to indicate it is able to find the instance (the 2012 one, i.e. the wrong one) but not able to process the query.
[Microsoft][ODBC-stuurprogrammabeheer] Fout in functievolgorde
I'm not sure how to translate this one, but it seems to be related to ODBC-drivers and says "error in function order". Anyway, this error is unrelated and I don't need it solved or explained, it simply goes to show that R does seem to be able to connect to the default instance but not to the newer, named one.
Enable Implied Authentication for Launchpad Accounts
Specifically navigate to the User Account from the Control Panel and you'll see the SQLR UserGroup with 20 accounts.
Permission these on the Server and DB Table with write access.
That should see you right. Good luck
You could create a ODBC connection in your local instance. A tutorial on creating ODBC connections can be found here. Background about the different types of SQL Server ODBC connections can be found here.
The ODBC connection should be able to distinguish between the the different SQL Server instances.
For me the main advantage in using ODBC connections is that I don't have to store database passwords inside/near my R scripts.
In R/Rstudio you can connect to the SQL Server instance via the ODBC channel. A tutorial on ODBC channels and RevoScaleR: link.
Other packages in R also provide possibilities to connect to ODBC connections, for instance: RODBC, dplyr.

Resources