更新3:带有下拉按钮问题的新测试应用
library(shiny)
library(shinydashboard)
library(plotly)
library(shinyWidgets)
rm(list = ls(), envir = environment()) ## to prevent cross over from old runs
testData = data.frame(day = sample(seq(as.Date('1999/01/01'), as.Date('2000/01/01'), by="day"), 24), frequency = sample(1:5, 24, replace = T ), datecoloring = sample(1:2, 24, replace = T ))
testData$dayPOSIXct <- as.POSIXct(testData$day)
dateRangeMin <- min(testData$day)
dateRangeMax <- max(testData$day)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
menuItem("Testpage", tabName = "Testpage", icon = icon("home"))
),
dashboardBody(
tabItems(
# 1) Test Tab ---------------------------------------------------------------
tabItem(tabName = "Testpage",
actionButton(inputId = 'Load', label = 'Data'),
dropdownButton(inputId= "TestButton", label = NULL,
plotlyOutput('testplot', width = 700, height = 500),
icon = icon("list-alt"),
tooltip = tooltipOptions(title = "Click to open and render the plot"), width = "1670px")
)
)
),
title = "Dashboard example"
)
server <- function(input, output,session) {
values <- reactiveValues()
observeEvent(input$Load, {
values$testData <- testData
})
output$testplot <- renderPlotly({
req(values$testData)
p <- plot_ly(data = values$testData, source = 'testplot',
color = as.factor(values$testData$datecoloring), colors = c('#339fff', '#eaf5ff'),
opacity= 0.5, showlegend = T,
marker = list(line = list(width = 2, color = '#0000ff')),
hoverinfo = "text",
text = ~paste('Files:', values$testData$frequency, '<br>Date:', format(values$testData$day, format = '%Y-%m-%d'), sep = ' '))%>%
add_bars( x = ~dayPOSIXct, y = ~frequency, type = "bar", width = 36000000
)
p
})
relayout_data <- reactive({
req(values$testData)
event_data("plotly_relayout", source = "testplot")
})
observeEvent(relayout_data(),{
print(relayout_data())
})
}
shinyApp(ui, server)
更新2:该问题确实可以通过正确使用req()
的方法来规避,无论是否将对event_data的观察与对event_data有所作为的代码分开。例如:
relayout_data <- reactive({
req(values$testData)
event_data("plotly_relayout", source = "testplot")
})
observeEvent(relayout_data(),{
print(relayout_data())
})
但是,对于情节在内部(即下拉按钮面板或闪亮的App的其他标签/页面)的情况,这似乎无法提供解决方案。加载所需的绘图数据后,将满足req()并且将触发代码,但由于未在当前屏幕中显示该绘图,因此仍未呈现。
更新:此问题也已在github上报告,尚无实际解决方案 https://github.com/ropensci/plotly/issues/1528
原始问题/帖子:
今天,我更新了R中的所有程序包,突然我得到了很多错误,这些错误来自我的R闪亮应用程序中R中新的密谋版本4.9.0。
所有这些错误均涉及plotly_relayout
,plotly_click
等。
警告:“ plotly_relayout”事件将源ID绑定为 “ DateRangeHisto”未注册。为了获得这个事件 数据,请在图表中添加
event_register(p, 'plotly_relayout')
(p
),您希望从中获取事件数据。
我试图以各种方式添加event_register,但没有效果。我想新版本中有错误吗?
这是一个无聊的虚拟应用程序,会产生与 密谋4.9.0 并更新了所有软件包。
更新:当该图没有可用数据时,似乎会发生错误
尽管req()
块内有plot_ly
,event_data
仍会导致错误。以前版本的plotly并没有发生这种情况。....因此,现在如何解决这个问题
library(shiny)
library(plotly)
rm(list = ls(), envir = environment()) ## to prevent cross over from old runs
testData = data.frame(day = sample(seq(as.Date('1999/01/01'), as.Date('2000/01/01'), by="day"), 24), frequency = sample(1:5, 24, replace = T ), datecoloring = sample(1:2, 24, replace = T ))
testData$dayPOSIXct <- as.POSIXct(testData$day)
dateRangeMin <- min(testData$day)
dateRangeMax <- max(testData$day)
if(!require('shiny')){ install.packages('shiny', dependencies=TRUE)}
if(!require('shinyWidgets')){ install.packages('shinyWidgets', dependencies=TRUE)}
if(!require('plotly')){ install.packages('plotly', dependencies=TRUE)}
if(!require('htmlwidgets')){ install.packages('htmlwidgets')}
ui <- fluidPage(
actionButton(inputId = 'Load', label = 'Data'),
plotlyOutput('testplot', width = 700, height = 500)
)
server <- function(input, output,session) {
values <- reactiveValues()
observeEvent(input$Load, {
values$testData <- testData
})
output$testplot <- renderPlotly({
req(values$testData)
p <- plot_ly(data = values$testData, source = 'testplot',
color = as.factor(values$testData$datecoloring), colors = c('#339fff', '#eaf5ff'),
opacity= 0.5, showlegend = T,
marker = list(line = list(width = 2, color = '#0000ff')),
hoverinfo = "text",
text = ~paste('Files:', values$testData$frequency, '<br>Date:', format(values$testData$day, format = '%Y-%m-%d'), sep = ' '))%>%
add_bars( x = ~dayPOSIXct, y = ~frequency, type = "bar", width = 36000000
)
p
})
observeEvent(event_data("plotly_relayout", source = "testplot"),{
#any code here, doesn't matter, bug happens already
})
}
shinyApp(ui, server)
会话信息
sessionInfo()
R version 3.5.3 (2019-03-11)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
Matrix products: default
locale:
[1] LC_COLLATE=Dutch_Netherlands.1252 LC_CTYPE=Dutch_Netherlands.1252 LC_MONETARY=Dutch_Netherlands.1252 LC_NUMERIC=C LC_TIME=Dutch_Netherlands.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] plotly_4.9.0 ggplot2_3.1.1 shiny_1.3.2
loaded via a namespace (and not attached):
[1] Rcpp_1.0.1 pillar_1.4.0 compiler_3.5.3 later_0.8.0 colourpicker_1.0.3 plyr_1.8.4 shinyjs_1.0 tools_3.5.3 digest_0.6.19 viridisLite_0.3.0
[11] jsonlite_1.6 tibble_2.1.1 gtable_0.3.0 pkgconfig_2.0.2 rlang_0.3.4 rstudioapi_0.10 crosstalk_1.0.0 yaml_2.2.0 httr_1.4.0 withr_2.1.2
[21] dplyr_0.8.1 htmlwidgets_1.3 grid_3.5.3 DT_0.6 tidyselect_0.2.5 glue_1.3.1 data.table_1.12.2 R6_2.4.0 tidyr_0.8.3 purrr_0.3.2
[31] magrittr_1.5 scales_1.0.0 promises_1.0.1 htmltools_0.3.6 assertthat_0.2.1 mime_0.6 xtable_1.8-4 colorspace_1.4-1 httpuv_1.5.1 miniUI_0.1.1.1
[41] lazyeval_0.2.2 munsell_0.5.0 crayon_1.3.4
答案 0 :(得分:1)
问题在于observeEvent
试图在绘制图形之前访问event_data
。您也可以为req()
使用event_data()
来解决此问题。确实,在4.9.0上确实对此要求更为严格。
library(shiny)
library(shinydashboard)
library(plotly)
library(shinyWidgets)
rm(list = ls(), envir = environment()) ## to prevent cross over from old runs
testData = data.frame(day = sample(seq(as.Date('1999/01/01'), as.Date('2000/01/01'), by="day"), 24), frequency = sample(1:5, 24, replace = T ), datecoloring = sample(1:2, 24, replace = T ))
testData$dayPOSIXct <- as.POSIXct(testData$day)
dateRangeMin <- min(testData$day)
dateRangeMax <- max(testData$day)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
sidebarMenu(
menuItem("Testpage", tabName = "Testpage", icon = icon("home"))
)
),
dashboardBody(
tabItems(
# 1) Test Tab ---------------------------------------------------------------
tabItem(tabName = "Testpage",
actionButton(inputId = 'Load', label = 'Data'),
dropdownButton(inputId = "TestButton", label = NULL,
plotlyOutput('testplot', width = 700, height = 500),
icon = icon("list-alt"),
tooltip = tooltipOptions(title = "Click to open and render the plot"), width = "1670px")
)
)
),
title = "Dashboard example"
)
server <- function(input, output, session) {
# output$testplot <- renderPlotly({plot_ly(data.frame(NULL), source = 'testplot')})
values <- reactiveValues()
observeEvent(input$Load, {
values$testData <- testData
})
output$testplot <- renderPlotly({
req(values$testData)
p <- plot_ly(data = values$testData, source = 'testplot',
color = as.factor(values$testData$datecoloring), colors = c('#339fff', '#eaf5ff'),
opacity= 0.5, showlegend = T,
marker = list(line = list(width = 2, color = '#0000ff')),
hoverinfo = "text",
text = ~paste('Files:', values$testData$frequency, '<br>Date:', format(values$testData$day, format = '%Y-%m-%d'), sep = ' '))%>%
add_bars( x = ~dayPOSIXct, y = ~frequency, type = "bar", width = 36000000)
p
})
relayout_data <- reactive({
req(values$testData)
req(input$TestButton_state)
event_data("plotly_relayout", source = "testplot")
})
observeEvent(relayout_data(),{
print(relayout_data())
})
}
shinyApp(ui, server)
答案 1 :(得分:0)
我遇到了同样的问题,一种避免警告的可能方法是启动observeEvent
,该event_data
处于suspended = TRUE
的暂停状态,例如
observer <- observeEvent(event_data("plotly_relayout", source = "testplot"), suspended = TRUE, {
print(event_data("plotly_relayout", source = "testplot"))
})
然后仅在定义图对象observer$resume()
之后,通过在renderPlotly
内添加p
创建图后开始观察。
以下最小的闪亮应用程序通过观察event_data("plotly_click")
中uiOutput
内的绘图来说明这一点,该绘图仅在选择了actionButton
和checkbox
之后才可用:< / p>
library(shiny)
library(plotly)
ui <- fluidPage(
## button
actionButton("load", label = "New data"),
## checkbox
checkboxInput("show", label = "Show plot"),
## plot
uiOutput("plot_ui"),
## click events
tableOutput("clicked")
)
server <- function(input, output, session) {
## reactive values
r <- reactiveValues(
data = NULL,
suspended = TRUE,
clicked = NULL
)
## load data
observeEvent(input$load, {
r$data <- data.frame(x= 1:100, y = rnorm(100))
})
## show plot only if checkbox is selected
output$plot_ui <- renderUI({
req(input$show)
plotlyOutput("plot")
})
## plotly plot
output$plot <- renderPlotly({
req(r$data)
p <- plot_ly(data = r$data, x = ~x, y = ~y, mode = "markers", type = "scatter", source = "plot")
## resume observer only if suspended
if(r$suspended) {
observer$resume()
r$suspended <- FALSE
}
return(p)
})
## observe click events
observer <- observeEvent(event_data("plotly_click", source = "plot"), suspended = TRUE, {
r$clicked <- rbind(r$clicked, event_data("plotly_click", source = "plot"))
})
## display clicked points
output$clicked <- renderTable(
r$clicked
)
}
shinyApp(ui, server)
NB:反应值r$suspended
确保仅在观察者的状态实际被挂起的情况下调用observer$resume()
。