0
votes

Edit: Solved.

I am not sure what the problem was, but I guess it is about Rstudio. When I uploaded the app to https://shinyapps.io/ , it works!

I am trying to render a plotly object with a shinyapp,

I read many queries online, most of them was about using "renderPlotly" instead of "renderPlot", but somehow my plot is not showing.

When I try with ggplot, it works great.

What am I doing wrong?

Thanks for any help, code attached:

library("shiny")
library("plotly")
library("shinydashboard")

ui <- dashboardPage(dashboardHeader(title = "Basic dashboard"), 
dashboardSidebar(),  
dashboardBody(
fluidRow(
  box(plotlyOutput("plot1",height = 250)),

  box(
    title = "Controls", 
    sliderInput("slider", "Slider Value:", 1, 10, 5)
    )
   )
  )
)

server <- function(input, output) {

 output$plot1 <- renderPlotly({

clusters = my_classifier(k=input$slider, data=df)
results_df = cbind(df,as.factor(clusters))
colnames(results_df) = c("x","y","z","color")

plot_ly(data=results_df, x=~x, y=~y, z=~z, 
        type="scatter3d", mode="markers", color=~color)

})
}

 # Run the application 
 shinyApp(ui = ui, server = server)
1

1 Answers

0
votes

The example is not complete and has several issues. It works technically if we outcomment the incomplete data analysis part and replace it with random test data:

library("shiny")
library("plotly")
library("shinydashboard")

ui <- dashboardPage(dashboardHeader(title = "Basic dashboard"), 
                    dashboardSidebar(),  
                    dashboardBody(
                      fluidRow(
                        box(plotlyOutput("plot1",height = 250)),

                        box(
                          title = "Controls", 
                          sliderInput("slider", "Slider Value:", 1, 10, 5)
                        )
                      )
                    )
)

server <- function(input, output) {
  output$plot1 <- renderPlotly({
    #clusters = my_classifier(k=input$slider, data=df)
    #results_df = cbind(df,as.factor(clusters))
    #colnames(results_df) = c("x","y","z","color")

    ## random test data set
    results_df <- data.frame(x=runif(10), y=runif(10), z=rnorm(10), color=1:10)

    plot_ly(data=results_df, x=~x, y=~y, z=~z, 
            type="scatter3d", mode="markers", color=~color)
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

So my suggestion is to fix the data analysis part outside of shiny first, and when everything works, put the pieces together.