0

First of all, I need to use R to get SQL query result from HANA database, which I finish by using RODBC in Rstudio.

Second of all, I need to share my code with others, which I use shinyapps.io to finish.

However, I need to use shinyapps to show my SQL query result on other computers, which I have the following error message:

error first argument is not an open rodbc channel

I used the answer from R shiny RODBC connection Failing, but it still does not work.

Here is my codes for ui.R and sever.R attached:

ui.R:

library(dplyr)
library(RODBC)
library(stringr)
library(ggplot2)
fluidPage(
  titlePanel("Basic DataTable"),
  fluidRow(
    DT::dataTableOutput("table")
  )
)

sever.R:

library(dplyr)
library(RODBC)
library(stringr)
library(ggplot2)
ch<-odbcConnect('HANARB1P',uid='****',pwd='****')
options(scipen = 200)
myOffice <- 0
StartDate <- 20170601
EndDate <- 20170610
office_clause = ""
if (myOffice != 0) {
  office_clause = paste(
    'AND "_outer"."/BIC/ZSALE_OFF" IN (',paste(myOffice, collapse=", "),')'
  )
}
function(input, output) {
  output$table <- DT::renderDataTable(DT::datatable({
  data <- sqlQuery(channel=ch,query=paste(' SELECT TOP 100
                                                   "/BIC/ZSALE_OFF" AS "SalesOffice",
                                                   "/BIC/ZHASHPAN" AS "CreditCard"
                                            FROM "SAPB1P"."/BIC/AZ_RT_A212"
                                            WHERE "CALDAY" BETWEEN',StartDate,'AND',EndDate,'
                                                  ',office_clause,'
                               '))
    data
  }))
}

How to use shinyapps.io and RODBC to show the SQL query result on the webpages for sharing?

According to the answer, I revised my code a little bit. But something weird happens again. When I use the code:

function(input, output) {
  output$table <- DT::renderDataTable(DT::datatable({
    data <- sqlQuery(channel=ch,query=paste(' SELECT TOP 50
                                                 "/BIC/ZSALE_OFF" AS "SalesOffice",
                                                     "/BIC/ZHASHPAN" AS "CreditCard"
                                              FROM "SAPB1P"."/BIC/AZ_RT_A212"
                                              WHERE "CALDAY" BETWEEN',StartDate,'AND',EndDate,'
                                                    ',office_clause,'
                                            '))
    data
  }))
}

I have the error information:

enter image description here

When I use the code:

shinyServer(
function(input, output) {
  data <- sqlQuery(channel=ch,query=paste(' SELECT TOP 50
                                                 "/BIC/ZSALE_OFF" AS "SalesOffice",
                                                     "/BIC/ZHASHPAN" AS "CreditCard"
                                              FROM "SAPB1P"."/BIC/AZ_RT_A212"
                                              WHERE "CALDAY" BETWEEN',StartDate,'AND',EndDate,'
                                                    ',office_clause,'
                                            '))
  output$table <- DT::renderDataTable(data)
}
)

I have the error information:

enter image description here

I am sure the channel works. If I just use run app to do this:

shiny::runApp('//paper/fchen4/feng.officeworks/mycode/myShiny')

It works fine. But I work in a company, I do not know if my firewall could have something to do with this error. But if I do not use SQL here, it is OK.

halfer
  • 19,824
  • 17
  • 99
  • 186
Feng Chen
  • 2,139
  • 4
  • 33
  • 62

1 Answers1

2

Well, have you checked that the channel is actually open? The error message can be the result of wrong credentials, unreachable server or anything else that would prevent a successful SQL connection.

I had no problems showing table content with the following code:

ui.R

library(shiny)

# Define UI for application that draws a histogram
shinyUI(fluidPage(

  # Application title
  titlePanel("Basic Data Table"),       
     fluidRow(
        dataTableOutput("table")
     )
))

server.R

library(shiny)
library(RODBC)

ch <- odbcConnect("S12")

# Define server logic to provide table output
shinyServer(
    function(input, output) {

        query_result <- sqlQuery(channel = ch, query = 'SELECT * FROM M_DATABASE')
        output$table <- renderDataTable(query_result)
    }
)

There is no reason to call DT::datatable() around the result of the SQL query as it already returns a data frame that can be fed into renderDataTable().

A few general hints:

  • never put your logon data into the application code. In SCN Blog "HANA quick note – checking my connections and using them securely …" I explained how to securely store and use connection and logon data for SAP HANA systems. This also gives you a very easy way to check the connectivity to your HANA instance. Besides, just pointing to the ODBC DSN connection instead of providing all the parameters looks much cleaner.

  • You don't need all the R libraries in the ui.R file as the code that uses libraries like RODBC is in the server.R file. Make sure to have the minimum required libraries in every file to make your life a lot easier.

  • It doesn't hurt to break up long nested function parameter calls as I did it with your "calling-SQL-statement-convert-resultset-data-type-feed-it-into-render-function". It's a lot easier to follow what happens where and what fails where, when there are not too many commands in a single line.

This should work for you.

Lars Br.
  • 9,949
  • 2
  • 15
  • 29
  • Thanks a lot for Lars Br.'s answer. But I still have some problem. I will hide my login information in next step. I am sure the channel is working because when I use shiny::runApp('//paper/fchen4/feng.officeworks/mycode/myShiny'), it is ok. Could you please take a look at here again? I add some new feedback based on your answer in my question – Feng Chen Jun 26 '17 at 22:48
  • I think you first should ensure that the connectivity between the SHINY server and HANA is working. ODBC DSNs like your `HANARB1P` are usually dependent on the OS user. So, if the SHINY server part runs on a different machine and/or a different user, this DSN needs to be set up for this user as well. – Lars Br. Jun 26 '17 at 23:53
  • I use shinyapps.io based on the manual from https://www.shinyapps.io/. I think it runs on a different machine. But I do not understand what you mean a different user? I saw some information about odbcDriverConnect like https://support.rstudio.com/hc/en-us/articles/225408367-Connecting-to-databases-using-RODBC-on-shinyapps-io. Do you think I should try odbcDriverConnect? – Feng Chen Jun 27 '17 at 00:45
  • The RODBC library uses connection details from the ODBC DSN (Data Source Name). The DSN is a configuration setting, that has to be done on the machine and with the operating system user that should use the connection. If the DSN is not set up on the server machine you run your code on, then no ODBC command or library will work. Best get in touch with whoever maintains the server machine and configure the DB access there. Also note, that for ODBC to work on any machine, the HANA client software has to be installed on that machine. – Lars Br. Jun 27 '17 at 00:49