ggplot的geom_vline返回无法找到反应变量的对象

时间:2018-02-16 15:50:35

标签: r ggplot2 shiny

我试图在有光泽的情节上绘制一条垂直线,其中x截距是两个用户输入的函数。该变量用于其他输出,因此我创建它并将其存储在变量中。我可以在renderText中打印变量,所以我知道计算是正确的,但是ggplot返回“object'xintercept'not found”。以下是无法正常工作的服务器功能。注意ggplot调用,当删除geom_vline时,查找工作。此外,如果time_after_formation()包装在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)

1 个答案:

答案 0 :(得分:0)

首先在反应环境中创建绘图,然后使用渲染图绘制它,如下所示:

@model CSC.ViewModels.ReportInfoViewModel


@using (Html.BeginForm("ViewReport", "Reports", Model, FormMethod.Post, new { target = "_blank" }))
{
if (@Model.plaza_param.ToString().ToLower().Equals("y"))
    {
    @Html.DevExpress().ListBox(
        l =>
        {
            l.Name = "lstPlazaParam";
            l.Width = Unit.Pixel(300);
            l.Height = Unit.Pixel(120);
            l.Properties.SelectionMode = ListEditSelectionMode.CheckColumn;
            l.Properties.EnableSelectAll = true;
            l.Properties.TextField = "facility_name";
            l.Properties.ValueField = "facility_id";
            l.SelectedIndex = 0;
            l.Properties.ValueType = typeof(string);
            l.Properties.ValidationSettings.RequiredField.IsRequired = true;
            l.Properties.ValidationSettings.RequiredField.ErrorText = "Please select a Plaza";
            l.Properties.ValidationSettings.ErrorText = l.Properties.ValidationSettings.RequiredField.ErrorText;
            l.Properties.ValidationSettings.ErrorTextPosition = ErrorTextPosition.Bottom;
            l.Properties.ValidationSettings.Display = Display.Dynamic;
            l.Properties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.ImageWithText;
        }).BindList(Model.facilitieslist).GetHtml();
        ViewContext.Writer.WriteLine("<br />");
}

<强> EDITED 我找到了你的问题,变量是[HttpPost] [Authorize] public ActionResult ViewReport(ReportInfoViewModel _model) { string _parameterList = ""; ReportViewerViewModel _rptObj = new ReportViewerViewModel(); if (_model.plaza_param.ToLower().Equals("y")) { string[] _selected = DevExpress.Web.Mvc.ListBoxExtension.GetSelectedValues<string>("lstPlazaParam"); string subParam = "plazaparam="; subParam += String.Join(",", _selected); _parameterList = string.Concat(_parameterList, "@", subParam); _parameterList = string.Concat(_parameterList, "@usrplazaparam=", getSelectedPlazaDisplayValues(_model.facilitieslist, _selected));**//here, _model.facilitieslist is null** } return View("AfterView", _rptObj); } ,这就是为什么它不起作用。只需在 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