[英]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.