如何使用R Shiny从可绘制的图形中显示表中的多行并删除表中的最后一行?

时间:2019-01-29 23:14:21

标签: r shiny reactive r-plotly

我正在尝试使用plotly在用户单击geom_points的位置上绘制图形,并且应该将geom_point行填充到下面的呈现表中。

我已经成功做到了。因此,Person单击图形上的geom_point,将显示geom_point数据(行)。现在,我尝试将多个行添加到同一表,而不是覆盖所选人员的上一行。基本上,我希望该人员单击多个geom_points,并且该表应显示所有geom_points数据,而不是覆盖前一个数据。

    library(shiny)
    library(plotly)
    library(DT)

    d1=structure(list(Topic = c("compensation", "manager", "benefits",
                                "family", "communication", "worklifebalance", "perks", "compensation",
                                "benefits", "manager", "communication", "worklifebalance", "family",
                                "perks", "benefits", "compensation", "manager", "communication",
                                "family", "worklifebalance", "perks"),
                      variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L),
                                           .Label = c("Prct", "Count"), class = "factor"),
                      value = c(2.23121245555964, 0.723305136692411, 0.576192227534633,
                                0.202280250091946, 0.190020840995464, 0.153242613706019,
                                0.0122594090964816, 0.913705583756345, 0.609137055837563,
                                0.50761421319797, 0.50761421319797, 0.304568527918782, 0.203045685279188,
                                0, 1.49977276170277, 1.21193758521436, 0.893803969095592,
                                0.439327374640206, 0.348432055749129, 0.242387517042872,
                                0.0757460990758976),
                      group = c("APAC", "APAC", "APAC", "APAC",  "APAC", "APAC", "APAC",
                                "EMEA", "EMEA", "EMEA", "EMEA", "EMEA", "EMEA", "EMEA",
                                "AMERICAS", "AMERICAS", "AMERICAS", "AMERICAS", "AMERICAS",
                                "AMERICAS", "AMERICAS")),
                 .Names = c("Topic", "variable", "value", "group"), class = c("data.table", "data.frame"),
                 row.names = c(NA, -21L))


    ui <- fluidPage(
      fluidRow(plotlyOutput('keywords')),
      fluidRow(verbatimTextOutput("selection")),
      fluidRow(DT::dataTableOutput("table1"))
    )   

    d0 = d1
    key <- row.names(d0)

    server = function(input,output){
      output$keywords = renderPlotly({


        d0 <- data.frame(d0, key)  

        p = ggplot(d0, aes(reorder(Topic,-value), value, key = key)) +
          geom_point(aes(colour = value),
                     shape = 16,
                     size = 3,
                     show.legend = F) +
          facet_wrap(~ group)+
          theme_minimal()
        ggplotly(p)


      })
      output$selection <- renderPrint({
        s <- event_data("plotly_click")
        cat("You selected: \n\n")
        data.frame(s)
      })

      selection2 <- reactive({
        s <- event_data("plotly_click")
        cat("You selected: \n\n")
        df <- data.frame(s)
      })

      output$table1 = renderDT({
        d2 <- d1 %>% filter(key == selection2()$key)
        d2 
      }) 
    }

    shinyApp(ui, server)

如果您将运行此代码并单击点。您会注意到它覆盖了表中的行。我希望当您继续单击点时,它应该继续追加行。我是使用Shiny的新手,但是是否可以使用reactValues或observeEvent或其他方法呢?

1 个答案:

答案 0 :(得分:0)

您必须更新d2的值,以便可以使用reactiveVal()进行更新。这是我在您的server函数中所做的更改:

  d2 <- reactiveVal(data.frame())

  observeEvent(event_data("plotly_click"), {
    d2Temp <- rbind(
      d2(),
      d1 %>% filter(key == selection2()$key)
    )
    d2(d2Temp)
  })

  output$table1 = renderDT({
    d2()
  }) 

首先,您必须使用空的d2初始化data.frame反应值。然后,观察"plotly_click"并将新行绑定到旧data.frame。最后,用reactiveVal更新您的d2(d2Temp)的价值。

相关问题