R语言 “警告:错误:Tibble列必须具有兼容的大小,”

izkcnapc  于 2023-05-20  发布在  其他
关注(0)|答案(1)|浏览(329)

更新:最初我试图使用plotly&ggplot与shinydashboard,但后来放弃了ggplot。我正在尝试分配hoverinfo数据,但是,我现在得到一个错误。“警告:错误:Tibble列必须具有兼容的大小。

  • 大小0:列xycolor
  • 尺寸2:色谱柱text。i只回收大小为1的值。一百一十四:”

下面是我的尝试。

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)
b1uwtaje

b1uwtaje1#

整个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并且只有一个点要绘制时,似乎确实存在问题。似乎由于某种原因,这些事件的组合禁用了悬停文本。这可能是一个bug。

相关问题