R ggplot&x27;s geom_vline返回未找到反应变量的对象

R ggplot&x27;s geom_vline返回未找到反应变量的对象,r,ggplot2,shiny,R,Ggplot2,Shiny,我试图在shiny中绘制一条垂直线,其中x截距是两个用户输入的函数。该变量用于其他输出,因此我创建它并将其存储在变量中。我可以在renderText中打印变量,这样我就知道计算是正确的,但是ggplot返回“object‘xintercept’not found”。下面是不起作用的服务器功能。注意ggplot调用,当geom_vline被移除时,works find。此外,如果在renderText()中包装时间_,则效果也很好 library(shiny) library(ggplot2) l

我试图在shiny中绘制一条垂直线,其中x截距是两个用户输入的函数。该变量用于其他输出,因此我创建它并将其存储在变量中。我可以在renderText中打印变量,这样我就知道计算是正确的,但是ggplot返回“object‘xintercept’not found”。下面是不起作用的服务器功能。注意ggplot调用,当geom_vline被移除时,works find。此外,如果在renderText()中包装时间_,则效果也很好

library(shiny)
library(ggplot2)
library(data.table)

ui = fluidPage(
      sidebarLayout(

        # Inputs
        sidebarPanel(
          numericInput(inputId = 'lon', label="Longitude:", value=-20),
          numericInput(inputId = 'lat', label="Lattitude:", value=20),
          numericInput(inputId = 'time_bp', label="Time:", value=65)
          ), 
        mainPanel(
          plotOutput(outputId="ocean_sub", width="auto", height="200px")
          )))


server = function(input, output){
  elementwise.all.equal <- Vectorize(function(x, y) {isTRUE(all.equal(x, y))})
  seafloor_age_drop = data.table(lon=-20, lat=20, age=150)
  setnames(floor_age_drop, c("lon", "lat", "age"))
  setkey(floor_age_drop, lon, lat)

  # Make model 
  model_age = seq(0,200, by=0.1)
  crosby_depth = 
    ifelse(model_age <=75, 2652+324*sqrt(model_age), 
           ifelse(model_age>75 & model_age <=160, 5028+5.26*model_age-250*sin(((model_age-75)/30)), 
                  ifelse(model_age>160, 5750,NA)))
  crosby_model = data.frame(age=model_age, depth=crosby_depth)



  # Reactives 
  age_from_grid = reactive({floor_age_drop[.(round(input$lon, digits=1), round(input$lat, digits=1)),'age']})
  time_after_floor_formation = reactive({age_from_grid() - input$time_bp})
  depth_crosby = reactive({crosby_model$depth[elementwise.all.equal(crosby_model$age, round(time_after_floor_formation(), 1))]
    })

  output$ocean_sub=renderPlot({
    ggplot(crosby_model, aes(x=age, y=depth)) + geom_line() +
 scale_y_reverse(lim=c(6000, 2500)) +
 geom_vline(xintercept=time_after_floor_formation(), colour='red')})

}

shinyApp(ui=ui, server=server)
库(闪亮)
图书馆(GG2)
库(数据表)
ui=fluidPage(
侧边栏布局(
#投入
侧栏面板(
数值输入(inputId='lon',label=“经度:”,value=-20),
数值输入(inputId='lat',label=“latitude:”,value=20),
数值输入(inputId='time\u bp',label=“time:”,value=65)
), 
主面板(
plotOutput(outputId=“ocean\u sub”,width=“auto”,height=“200px”)
)))
服务器=功能(输入、输出){

elementwise.all.equal首先在反应式环境中创建绘图,然后使用渲染绘图进行绘图,如下所示:

 p = reactive({ggplot(my_model, aes(x=age, y=depth)) + geom_line() +
    scale_y_reverse(lim=c(6000, 2500)) + 
    geom_vline(xintercept=time_after_formation(), colour='red')})

output$ocean_sub = renderPlot({p})
已编辑 我发现了你的问题,那就是变量是
data.table
,这就是它不起作用的原因。只要把它写成.numeric
就可以了

 ggplot(crosby_model, aes(x=age, y=depth)) + geom_line() +
      scale_y_reverse(lim=c(6000, 2500)) +
      geom_vline(xintercept=as.numeric(time_after_floor_formation()), colour='red')

你能做一个可复制的例子吗?用a(希望)更改完全可复制的示例。我也不能让它工作-绘图不再抛出错误,而是消失。我用一个完全可复制的示例更改了我的OP。@MurrayHoggett我不知道为什么你说如果它使用.csv数据,使用类似mtcars的内容,那么它是可复制的。这个.csv内容是有问题的,但现在更改为一个line csv等效文件已读入。代码现在可用于复制和粘贴。