我想通过使用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
答案 0 :(得分:0)
问题在于,打印表的顺序与反应式数据的顺序不同。这样您会得到不同的结果,或者可能总是相同。
如果您在反应式和表格中按名称对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)