r - 使用 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
解决方案
问题是绘图表的顺序与您的反应数据不同。所以你会得到不同的结果,或者可能总是一样的。
如果您在反应式和表格中按名称订购 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)
推荐阅读
- asp.net - 如何将 GridView 中一行的 Background.Color 更改为 Gray
- jquery - 触摸缩放不适用于 jQuery Panzoom
- haskell - 如何从元组列表中只获取我想要的元组?[哈斯克尔]
- macos - “裸域”意外关闭了我电脑上的连接
- angular - Angular 最佳实践:将数据作为输入传递或使用查询参数
- c - 在尝试计算 C 中的大写字母时获得随机增量
- gradle - 在 IDEA 中打开 gradle 项目时如何修复 `#USE_PROJECT_JDK`?
- php - 当您在另一个字段中输入值时如何更新表单字段
- ruby-on-rails - 我的控制器未定义方法“user_id”中的 Ruby-on-Rails NoMethodError 用于 nil:NilClass
- ios - 从我的应用发布时如何确保 Facebook 将 sdk 帖子共享到 Facebook 时间线