r - How to create graph using mysql table in shiny? -
i'm new r , shiny, please bear me if there syntax or basic errors. i'm trying create bar graph using mysql table. table looks like:
+--------------+------------------------+--------+ | 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 | +--------------+------------------------+--------+
i want epic name in x-axis , results in y-axis.
here shiny app code.
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 2017_8_16;") } detail = data.frame(x_names, loaddata) shinyserver(function(input, output){ bar2 <- tapply(detail) barplot(bar2) })
any appreciated. thanks!
your ui , server logic must refer same id of element. therefore ui code should read plotoutput("someid") container plot , server code creates plot in output$someid = renderplot({...})
since you shiny why not use advantage of interactivity rather static plots basic r, here have introduction amcharts implementation in r https://datastorm-open.github.io/introduction_ramcharts/ see below example:
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']]) }) }) ))
Comments
Post a Comment