gpt4 book ai didi

mysql - 如何使用 Shiny 的mysql表创建图形?

转载 作者:行者123 更新时间:2023-11-29 18:29:49 25 4
gpt4 key购买 nike

我是 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 轴上显示结果。

这是我 Shiny 的应用程序代码。

Ui.R

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

mainPanel(
plotOutput("")
)
))

服务器.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)

})

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

最佳答案

您的 ui 和服务器逻辑必须引用元素的相同 id。因此,ui代码应该读取plotOutput(“someID”),它是绘图和服务器代码的容器,然后在output$someID = renderPlot({...})中创建绘图本身

既然你很 Shiny ,为什么不利用基本 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']])
})
})
))

关于mysql - 如何使用 Shiny 的mysql表创建图形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45790763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com