与ShinyApp密谋?

时间:2020-05-19 18:59:20

标签: r shiny plotly shinyapps

编辑:已解决。

我不确定问题出在哪里,但我想这与Rstudio有关。 当我将应用程序上传到https://shinyapps.io/时,它可以正常工作!

我正在尝试使用Shinyapp渲染一个绘图对象,

我在线阅读了许多查询,其中大多数是关于使用“ renderPlotly”而不是“ renderPlot”的,但是以某种方式我的图没有显示。

当我尝试使用ggplot时,效果很好。

我在做什么错了?

感谢您的帮助,并附上代码:

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 个答案:

答案 0 :(得分:0)

该示例不完整,存在多个问题。如果我们不推荐不完整的数据分析部分,并用随机测试数据代替它,则该方法在技术上会起作用:

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)

所以我的建议是先将数据分析部分固定在闪亮的外部,然后当一切正常时,将各个部分放在一起。