对于许多专家来说这听起来很容易,但是花了好几个小时后我还没有想出一个合适的解决方案,我可能忽略了一些易于配置的东西.
我的问题是在部署到shinyapps.io之后,如何使这个闪亮的应用程序与云关系数据库(例如Google MySQL服务)交谈
我已经在我的Windows 7 64位计算机上成功启动了这个闪亮的应用程序,因为我已将用户DSN指定为google_sql,具有正确的驱动程序MysqL ODBC 5.3 ANSI驱动程序,ip,密码等,因此在代码行odbcConnect中我可以简单地提供dsn,用户名和密码来打开连接.然而,当我将它部署到shinyapps.io时,它失败了我的期望.我的猜测是我的DSN google_sql无法被shinyapps.io识别,所以为了让它正常工作,我该怎么办?我应该更改一些代码吗?或者在shinyapps.io上配置
PS:这不是关于如何安装RMysqL,有人在这里发布类似的问题(除非他们认为RMysqL可以做一些RODBC不能做的事情)
connecting shiny app to mysql database on server
server.R
library(shiny)
# library(RODBC)
library(RMysqL)
# ch <- odbcConnect(dsn = "google_sql",uid = "abc",pwd = "def")
ch <- dbConnect(MysqL(),user='abc',password='def',host = 'cloud_rdb_ip_address',dbname = 'my_db')
shinyServer(function(input,output) {
statement <- reactive({
if(input$attribute == 'All'){
sprintf("SELECT * FROM test_db WHERE country = '%s' AND item = '%s' AND year = '%s' AND data_source = '%s'",input$country,input$item,input$year,input$data_source)
}else{
sprintf("SELECT * FROM test_db WHERE country = '%s' AND item = '%s' AND attribute = '%s' AND year = '%s' AND data_source = '%s'",input$attribute,input$data_source)
}
})
output$result <- renderTable(dbFetch(dbSendQuery(ch,statement=statement()),n=1000))
})
ui.R
library(shiny)
shinyUI(fluidPage(
# Application title
headerPanel("Sales Database User Interface"),fluidRow(
column(4,selectInput('country','Country',c('United States','European Union','China'),selected = NULL),selectInput('item','Item',c('Shoes','Hat','Pants','T-Shirt'),selectInput('attribute','Attribute',c('All','Sales','Procurement'),selected = NULL)
),column(4,selectInput('year','Calendar Year',c('2014/2015','2015/2016'),selectInput('data_source','Data Source',c('Automation','Manual'),selected = NULL)
)
),submitButton(text = "Submit",icon = NULL),# Sidebar with a slider input for the number of bins
# Show a plot of the generated distribution
mainPanel(
tableOutput("result")
)
))
我认为值得发布我的闪亮showLogs()错误日志专家来启发我,
2015-05-04T06:32:16.143534+00:00 shinyapps[40315]: R version: 3.1.2
2015-05-04T06:32:16.392183+00:00 shinyapps[40315]:
2015-05-04T06:32:16.143596+00:00 shinyapps[40315]: shiny version: 0.11.1
2015-05-04T06:32:16.392185+00:00 shinyapps[40315]: Listening on http://0.0.0.0:51336
2015-05-04T06:32:16.143598+00:00 shinyapps[40315]: rmarkdown version: NA
2015-05-04T06:32:16.143607+00:00 shinyapps[40315]: knitr version: NA
2015-05-04T06:32:16.143608+00:00 shinyapps[40315]: jsonlite version: NA
2015-05-04T06:32:16.143616+00:00 shinyapps[40315]: RJSONIO version: 1.3.0
2015-05-04T06:32:16.143660+00:00 shinyapps[40315]: htmltools version: 0.2.6
2015-05-04T06:32:16.386758+00:00 shinyapps[40315]: Using RJSONIO for JSON processing
2015-05-04T06:32:16.386763+00:00 shinyapps[40315]: Starting R with process ID: '27'
2015-05-04T06:32:19.572072+00:00 shinyapps[40315]: Loading required package: DBI
2015-05-04T06:32:19.831544+00:00 shinyapps[40315]: Error in .local(drv,...) :
2015-05-04T06:32:19.831547+00:00 shinyapps[40315]: Failed to connect to database: Error: Lost connection to MysqL server at 'reading initial communication packet',system error: 0
2015-05-04T06:32:19.831549+00:00 shinyapps[40315]:
PS:我想我需要将shinyapps.io ip地址列入我的Google可以,以便在shinyapps.io上启用部署.
最佳答案
我实际上设法自己想出了这个问题的答案,但是想分享答案,因为它也可能与其他人有关.
原文链接:https://www.f2er.com/mysql/434099.html以下是白名单所需的IP地址.
54.204.29.251
54.204.34.9
54.204.36.75
54.204.37.78