如何在闪亮的情况下使用mysql表创建图形?

时间:2017-08-21 06:44:24

标签: mysql r plot graph shiny

我是R的新手并且有光泽,所以如果有语法或一些基本错误,请耐心等待。 我正在尝试使用mysql表创建一个条形图。表格如下:

+--------------+------------------------+--------+
| SerialNumber | EpicName               | Result |
+--------------+------------------------+--------+
|            1 | UnifiedSearch_Pass     |    177 |
|            2 | UnifiedSearch_Fail     |      8 |
|            3 | Settings_Pass          |     57 |
|            4 | Settings_Fail          |      5 |
|            5 | Map_Overview_Pass      |     90 |
|            6 | Map_Overview_Fail      |      6 |
|            7 | Map_Guidance_Pass      |     48 |
|            8 | Map_Guidance_Fail      |      3 |
|            9 | RouteBar_Pass          |     48 |
|           10 | RouteBar_Fail          |      6 |
|           11 | MainMenu_Pass          |    109 |
|           12 | MainMenu_Fail          |     12 |
|           13 | SpeedCameras_Pass      |     17 |
|           14 | SpeedCameras_Fail      |      2 |
|           15 | MapManagement_Pass     |     14 |
|           16 | MapManagement_Fail     |      0 |
|           17 | AccountManagement_Pass |     12 |
|           18 | AccountManagement_Fail |      0 |
|           19 | VoiceManagement_Pass   |     10 |
|           20 | VoiceManagement_Fail   |      0 |
|           21 | Total_Automated_Tests  |    624 |
|           22 | Total_Pass             |    582 |
|           23 | Total_Fail             |     42 |
+--------------+------------------------+--------+

我想要x轴上的史诗名称并导致y轴。

这是我闪亮的应用代码。

Ui.R

shinyUI(fluidPage(
  titlePanel("Bar Chart"),

  mainPanel(
    plotOutput("")
  )
))

server.R

library(shiny)
library(RMySQL)

con <- dbConnect(MySQL(),
                 dbname = "mydb",
                 host = "localhost",
                 user = "root", 
                 password = "root")

x_names = c("UnifiedSearch_Pass, UnifiedSearch_Fail, Settings_Pass, Settings_Fail, Map_Overview_Pass, Map_Overview_Fail, Map_Guidance_Pass, Map_Guidance_Fail, RouteBar_Pass, RouteBar_Failm, MainMenu_Pass, MainMenu_Fail, SpeedCameras_Pass, SpeedCameras_Fail, MapManagement_Pass, MapManagement_Fail, AccountManagement_Pass, AccountManagement_Fail, VoiceManagement_Pass, VoiceManagement_Fail, Total_Automated_Tests, Total_Pass, Total_Fail")

loadData = function(){
  dbGetQuery(conn = con, statement = "SELECT Result FROM 2017_8_16;")
}

DETAIL = data.frame(x_names, loadData)

shinyServer(function(input, output){

bar2 <- tapply(DETAIL)
barplot(bar2)

})

任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

您的ui和服务器逻辑必须引用该元素的相同ID。 因此,ui代码应该读取plotOutput(&#34; someID&#34;),这是您的绘图和服务器代码的容器,然后在输出中创建绘图本身$ someID = renderPlot({...})

因为你是有光泽的,为什么不利用交互性的优势而不是基本R的静态图, 在这里,您对R中的amCharts实现有很好的介绍 https://datastorm-open.github.io/introduction_ramcharts/ 见下面的例子:

library(shiny)
library(data.table)
library(rAmCharts)

runApp(launch.browser = TRUE,
       shinyApp(
       ui=shinyUI(fluidPage(titlePanel("Bar Chart"),mainPanel(amChartsOutput("am_barplot"),plotOutput("basic_barplot")))),
       server=shinyServer(function(input,output,session){
         plot.data = reactive({
           table.text = "1 | UnifiedSearch_Pass     |    177
           2 | UnifiedSearch_Fail     |      8
           3 | Settings_Pass          |     57
           4 | Settings_Fail          |      5 
           5 | Map_Overview_Pass      |     90 
           6 | Map_Overview_Fail      |      6 
           7 | Map_Guidance_Pass      |     48 
           8 | Map_Guidance_Fail      |      3 
           9 | RouteBar_Pass          |     48 
           10 | RouteBar_Fail          |      6 
           11 | MainMenu_Pass          |    109 
           12 | MainMenu_Fail          |     12 
           13 | SpeedCameras_Pass      |     17 
           14 | SpeedCameras_Fail      |      2 
           15 | MapManagement_Pass     |     14 
           16 | MapManagement_Fail     |      0 
           17 | AccountManagement_Pass |     12 
           18 | AccountManagement_Fail |      0 
           19 | VoiceManagement_Pass   |     10 
           20 | VoiceManagement_Fail   |      0 
           21 | Total_Automated_Tests  |    624 
           22 | Total_Pass             |    582 
           23 | Total_Fail             |     42"
           plot.data = as.data.table(read.table(sep = "|",text = table.text))
           setnames(x=plot.data,c('SerialNumber','EpicName','Result'))
           return(plot.data)
         })
         output$am_barplot = renderAmCharts({
          expr=amBarplot(data=plot.data(),x='EpicName',y='Result', labelRotation = 90L)
         })
         output$basic_barplot = renderPlot({
           barplot(height=plot.data()[['Result']],names.arg = plot.data()[['EpicName']])
         })
       })
))