r语言 - 将悬停信息添加到过滤图时"Warning: Error in : Tibble columns must have compatible sizes."



已更新:最初我试图使用plotly&ggplot与shinydashboard合作,但后来放弃了ggplot。我正在尝试分配悬停信息数据,但是,我现在遇到了一个错误。"警告:中的错误:Tibble列必须具有兼容的大小。

  • 大小0:列xycolor
  • 大小2:列text。i只回收大小为1的值。114:">

下面是我的尝试。

library(shinydashboard)
library(shinyWidgets)
library(shiny)
library(DT)
#______________________________________________________________________________#
server <- function(input, output, session) { 
df <- reactive({
subset(iris, Petal.Width %in% input$Petalw)
})

# Extract list of Petal Lengths from selected data - to be used as a filter
p.lengths <- reactive({
unique(df()$Petal.Length)
})

# Filter based on Petal Length
output$PetalL <- renderUI({
pickerInput("PetalLengthSelector", "PetalLength", as.list(p.lengths()), options = list(`actions-box` = TRUE),multiple = T)

})

# Subset this data based on the values selected by user
df_1 <- reactive({
foo <- subset(df(), Petal.Length %in% input$PetalLengthSelector)
return(foo)
})

output$table <- DT::renderDataTable(
DT::datatable(df_1(), options = list(searching = FALSE,pageLength = 25))
)


output$correlation_plot <- renderPlotly({
plot1 <- plot_ly(data=df_1(),
x = ~Petal.Length,
y = ~Petal.Width,
type = 'scatter',
#mode ="lines+markers",
color =~Petal.Length,
text = paste("Sepal.Length:",~Sepal.Length,"<br>",
"Sepal.Width:",~Sepal.Width,"<br>",
"Petal.Length:",~Petal.Length,"<br>",
"Petal.Width:",~Petal.Width,"<br>",
"Species:",~Species),
hoverinfo = 'text'

)
})

}
#______________________________________________________________________________#
ui <- navbarPage(
title = 'Select values in two columns based on two inputs respectively',

fluidRow(
column(width = 12,
plotlyOutput('correlation_plot')
)
),


fluidRow(
column(width = 6,
pickerInput("Petalw","PetalWidth", choices = unique(iris$Petal.Width),selected = c("PetalWidth"), options = list(`actions-box` = TRUE),multiple = T)
),
column(width = 6,
uiOutput("PetalL")
)
),

fluidRow(
column(12,
tabPanel('Table', DT::dataTableOutput('table'))
)
)
)
shinyApp(ui, server)

整个text=参数需要是一个公式。~不仅仅表示数据列名,它还用于未赋值的表达式。因此,一个合适的工作示例应该看起来像

output$correlation_plot <- renderPlotly({
fig <- plot_ly(
data = df_1(),
x = ~Sepal.Length, 
y = ~Sepal.Width, 
type = 'scatter', 
mode = 'markers',
text = ~paste("Sepal.Length:",Sepal.Length,"<br>",
"Sepal.Width:",Sepal.Width,"<br>",
"Petal.Length:",Petal.Length,"<br>",
"Petal.Width:",Petal.Width,"<br>",
"Species:",Species),
hoverinfo = 'text'
) 
})

当你尝试使用color =~Petal.Length,而你只有一个点要绘制时,似乎确实存在问题。似乎由于某种原因,这种事件组合禁用了悬停文本。这可能是一个错误。

最新更新