- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个客户希望能够在 Rshiny 中“徒手”绘制绘图(ggplot)图。我说过要在 plotly 图上使用套索选择按钮,但他们并不高兴,如果您单击图表上的其他地方,它会删除第一个套索。
使用此 post ,我能够处理一个我可以借鉴的ggplot。但是,我无法让它与 plotly 一起使用,因为我不知道下面 ui 中悬停选项的等效项。我想了解如何使用 plotly 执行此操作,如何改进代码以使其更快,和/或如何不让它从 (1,1) 的任意值开始。
可以理解的是,这只适用于数据完全是数字的情况。如果说数据中的第一列是 c("a","b","c") 而不是像我下面那样的 c(1,2,3) ,有没有办法做到这一点。
注意:第一次点击时,这条线从 (1,1) 开始,因为 ggplot 需要一个值来绘制图表,但 react 输入需要一个图表。为了绕过这个循环,我只是将列放在 c(1,vals$x) ......希望这是有道理的。
library(shiny)
library(tidyverse)
ui <- fluidPage(
actionButton("reset", "reset"),
plotOutput("plot",
hover=hoverOpts(id = "hover", delay = 300, delayType = "throttle", clip = TRUE, nullOutside = TRUE),
click="click"))
server <- function(input, output, session) {
vals = reactiveValues(x=NULL, y=NULL)
draw = reactiveVal(FALSE)
observeEvent(input$click, handlerExpr = {
temp <- draw(); draw(!temp)
if(!draw()) {
vals$x <- c(vals$x, NA)
vals$y <- c(vals$y, NA)
}})
observeEvent(input$reset, handlerExpr = {
vals$x <- NULL; vals$y <- NULL
})
observeEvent(input$hover, {
if (draw()) {
vals$x <- c(vals$x, input$hover$x)
vals$y <- c(vals$y, input$hover$y)
}})
output$plot= renderPlot({
Data<-cbind(c(1,2,3),c(2,3,4))%>%as.data.frame()
d<-cbind(c(1,vals$x),c(1,vals$y))%>%as.data.frame()
ggplot(data=Data)+geom_point(data=Data,aes(x=V1,y=V2))+
geom_path(data=d,aes(x=V1,y=V2))+xlim(c(0,15))+ylim(c(0,15))
})
}
shinyApp(ui, server)
最佳答案
首先,您可以通过按 shift
在 plotly 中选择多个套索。 .
以下是对我的回答here的修改- 所以是 plot_ly
/plotlyProxy
基于,修改现有的绘图对象(不重新渲染)不使用 ggplotly
.由于 GitHub issue 中正在进行一些相关工作和 PR下面的答案可能不是 100% 可靠(例如,缩放似乎把事情搞砸了 - 您可能想要停用它)并且可能会过时。
不过,请检查以下内容:
library(plotly)
library(shiny)
library(htmlwidgets)
ui <- fluidPage(
plotlyOutput("myPlot"),
verbatimTextOutput("click")
)
server <- function(input, output, session) {
js <- "
function(el, x){
var id = el.getAttribute('id');
var gd = document.getElementById(id);
Plotly.plot(id).then(attach);
function attach() {
var xaxis = gd._fullLayout.xaxis;
var yaxis = gd._fullLayout.yaxis;
var coordinates = [null, null]
gd.addEventListener('click', function(evt) {
var bb = evt.target.getBoundingClientRect();
var x = xaxis.p2d(evt.clientX - bb.left);
var y = yaxis.p2d(evt.clientY - bb.top);
var coordinates = [x, y];
Shiny.setInputValue('clickposition', coordinates);
});
gd.addEventListener('mousemove', function(evt) {
var bb = evt.target.getBoundingClientRect();
var x = xaxis.p2d(evt.clientX - bb.left);
var y = yaxis.p2d(evt.clientY - bb.top);
var coordinates = [x, y];
Shiny.setInputValue('mouseposition', coordinates);
});
};
}
"
output$myPlot <- renderPlotly({
plot_ly(type = "scatter", mode = "markers") %>% layout(
xaxis = list(range = c(0, 100)),
yaxis = list(range = c(0, 100))) %>%
onRender(js)
})
myPlotProxy <- plotlyProxy("myPlot", session)
followMouse <- reactiveVal(FALSE)
traceCount <- reactiveVal(0L)
observeEvent(input$clickposition, {
followMouse(!followMouse())
if(followMouse()){
plotlyProxyInvoke(myPlotProxy, "addTraces", list(x = list(input$clickposition[1]), y = list(input$clickposition[2])))
traceCount(traceCount()+1)
}
})
observe({
if(followMouse()){
plotlyProxyInvoke(myPlotProxy, "extendTraces", list(x = list(list(input$mouseposition[1])), y = list(list(input$mouseposition[2]))), list(traceCount()))
}
})
}
shinyApp(ui, server)
library(plotly)
library(shiny)
library(htmlwidgets)
ui <- fluidPage(
plotlyOutput("myPlot"),
verbatimTextOutput("click")
)
server <- function(input, output, session) {
js <- "
function(el, x){
var id = el.getAttribute('id');
var gd = document.getElementById(id);
Plotly.plot(id).then(attach);
function attach() {
var xaxis = gd._fullLayout.xaxis;
var yaxis = gd._fullLayout.yaxis;
var coordinates = [null, null]
gd.addEventListener('click', function(evt) {
var bb = evt.target.getBoundingClientRect();
var x = xaxis.p2d(evt.clientX - bb.left);
var y = yaxis.p2d(evt.clientY - bb.top);
var coordinates = [x, y];
Shiny.setInputValue('clickposition', coordinates);
});
gd.addEventListener('mousemove', function(evt) {
var bb = evt.target.getBoundingClientRect();
var x = xaxis.p2d(evt.clientX - bb.left);
var y = yaxis.p2d(evt.clientY - bb.top);
var coordinates = [x, y];
Shiny.setInputValue('mouseposition', coordinates);
});
};
}
"
output$myPlot <- renderPlotly({
plot_ly(type = "scatter", mode = "markers") %>% layout(
xaxis = list(range = c(0, 100)),
yaxis = list(range = c(0, 100))) %>%
onRender(js)
})
myPlotProxy <- plotlyProxy("myPlot", session)
followMouse <- reactiveVal(FALSE)
clickCount <- reactiveVal(0L)
observeEvent(input$clickposition, {
followMouse(!followMouse())
clickCount(clickCount()+1)
if(clickCount() == 1){
plotlyProxyInvoke(myPlotProxy, "addTraces", list(x = list(input$clickposition[1]), y = list(input$clickposition[2])))
}
})
observe({
if(followMouse()){
plotlyProxyInvoke(myPlotProxy, "extendTraces", list(x = list(list(input$mouseposition[1])), y = list(list(input$mouseposition[2]))), list(1))
} else {
plotlyProxyInvoke(myPlotProxy, "extendTraces", list(x = list(list(NA)), y = list(list(NA))), list(1))
}
})
}
shinyApp(ui, server)
关于r - 徒手画ggplot。如何改进和/或格式化 plotly?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64309573/
是否有功能或其他方法可以在Shiny中使用鼠标启用徒手绘制(即绘制随机形状/尺寸)? 具体来说,我希望能够通过以各种(但非均匀)方式进行标记来与renderPlot中的图进行“交互”。 -换句话说,我
我是一名优秀的程序员,十分优秀!