[英]Shiny R: How to change plotly opacity basing on an input?
In my project, a slider controls the size of a simulated dataset, and the range of size is [5,10000].在我的项目中,一个 slider 控制模拟数据集的大小,大小范围为 [5,10000]。
As the data points may overlap, I hope to have a higher opacity when the size is small and a lower opacity when the size is large.由于数据点可能会重叠,我希望在尺寸较小时具有较高的不透明度,在尺寸较大时具有较低的不透明度。
I tried to use reactiveValue and observeEvent but the opacity is unchangeable at 0.2:我尝试使用 reactiveValue 和 observeEvent 但不透明度在 0.2 时是不可改变的:
o<- reactiveValues(data = NULL)
observeEvent(input$size<100,{
o$data<-1
})
observeEvent(input$size>=100,{
o$data<-0.2
})
p<-plot_ly()%>%
add_trace(x=x,y=y1,type = 'scatter',mode = 'lines', name = "y1=a1+b1*x",line = list(color = 'rgb(255, 129, 10)'))%>%
add_trace(x=round(random_x,4),y= round(y1_data,4), opacity=o$data,name = 'y1_data',type = 'scatter',mode = 'markers',marker = list(size = 5, color ='rgb(255, 129, 10)')
Here is the relavent code:这是相关的代码:
sliderInput("size", "Select the sample size",min = 5,max = 10000,value = NULL,step = 1)
random_x<- rnorm(input$size,mean = 0, 3)
random_num1<- rnorm(input$size, mean=0, sd=input$sd1)
random_num2<- rnorm(input$size, mean=0, sd=input$sd2)
y1_data<- random_num1 + random_x*input$b1+input$a1
y2_data<- 1/(1+exp(random_num2-input$a2-input$b2 * random_x))
x<- seq(-10,10,0.1)
y1<- x*input$b1+input$a1
y2<- 1/(1+exp(-input$a2-input$b2*x))
#plot
p<-plot_ly()%>%
layout(xaxis = list(range=c(-10,10)), yaxis = list(range=c(-32,32)))
if (input$simple){
p<-p%>%
add_trace(x=x,y=y1,type = 'scatter', mode = 'lines', name = "y1=a1+b1*x",line = list(color = 'rgb(255, 129, 10)'))%>%
add_trace(x=round(random_x,4),y= round(y1_data,4), name = 'y1_data',
type = 'scatter',mode = 'markers',marker = list(size = 5, color ='rgb(255, 129, 10)'))
}
if (input$logistic) {
p<-p%>%
add_trace(x=x,y=y2,type = 'scatter', mode = 'lines', name = "y2=1/(exp(-a2-b2*x))", line = list(color = 'rgb(22, 96, 167)'))%>%
add_trace(x=round(random_x,4),y= round(y2_data,4), name = 'y2_data',
type = 'scatter',mode = 'markers',marker = list(size = 5, color ='rgb(22, 96, 167)'))
}
My suspicion is that both of your observeEvent
methods are called when the slider changes value.我的怀疑是,当 slider 更改值时,您的两个observeEvent
方法都会被调用。 Then, even if your reactiveValues
o$data
changes to 1 by one observeEvent
, it will be immediately overwritten with.2 by the other observeEvent
.然后,即使您的reactiveValues
o$data
被一个observeEvent
更改为 1 ,它也会立即被另一个observeEvent
覆盖为 .2 。 Instead, could you simplify to a single observeEvent
statement (or just a single observe
)?相反,您能否简化为单个observeEvent
语句(或仅单个observe
)? Try something like this below and see if behaves the way you want it to.在下面尝试类似的操作,看看是否按照您想要的方式运行。
library(shiny)
ui <- fluidPage(
sliderInput("size", "Select the sample size", min = 5, max = 1000, value = NULL, step = 1)
)
server <- function(input, output, session) {
o <- reactiveValues(data = NULL)
observe({
if (input$size < 100) {
o$data <- 1
} else {
o$data <- .2
}
print(o$data)
})
}
shinyApp(ui, server)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.