R 不支持的索引类型:空-->绘图图表

R 不支持的索引类型:空-->绘图图表,r,shiny,plotly,R,Shiny,Plotly,我在使用plotly和SHINK中的反应值时遇到了一个绘图索引错误。侧边栏面板加载时没有问题,但显示图表时出现问题,我无法确定。如果您能帮助解决索引问题,我们将不胜感激。谢谢 library(shiny) library(plotly) data(economics, package = "ggplot2") nms <- names(economics) ui <- fluidPage( headerPanel("TEST"), sidebarPanel(

我在使用plotly和SHINK中的反应值时遇到了一个绘图索引错误。侧边栏面板加载时没有问题,但显示图表时出现问题,我无法确定。如果您能帮助解决索引问题,我们将不胜感激。谢谢

library(shiny)
library(plotly)

data(economics, package = "ggplot2")


nms <- names(economics) 

ui <- fluidPage(

  headerPanel("TEST"),
  sidebarPanel(
    selectInput('x', 'X', choices = nms, selected = nms[[1]]),
    selectInput('y', 'Y', choices = nms, selected = nms[[2]]),
    sliderInput('plotHeight', 'Height of plot (in pixels)', 
                min = 100, max = 2000, value = 1000)
  ),
  mainPanel(
    plotlyOutput('trendPlot', height = "900px")
  )
)

server <- function(input, output) {

  #add reactive data information. Dataset = built in diamonds data
  dataset  <- reactive({economics[, c(input$xcol, input$ycol)]
  })

  output$trendPlot <- renderPlotly({

    # build graph with ggplot syntax
    p <- ggplot(dataset(), aes_string(x = input$x, y = input$y)) + 
      geom_line()


    ggplotly(p) %>% 
      layout(height = input$plotHeight, autosize=TRUE)

  })

}

shinyApp(ui, server)
警告:错误:不支持的索引类型:NULL

您错误地使用了xcol和ycol,不知道为什么。如果没有这些名称,代码就可以正常工作

library(shiny)
library(plotly)
library(tidyverse)

data(economics, package = "ggplot2")


nms <- names(economics) 

ui <- fluidPage(

  headerPanel("TEST"),
  sidebarPanel(
    selectInput('x', 'X', choices = nms, selected = nms[[1]]),
    selectInput('y', 'Y', choices = nms, selected = nms[[2]]),
    sliderInput('plotHeight', 'Height of plot (in pixels)', 
                min = 100, max = 2000, value = 1000)
  ),
  mainPanel(
    plotlyOutput('trendPlot', height = "900px")
  )
)

server <- function(input, output) {

  #add reactive data information. Dataset = built in diamonds data
  dataset  <- reactive({
    economics[, c(input$x, input$y)]

  })

  output$trendPlot <- renderPlotly({


    # build graph with ggplot syntax
    p <- ggplot(dataset(), aes_string(input$x, input$y)) + 
      geom_line()


    ggplotly(p, height = input$plotHeight)

  })

}

shinyApp(ui, server)

在您的反应式代码中将xcol和ycol替换为x和y,因为没有使用xcol、ycol名称的inputId。i、 e,dataset@amrrs,非常感谢。我回复了代码,我现在得到了,主题元素“text”有空属性:margin,debugWarning:ggplot2::calc_元素中的错误:主题元素“text”有空属性:margin,debug[没有可用的堆栈跟踪]奇怪,我没有得到那个警告。你什么时候得到它?它以红色显示,在应用程序运行后,图表应该显示在那里。@Joe你在我的答案中使用了代码吗?它使用了吗?