将ggplot2更改为使用checkboxInput

时间:2019-01-23 17:35:17

标签: r ggplot2 shiny plotly

我有一个用于K-Means聚类的闪亮应用程序:

enter image description here

ui <- navbarPage("My Application",
                 tabPanel("K-Means", icon = icon("folder-open"),
                          sidebarLayout(
                            sidebarPanel(
                              sliderInput("num_centers", 
                                          label = "Select K",
                                          min = 2,
                                          max = 10,
                                          value = 2),
                              checkboxInput("plotly_checkbox", label = "Interactivity with plotly", value = TRUE)
                              ),
                            mainPanel(
                              plotOutput("kmeans")
                            )
                          )
                 )
)


server <- function(input, output, session) {

  output$kmeans <- renderPlot({

    # Require number of centers
    req(input$num_centers)

    # K-Means Algorithm
    k_centers <- reactive({kmeans(x = harvard_scaled, centers = input$num_centers)})
    harvard_cluster <- augment(k_centers(), harvard_processed)


    # Static Plot 
    harvard_cluster %>% 
      janitor::clean_names() %>% 
      ggplot(aes(nevents, nplay_video, color = cluster)) +
      geom_point() +
      labs(x = "# of interactions with the course",
           y = "# of play video events",
           color = "Cluster") +
      xlim(0, 52000) +
      ylim(0, 12500) +
      ggtitle(paste("K-Means Clustering of students where", "K =", input$num_centers))
  })
}



# Create Shiny app object
shinyApp(ui = ui, server = server)
  • 如您所见,我想允许用户使用plotly选择并查看checkboxInput图。
  • 我尝试了ifelse无济于事,因为一个是静态图并且需要renderPlot,另一个是交互式图并且需要renderPlotly

有人知道如何使用户能够选择应用显示plotly还是ggplot图形吗?

编辑:示例数据集(注意:这是一个虚拟数据集,与我上面绘制的数据集不同):

structure(c(0.150884824647657, 0.150884824647657, 0.449543446630647, 
0.217253407310543, -0.230734525663942, -0.330287399658272, -0.960788934955696, 
0.715017777282194, 0.449543446630647, -0.147773797335334, -0.380063836655437, 
-0.612353875975541, -0.463024564984046, -0.811459623964201, -1.60788261591884, 
-1.60788261591884, -0.89442035229281, 2.04238943053993, 1.7105465172255, 
2.29127161552575, 0.233845552976265, -0.761683186967036, -0.811459623964201, 
-1.12671039161291, -0.147773797335334, 1.19619000158812, 0.980492107933741, 
1.7105465172255, -0.711906749969871, -0.0648130690067253, -0.844643915295645, 
0.217253407310543, -0.570619818667904, -0.570619818667904, -0.990182090888924, 
0.22009369436402, 1.04308122833602, -0.046166978391628, 1.04308122833602, 
-0.677930938293665, -0.725535119180281, -0.509299178881755, -0.509299178881755, 
0.363713087547369, 0.363713087547369, 0.363713087547369, 1.94675381465822, 
1.84993175183798, 1.68856164713759, -1.226589294275, -1.25079480998006, 
-1.28790993406115, -0.892553177545187, 0.704204008465197, 0.591244935174923, 
0.962396175985825, 1.36582143773681, -1.22416874270449, -0.890939476498183, 
-1.09426580842068, 0.970464681220845, -0.691647397193198, 0.567039419469864, 
-0.885291522833669), .Dim = c(32L, 2L), .Dimnames = list(c("Mazda RX4", 
"Mazda RX4 Wag", "Datsun 710", "Hornet 4 Drive", "Hornet Sportabout", 
"Valiant", "Duster 360", "Merc 240D", "Merc 230", "Merc 280", 
"Merc 280C", "Merc 450SE", "Merc 450SL", "Merc 450SLC", "Cadillac Fleetwood", 
"Lincoln Continental", "Chrysler Imperial", "Fiat 128", "Honda Civic", 
"Toyota Corolla", "Toyota Corona", "Dodge Challenger", "AMC Javelin", 
"Camaro Z28", "Pontiac Firebird", "Fiat X1-9", "Porsche 914-2", 
"Lotus Europa", "Ford Pantera L", "Ferrari Dino", "Maserati Bora", 
"Volvo 142E"), c("mpg", "disp")), "`scaled:center`" = c(mpg = 20.090625, 
disp = 230.721875), "`scaled:scale`" = c(mpg = 6.0269480520891, 
disp = 123.938693831382))

1 个答案:

答案 0 :(得分:0)

一种简单的解决方案是使用conditionalPanel()(如果适合您)。这是一个例子

library(shiny)
library(ggplot2)
library(plotly)

ui <- fluidPage(
  checkboxInput("typeplotly", "Use interactivity", FALSE),
  conditionalPanel("input.typeplotly == true", plotlyOutput("plotly")),
  conditionalPanel("input.typeplotly == false", plotOutput("plot"))
)

server <- function(input, output, session) {
  output$plotly <- renderPlotly({
    ggplotly(ggplot(mtcars, aes(mpg, wt)) + geom_point())
  })
  output$plot <- renderPlot({
    ggplot(mtcars, aes(mpg, wt)) + geom_point()
  })
}

shinyApp(ui, server)