简体   繁体   English

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

[英]Plotly scatter plot in Shiny in R; plot is not updating nor is it fully interactive

I am trying to create an interactive scatter plot within Shiny using the plotly library, where I can plot grouped data and move my cursor over each data point.我正在尝试使用plotly库在 Shiny 中创建一个交互式散点图,我可以在其中绘制分组数据并将光标移动到每个数据点上。 I am new to plotly but have used Shiny extensively.我是plotly但广泛使用了 Shiny。

Here is a mock example that I have came up with:-这是我想出的一个模拟示例:-


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)


Which gives this output:-这给出了这个输出:-

在此处输入图片说明

Desired output期望输出

What I want to be able to do is plot the grouped data points, so when my cursor moves over the data points, the markers are able to tell me the x-axis and y-axis coordinates plus the grouped variable name (in this case, the Species ).我想要做的是绘制分组的数据点,所以当我的光标移动到数据点上时,标记能够告诉我 x 轴和 y 轴坐标加上分组变量名称(在这种情况下, Species )。 Lastly, I want to be able to update the x-axis and y-axis using the selectInput boxes, but when you run the app, it does not update.最后,我希望能够使用selectInput框更新 x 轴和 y 轴,但是当您运行应用程序时,它不会更新。

Can anyone show me what I am doing wrong and kindly suggest edits?任何人都可以告诉我我做错了什么并提出修改建议吗?

Thanks :)谢谢 :)

Dont use observe event but rather reactive values , and let plotly handle the rest internally不要使用观察事件,而是使用反应值,让内部处理其余部分

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM