如何在geomtext()上应用shinyjs来隐藏和显示数据标签

时间:2018-04-02 22:40:20

标签: r ggplot2 shiny shinyjs

您好我已经创建了一个闪亮的应用程序,它可以在所选变量之间创建散点图。然后,当我单击数据点时,点的名称将打印在图中。问题是,当我用其他变量更新绘图时,打印不会被删除。我的想法是使用这种逻辑:R shiny, shinyjs, remove plot and draw it again if button is clicked但我不确定我是否正确,因为我只想让数据标签消失而不是整个情节

 #ui.r
    library(shiny)
    library(ggplot2)
    library(plotly)
    library(dplyr)

    fluidPage(
    useShinyjs(),
      # App title ----
      titlePanel(div("CROSS CORRELATION",style = "color:blue")),

      # Sidebar layout with input and output definitions ----
      sidebarLayout(

        # Sidebar panel for inputs ----
        sidebarPanel(


        ),
        # Main panel for displaying outputs ----
        mainPanel(

          tabsetPanel(type = "tabs",

                      tabPanel("Correlation Plot",

                               fluidRow(
                                 column(3, uiOutput("lx1")),
                               column(3,uiOutput("lx2")),
 actionButton("hideshow", "Hide/show plot")),
                               hr(),
                               fluidRow(
                                 tags$style(type="text/css",
                                            ".shiny-output-error { visibility: hidden; }",
                                            ".shiny-output-error:before { visibility: hidden; }"
                                 )
                               ),
                               fluidRow(
                               plotlyOutput("sc"))
          )
          )
      )))
    #server.r
    function(input, output,session) {


      output$lx1<-renderUI({
        selectInput("lx1", label = h4("Select 1st Expression Profile"), 
                    choices = colnames(iris[,1:4]), 
                    selected = "Lex1")
      })
      output$lx2<-renderUI({
        selectInput("lx2", label = h4("Select 2nd Expression Profile"), 
                    choices = colnames(iris[,1:4]), 
                    selected = "Lex2")
      })





      # 1. create reactive values
      vals <- reactiveValues()
      # 2. create df to store clicks
      vals$click_all <- data.frame(x = numeric(),
                                   y = numeric(),
                                   label = character())
      # 3. add points upon plot click
      observe({
        # get clicked point
        click_data <- event_data("plotly_click", source = "select")
        # get data for current point
        label_data <- data.frame(x = click_data[["x"]],
                                 y = click_data[["y"]],
                                 label = click_data[["key"]],
                                 stringsAsFactors = FALSE)
        # add current point to df of all clicks
        vals$click_all <- merge(vals$click_all,
                                label_data, 
                                all = TRUE)
      }) 

     output$sc<-renderPlotly({


           p1 <- ggplot(iris, aes_string(x = input$lx1, y = input$lx2,key = "Species"))+
             # Change the point options in geom_point
             geom_point(color = "darkblue") +

             # Change the title of the plot (can change axis titles
             # in this option as well and add subtitle)
             labs(title = "Cross Correlation") +
             # Change where the tick marks are
             # Change how the text looks for each element

             theme_bw()+

             # 4. add labels for clicked points
             geom_text(data = vals$click_all,
                       aes(x = x, y = y, label = label),
                       inherit.aes = FALSE, nudge_x = 0.25)


       ggplotly(p1,source = "select", tooltip = c("key")) %>%
         layout(hoverlabel = list(bgcolor = "white", 
                                  font = list(family = "Calibri", 
                                              size = 9, 
                                              color = "black")))

     }) 



     observeEvent(input$hideshow, {
       # every time the button is pressed, alternate between hiding and showing the plot
       toggle("sc")
     })



        }

0 个答案:

没有答案