数据更改时,使用plotlyProxy添加多条记录道
感谢这个问题:我现在已经了解了如何去除痕迹。在本例中,我只需删除0:2,但我可以将其更改为,即数据更改时,使用plotlyProxy添加多条记录道,r,shiny,r-plotly,plotly.js,R,Shiny,R Plotly,Plotly.js,感谢这个问题:我现在已经了解了如何去除痕迹。在本例中,我只需删除0:2,但我可以将其更改为,即array(O:unique(factor(df$group))),以删除我的模型在上一次运行中创建的组数 然而,我还没有弄清楚的是,如何添加多个跟踪,目标列中的每个因素对应一个跟踪,并根据颜色中的颜色给它们上色 library("shiny") library("plotly") rock[,2] <- sample(c('A', 'B', 'C'), 48, replace = T) THE
array(O:unique(factor(df$group)))
,以删除我的模型在上一次运行中创建的组数
然而,我还没有弄清楚的是,如何添加多个跟踪,目标列中的每个因素对应一个跟踪,并根据颜色中的颜色给它们上色
library("shiny")
library("plotly")
rock[,2] <- sample(c('A', 'B', 'C'), 48, replace = T)
THECOLORS <- c('#383838', '#5b195b','#1A237E', '#000080', '#224D17', '#cccc00', '#b37400', '#990000')
ui <- fluidPage(
selectInput("dataset", "Choose a dataset:", choices = c("mtcars","rock")),
plotlyOutput("Plot1")
)
server <- function(input, output, session) {
dataSource <- reactive({switch(input$dataset,"rock" = rock,"mtcars" = mtcars)})
output$Plot1 <- renderPlotly({plot_ly(mtcars, x = ~mpg, y = ~hp, type = 'scatter', mode = 'markers', color = as.factor(mtcars$cyl), colors = THECOLORS) })
observeEvent(input$dataset, {
f <- list(
family = "Courier New, monospace",
size = 18,
color = "#7f7f7f"
)
x <- list(
title = "x Axis",
titlefont = f,
range = c(0,(max(dataSource()[,1])+ 0.1*max(dataSource()[,1])))
)
y <- list(
title = "y Axis",
titlefont = f,
range = c(0,(max(dataSource()[,4])+ 0.1*max(dataSource()[,4])))
)
plotlyProxy("Plot1", session) %>%
plotlyProxyInvoke("deleteTraces",array(0:2)) %>%
# lapply(unique(dataSource()[,2], function(x) { data <- dataSource()[which(dataSource()[,2] == x)],
# plotlyProxyInvoke("addTraces",
#
# x = data()[,1],
# y = data()[,4],
# type = 'scatter',
# mode = 'markers')}) %>%
plotlyProxyInvoke("relayout", list(xaxis = x, yaxis = y))
})
}
shinyApp(ui, server)
库(“闪亮”)
图书馆(“阴谋地”)
rock[,2]基本上是在使用plotlyProxy时,而不是使用带有“addTraces”的plotlyProxyInvoke,“addTraces”用于添加更多的跟踪。
您必须创建一个列表列表,每个内部列表将包含每个跟踪的详细信息。
在需要添加许多跟踪的情况下,purrr包中的一些函数可能有助于创建定义跟踪的列表列表
请看下面的代码。这是一个非常简化的示例,只添加了两个跟踪,但列表方法的列表就在那里。
关于你对速度的评论,也许你只能在需要时加载数据,如果你的应用程序概念允许的话,部分加载数据
守则:
library("shiny")
library("plotly")
library(purrr)
ui <- fluidPage(
selectInput("dataset", "Choose a dataset:", choices = c("rock", "mtcars")),
plotlyOutput("Plot1")
)
server <- function(input, output, session) {
output$Plot1 <- renderPlotly({plot_ly(data = rock, x = ~area,
y =~peri, mode = 'markers', type = 'scatter')})
observeEvent(input$dataset, {
if (input$dataset == "rock") {
f <- list(
family = "Courier New, monospace",
size = 18,
color = "#7f7f7f"
)
x <- list(
title = "Area",
titlefont = f,
range = c(0, max(rock$area))
)
y <- list(
title = "Peri/Perm",
titlefont = f,
range = c(0, max(rock$peri))
)
plotlyProxyInvoke(plotlyProxy("Plot1", session), "addTraces", list(list(
x = rock$area,
y = rock$peri,
type = 'scatter',
mode = 'markers',
marker = list(size = 10,
color = 'rgba(255, 182, 193, .9)',
line = list(color = 'rgba(0, 255, 0, .3)',
width = 2))
),
list(
x = rock$area,
y = rock$perm,
type = 'scatter',
mode = 'markers',
marker = list(size = 10,
color = 'rgba(255, 182, 193, .9)',
line = list(color = 'rgba(152, 0, 0, .8)',
width = 2))
))
)
plotlyProxy("Plot1", session) %>%
plotlyProxyInvoke("deleteTraces", list(as.integer(0))) %>%
plotlyProxyInvoke("relayout", list(xaxis = x, yaxis = y))
} else {
f <- list(
family = "Courier New, monospace",
size = 18,
color = "#7f7f7f"
)
x <- list(
title = "hp",
titlefont = f,
range = c(0, max(mtcars$hp))
)
y <- list(
title = "mpg/cyl",
titlefont = f,
range = c(0, max(mtcars$mpg))
)
plotlyProxyInvoke(plotlyProxy("Plot1", session), "addTraces", list(list(
x = mtcars$hp,
y = mtcars$mpg,
type = 'scatter',
mode = 'markers',
marker = list(size = 10,
color = 'rgba(255, 182, 193, .9)',
line = list(color = 'rgba(0, 255, 0, .3)',
width = 2))
),
list(
x = mtcars$hp,
y = mtcars$cyl,
type = 'scatter',
mode = 'markers',
marker = list(size = 10,
color = 'rgba(255, 182, 193, .9)',
line = list(color = 'rgba(152, 0, 0, .8)',
width = 2))
))
)
plotlyProxy("Plot1", session) %>%
plotlyProxyInvoke("deleteTraces", list(as.integer(0))) %>%
plotlyProxyInvoke("relayout", list(xaxis = x, yaxis = y))
}
})
}
shinyApp(ui, server)
库(“闪亮”)
图书馆(“阴谋地”)
图书馆(purrr)
用户界面