【发布时间】:2015-07-09 22:43:48
【问题描述】:
这对许多专家来说可能听起来很容易,但在花了几个小时后我还没有想出正确的解决方案,我可能忽略了一些易于配置的东西。
我的问题是,在部署到 shinyapps.io 之后
,如何让这个闪亮的应用与云关系数据库(例如 Google MySQL 服务)通信我已经在我的 Windows 7 64 位机器上成功启动了这个闪亮的应用程序,因为我已经使用正确的驱动程序 MySQL ODBC 5.3 ANSI 驱动程序将 User DSN 指定为 google_sql ,ip,密码等,所以在代码行odbcConnect我可以简单地提供dsn,用户名和密码来打开一个连接。然而,当我将它部署到 shinyapps.io 时,它以我的期望失败了。我的猜测是 Shinyapps.io 无法识别我的 DSN google_sql,所以为了使其正常工作,我应该怎么做?我应该更改一些代码吗?或者在 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$country,input$item,input$attribute,input$year,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'),selected = NULL),
selectInput('attribute','Attribute',c('All','Sales','Procurement'),selected = NULL)
),
column(4,
selectInput('year','Calendar Year',c('2014/2015','2015/2016'),selected = NULL),
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")
)
))
我认为值得将我的shiny 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 上的部署。
【问题讨论】:
-
基本上,问题是关于:在将shinyapps.io 与托管在AWS 云或Google SQL 中的RDS 数据库一起使用时,该数据库通过将IP 地址列入外部服务器的白名单来工作。是否有 Shinyapps 使用的 IP 地址/范围,以便云数据库用户可以将其列入白名单并授予对数据库的访问权限以获取数据。