[英]Plotly scatter plot in Shiny in R; plot is not updating nor is it fully interactive
我正在尝试使用plotly
库在 Shiny 中创建一个交互式散点图,我可以在其中绘制分组数据并将光标移动到每个数据点上。 我是plotly
但广泛使用了 Shiny。
这是我想出的一个模拟示例:-
library(shiny)
library(shinyWidgets)
library(plotly)
library(tidyverse)
ui<-fluidPage(
titlePanel("Iris"),
tabsetPanel(
tabPanel("Iris scatter",
sidebarPanel(width=5,
h5("Select the axis variables"),
selectInput("eda_scatter_x", "Select x-axis",
c("Sepal Length"="Sepal.Length",
"Sepal Width" ="Sepal.Width",
"Petal Length"= "Petal.Length",
"Petal Width"= "Petal.Width")),
selectInput("eda_scatter_y", "Select y-axis",
c("Sepal Length"="Sepal.Length",
"Sepal Width" ="Sepal.Width",
"Petal Length"= "Petal.Length",
"Petal Width"= "Petal.Width")),
actionButton(inputId = "eda_run", "Run analysis")),
mainPanel(
plotlyOutput("eda_scatter_graph")
)))
)
server <- function(input,output,session){
observeEvent(input$eda_run,{
output$eda_scatter_graph<- renderPlotly({
iris%>%
group_by(Species)%>%
summarise_at(vars(Sepal.Length:Petal.Width),mean)%>%
plot_ly(x=input$eda_scatter_x, y=input$eda_scatter_y, size=I(c(100,50)))%>%
add_markers()
})
})
}
shinyApp(ui,server)
这给出了这个输出:-
期望输出
我想要做的是绘制分组的数据点,所以当我的光标移动到数据点上时,标记能够告诉我 x 轴和 y 轴坐标加上分组变量名称(在这种情况下, Species
)。 最后,我希望能够使用selectInput
框更新 x 轴和 y 轴,但是当您运行应用程序时,它不会更新。
任何人都可以告诉我我做错了什么并提出修改建议吗?
谢谢 :)
不要使用观察事件,而是使用反应值,让内部处理其余部分
library(shiny)
library(plotly)
ui<-fluidPage(
titlePanel("Iris"),
tabsetPanel(
tabPanel("Iris scatter",
sidebarPanel(width=10,
h5("Select the axis variables"),
selectInput('xcol','X Variable', names(iris)),
selectInput('ycol','Y Variable', names(iris)),
selectInput('color','Group Color', names(iris)),
mainPanel(
plotlyOutput("eda_scatter_graph")
)
)
)
)
)
server <- function(input,output,session){
x <- reactive({
iris[,input$xcol]
})
y <- reactive({
iris[,input$ycol]
})
color <- reactive({
iris[,input$color]
})
output$eda_scatter_graph<- renderPlotly(
plot1<- plot_ly( x=x(), y=y(), type = 'scatter',
mode = 'markers', color = color())
)
}
shinyApp(ui,server)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.