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
}))
}
Could anyone please help me out here? 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 sth weired 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:
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:
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 firewall or sth could have sth to do with this error. But if I do not use SQL here, it is OK
Could anyone please help me out here?