You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

echarts4rBox函数无法识别变量,报错“object 'speed' not found”

问题解决:Error: object 'speed' not found

错误原因

调用echarts4rBox时的参数传递方式有误,直接写speed、dist会让R将其视为全局环境中的独立对象,而非cars数据框内的列,因此触发找不到对象的报错。

修正方案

使用公式语法(~变量名)指定数据框中的列,这是echarts4r系列函数推荐的变量映射方式;也可以通过数据框$列名的形式显式引用列。

修正后的完整代码

library(shiny)
library(echarts4r)

ui <- fluidPage(
  
  fluidRow(
    column(3, echarts4rBoxOutput("box1"))
  )
  
)

server <- function(input, output, session) {
  
  output$box1 <- renderEcharts4rBox({
    # 用公式语法映射数据列
    echarts4rBox(cars, x = ~speed, y = ~dist, title = "Cake", type = "bar")
  })
  
}

shinyApp(ui, server) 

替代写法(显式引用列)

如果不习惯公式语法,也可以直接指定数据框的列:

echarts4rBox(cars, x = cars$speed, y = cars$dist, title = "Cake", type = "bar")

内容的提问来源于stack exchange,提问作者neves

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 02:43:16