首页 > 解决方案 > 与 ShinyApp 密谋?

问题描述

编辑:已解决。

我不确定问题出在哪里,但我猜是关于 Rstudio。当我将应用程序上传到https://shinyapps.io/时,它可以工作了!

我正在尝试用一个闪亮的应用程序渲染一个情节对象,

我在网上阅读了很多查询,其中大部分是关于使用“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)

标签: rshinyplotlyshinyapps

解决方案


该示例不完整并且有几个问题。如果我们排除不完整的数据分析部分并将其替换为随机测试数据,它在技术上是有效的:

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)

所以我的建议是先修复shiny之外的数据分析部分,当一切正常时,将各个部分放在一起。


推荐阅读