更新3:带有dropdownbutton问题的新测试应用
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())
})
但是,对于情节在内部的情况,即下拉按钮面板或发光应用程序的其他选项卡/页面,这似乎无法提供解决方案。加载绘图所需的数据后,将满足req()并将激发代码,但由于当前屏幕中未显示该绘图,因此仍未呈现该绘图。
更新:这个问题也报告在github上,目前还没有真正的解决方案 https://github.com/ropensci/plotly/issues/1528
原始问题/帖子:
今天,我更新了R中的所有程序包,突然我得到了很多错误,这些错误来自于R闪亮的App中R中的新版4.9.0版本。
所有这些错误均参考plotly_relayout
,plotly_click
等等。
警告:与源ID“ DateRangeHisto”绑定的“ plotly_relayout”事件未注册。为了获取此事件数据,请添加
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
问题在于,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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句