Nikita Kagan
Nikita Kagan

Reputation: 75

Bar Chart Plotly doesn't show in shiny web app

I am trying to display a bar chart in my web application but instead have a white area. I looked at my code outside the application code works. What is the problem? help? My Bar chart code

output$revenuebyPrd <- renderPlot({
    plot_ly(data,
      x = ~company_name,
      y = ~revenue,
      type = "bar"
    )
  })

My data can help:

mydb = dbConnect(MySQL(), user='root', password='xxxx', dbname='app', host='localhost')
  on.exit(dbDisconnect(mydb), add = TRUE)
  rs=dbGetQuery(mydb, "SET NAMES 'cp1251'") 
  rs=dbSendQuery(mydb,"select * from companies")
  data=fetch(rs,n=-1)
  rs1=dbSendQuery(mydb,"select revenue from companies")
  revenue=fetch(rs1,n=-1)
  rs2=dbSendQuery(mydb,"select industry from companies")
  industry=fetch(rs2,n=-1)
  rs3=dbSendQuery(mydb,"select company_name from companies")
  industry=fetch(rs3,n=-1)

In Shiny

enter image description here

Without Shiny

enter image description here

Upvotes: 1

Views: 275

Answers (1)

Ismail T
Ismail T

Reputation: 51

When rendering objects on the server side of a Shiny app, you would need to use the accompanying render_() function. For example, to render a dataframe, you use renderTable(). If you are trying to render an object from a third-party package (e.g. DT, Plotly), and that object is compatible with Shiny, the package should include an accompanying render_() function (e.g. DT::renderDT(), plotly::renderPlotly()).

In your case, you are using the third-party package "plotly". Plotly comes with accompanying renderPlotly() and plotlyOutput() functions for use in Shiny. Therefore, your code should be as follows:

output$revenuebyPrd <- renderPlotly({
  plot_ly(data,
    x = ~company_name,
    y = ~revenue,
    type = "bar"
  )
})

Upvotes: 1

Related Questions