与MySQL数据库的闪亮应用程序连接

时间:2016-10-03 16:14:19

标签: r shiny shiny-server rmysql

我在发布这个Shiny应用程序时遇到了一些麻烦。对于我的问题,我不认为ui.R是相关的。这是server.R:

library(shiny)
library(RMySQL)
library(tidyverse)
library(colorspace)
library(ggplot2)

###     CONNECT AND QUERY THE DATABASE

### uncomment the next line to reconnect to server & re-query the database

source("http://www.mvabl.com/Dashrock/MySQL_connect_query.R")

con <- dbConnect(MySQL(),
    user = 'shiny_apps',
    password = '####',
    host = 'mysql.mvabl.com',
    dbname='sandbox191')

qmain <- dbSendQuery(con, "SELECT * FROM sizes;")

sizes <- as.data.frame(dbFetch(qmain,n=-1),na.rm=TRUE)

###     GENERATE GGPLOT

colors17 <- c("#9D8FAC","#8E96B0","#7F9BB0","#71A0AD","#67A5A7","#63A89E","#64AB94","#6BAC88","#77AD7B","#85AD70","#94AC66","#A4AA5F","#B5A75B","#C4A35B","#D39F5F","#DF9C67","#E99872")

colors6 <- c("#9D8FAC","#6FA1AC","#66AB8F","#8EAC6A","#C1A45A","#E99872")

sizes$Size_Cat <- factor(sizes$Size_Cat,
                         levels = c("n1_4","n5_9","n10_19",
                                    "n20_49","n50_99","n100_249",
                                    "n250_499","n500_999","n1000",
                                    "n1000_1","n1000_2","n1000_3",
                                    "n1000_4"))
sizes$market <- factor(sizes$market,
                         levels = c("NYC","LA","CHI","DC","SF","BOS"))
sizes <- sizes %>%
  filter(market %in% c("NYC","SF"),
         Size_Cat %in% c("n50_99","n100_249","n250_499"))

shinyServer(function(input, output) {

  output$plot <- renderPlot({

    g <- ggplot(sizes)
    g + geom_bar(stat = "identity",
                 position = "dodge",
                 aes_string(x=input$x, y=input$y))
    if (input$color != 'None')
    g <- g + aes_string(color=input$color)

    facets <- paste(input$facet_row, '~', input$facet_col)
    if (facets != '. ~ .')
      g <- g + facet_grid(facets)

      g <- g + scale_fill_manual (values=colors17)
      g <- g + facet_wrap(~ input$facet_row)
      g <- g + labs( y = "Number of Companies", title = "Market Structure")
      g <- g + theme(strip.text.x = element_text(size = 8),
            axis.text.x  = element_text(angle=90, size=6)) 

    if (input$jitter)
      g <- g + geom_jitter()
    if (input$smooth)
      g <- g + geom_smooth()

    print(g)

  }, height=700)

})    
dbHasCompleted(qmain)
dbClearResult(qmain)
dbDisconnect(con)

我得到的结果是“找不到对象'尺寸”

问题是,当我在本地运行完全相同的代码,但 没有 Shiny部分时,我得到了预期的结果。这是代码:

library(RNeo4j)
library(tidyverse)
library(stringr)
library(MASS)
library(RColorBrewer)
library(colorspace)

### uncomment the next 2 lines to reconnect to server & re-query the database
 setwd("~/Desktop/Dashrock/")
 source("http://www.mvabl.com/Dashrock/MySQL_connect_query.R")

colors17 <- c("#9D8FAC","#8E96B0","#7F9BB0","#71A0AD","#67A5A7","#63A89E","#64AB94","#6BAC88","#77AD7B","#85AD70","#94AC66","#A4AA5F","#B5A75B","#C4A35B","#D39F5F","#DF9C67","#E99872")

colors6 <- c("#9D8FAC","#6FA1AC","#66AB8F","#8EAC6A","#C1A45A","#E99872")

sizes$Size_Cat <- factor(sizes$Size_Cat,
                         levels = c("n1_4","n5_9","n10_19",
                                    "n20_49","n50_99","n100_249",
                                    "n250_499","n500_999","n1000",
                                    "n1000_1","n1000_2","n1000_3",
                                    "n1000_4"))
sizes$market <- factor(sizes$market,
                         levels = c("NYC","LA","CHI","DC","SF","BOS"))
sizes <- sizes %>%
  filter(market %in% c("NYC","SF"),
         Size_Cat %in% c("n50_99","n100_249","n250_499"))

g <- ggplot(sizes)
      g + geom_bar(stat = "identity",
                   position = "dodge",
             aes(x = market, 
                 y = firms,
                 fill = industry),
             color = "grey") +
        scale_fill_manual (values=colors17) +
        facet_wrap(~ Size_Cat) +
        labs( y = "Number of Companies", title = "Market Structure") + 
        theme(strip.text.x = element_text(size = 8),
              axis.text.x  = element_text(angle=90, size=6))

“连接”源文件可以在网络上公开查看http://www.mvabl.com/Dashrock/MySQL_connect_query.R(完全是上面代码块中的URL),但是我确实对PW进行了哈希处理,因此这不可执行。 (我不知道如何屏蔽代码访问我的数据库的密码。)

最后,在我的第三方MySQL客户端(Sequel Pro&amp; MySQL Workbench)中,sizes表显示没有问题。

有人能发现问题吗?或者,更好的是,告诉我如何自己诊断它?

THX

1 个答案:

答案 0 :(得分:3)

我之前有类似的东西。尝试替换

g <- ggplot(sizes)

g <- ggplot(sizes())