首页 > 解决方案 > 使用 Plotly 绘制反应图

问题描述

我想通过使用 Plotly 制作一个反应图。当我单击表格行时,我希望出现一个模态并将该特定元素绘制在模态中。该图显示了数据表中每个元素每天的出现次数。

原始数据框是:

> dput(head(closed_srs3))
structure(list(Name = c("DASOVOUNIX", "MYSTEGNA_X", "PETROCHORI_SAR", 
"AGNIK_SAR", "GARDIKI-X", "AMOELATOPOS"), Id = c(3311, 1632, 
4779, 4796, 4291, 1449), Date = structure(c(1548892800, 1548892800, 
1548892800, 1548892800, 1548892800, 1548460800), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), Created = structure(c(1548892800, 
1548892800, 1548892800, 1548892800, 1548892800, 1548460800), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), Closed = structure(c(1548979200, 1548979200, 
1548979200, 1548979200, 1548979200, 1548979200), class = c("POSIXct", 
"POSIXt"), tzone = "UTC")), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

当手动放置元素的名称时,以下方法有效。

library(dplyr)
library(plotly)

myFrame <- data.frame(closed_srs3)

myFrame <- subset(myFrame, myFrame$Name == 'AGNIK_SAR')

newFrame <- myFrame %>%
  group_by(Name, Closed) %>%
  summarize(count = n())

p <- plot_ly(newFrame, x = newFrame$Closed, y = newFrame$count, type = 'bar')

问题是当我尝试使用以下代码使其反应时,绘制的结果是错误的。任何人都可以帮忙吗?

 # SUBITEM1 -> SRs
           tabItem(tabName = "SRs", 

            fluidRow(infoBoxOutput("progressBox")),       


        tabBox( width = 12,           
          tabPanel("All Closed SRs",
            column(3,
                selectInput(inputId="1", label= "Name", c("All", unique(as.character(closed_srs3$Name))))
                   ),
          column(3,
                selectInput(inputId="2", label= "Id", c('All', unique(as.character(closed_srs3$Id))))
                   ),
          column(3,
                selectInput(inputId="3", label= "Created", c('All', unique(as.character(closed_srs3$Created))))
                   ),
          column(3,
                selectInput(inputId="4", label= "Closed", c("All", unique(as.character(closed_srs3$Closed))))
                   ),


                          DT::dataTableOutput("mytable")),


          tabPanel("Wosrt Items by No of Appearences",    
            column(3,
                selectInput(inputId="5", label= "Name", c("All", unique(as.character(closed_srs3$Name))))
                   ),
            column(3,
                selectInput(inputId="6", label= "Id", c('All', unique(as.character(closed_srs3$Id))))
                   ),

                         DT::dataTableOutput(outputId="table2", width="100%")))


                               )# fluidrow
                      ) #tabItems
              ) #dashboardBody
) #dashboardPage


output$table2 <- DT::renderDataTable(options = list(pageLength = 10),
                                       rownames= FALSE, server = FALSE, selection='single',

    {
      data<-closed_srs3 %>% count(Name, Id) %>%arrange(desc(n)

  if (input$`5` != "All") {
    data <- data[data$Name == input$`5`,]
  }

  if (input$`6` != "All") {
    data <- data[data$Id == input$`6`,]
  }

  data})
plotData<-reactive ({       
  s <- input$table2_rows_selected
  selectedName <- as.character(closed_srs3$Name[s])
  myFrame <- data.frame(closed_srs3)
  myFrame <- subset(myFrame, myFrame$Name == selectedName)

  newFrame <- myFrame %>%
    group_by(Name, Closed) %>%
    summarize(count = n())

  return(newFrame)  
  })

  observeEvent(input$table2_rows_selected,

    {showModal(modalDialog(
     title = "You have selected a row!", size = "l",
     output$plot1 <- renderPlotly(
     plot_ly( x = plotData()$Closed, y = plotData()$count, type = 'bar')

              ))) #showModal
              }) # observeEvent

标签: rshiny

解决方案


问题是绘图表的顺序与您的反应数据不同。所以你会得到不同的结果,或者可能总是一样的。

如果您在反应式和表格中按名称订购 data.frame,它应该是正确的。

library(dplyr)
library(plotly)
library(shiny)

ui <- fluidPage(
  tabPanel("Wosrt Items by No of Appearences", 
           column(3,
                  selectInput(inputId="5", label= "Name", c("All", 
                                                            unique(as.character(closed_srs3$Name)))) ), 
           column(3, selectInput(inputId="6", label= "Id", c('All', 
                                                             unique(as.character(closed_srs3$Id)))) ), 
           DT::dataTableOutput(outputId="table2", width="100%"))
)

server <- function(input, output){
  output$table2 <- DT::renderDataTable({
    data <- closed_srs3 %>% count(Name, Id)  %>% arrange(desc(n))

    if (input$`5` != "All") {
      data <- data[data$Name == input$`5`,]
    }

    if (input$`6` != "All") {
      data <- data[data$Id == input$`6`,]
    }

    data
  }, options = list(pageLength = 10),
  rownames= FALSE, server = FALSE, selection='single')

  plotData<-reactive({
    s <- input$table2_rows_selected

    ## Order the data the same way as the table !!!
    ordereddf <- closed_srs3 %>%  arrange(Name)
    selectedName <- as.character(ordereddf$Name[s])

    myFrame <- data.frame(closed_srs3)
    myFrame <- subset(myFrame, myFrame$Name == selectedName)

    newFrame <- myFrame %>%
      group_by(Name, Closed) %>%
      summarize(count = n())

    return(newFrame)  
  })

  observeEvent(input$table2_rows_selected, {
    req(plotData())
    showModal(modalDialog(
      title = "You have selected a row!", size = "l",
      output$plot1 <- renderPlotly(
        plot_ly( x = plotData()$Closed, y = plotData()$count, type = 'bar')

      ))) #showModal
  }) # observeEvent
}


shinyApp(ui, server)

推荐阅读