在 Shiny in R 中绘制散点图;情节没有更新,也不是完全互动的

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)