RShiny UI.r中的错误:缺少参数“变量”,没有默认值

时间:2019-04-18 19:47:20

标签: r ggplot2 shiny rpart

尝试创建ui.r文件以显示Pert分布

我遇到以下错误:ui.r错误:缺少参数“变量”,没有默认值

我看到的常见解决方法是删除不必要的逗号,我相信我已经做到了。

server <- function(input, output){

  BS = function(n, births, cat2, statusmin, statusmode, statusmax, impactmin, impactmode, impactmax){
    d1 = births*cat2*rpart(n,statusmin,statusmode,statusmax)*rpart(n,impactmin,impactmode,impactmax)
    return(d1)
  }

output$plotCall <- renderPlot({
  n = input$n
  births = input$births
  cat2 = input$cat2
  statusmin = input$statusmin
  statusmode = input$statusmode
  statusmax = input$statusmax
  impactmin = input$impactmin
  impactmode = input$impactmode
  impactmax = input$impactmax

  gg <- ggplot(data.frame(BS()), aes(x = BS))
  gg <- gg + geom_histogram(aes(y = ..density..),color = "black", fill = "white",
                            binwidth = 2 * IQR(BS) / length(BS)^(1/3))
  gg <- gg + geom_density(fill = "steelblue", alpha = 1/3)
  gg <- gg + scale_x_continuous(labels = comma)
  gg <- gg + theme_bw()
  plot(gg, labels = TRUE, conf.level = .8)
  }

)
}

ui <- shinyUI(fluidPage(

  titlePanel("ROI"),

  sidebarLayout(
    sidebarPanel(
      numericInput('n', 'Number of Simulations', 1000, min = 1, max = 1000, step = 1),
      numericInput('birth', 'Number of Births', 6811, min = 1, max = 10000, step = 1),
      numericInput('cat2', 'Percentage of Category II Strips', 0.84, min = 0.01, max = 1, step = 0.01),
      numericInput('statusmin', '% Status Min', 0.1, min = 0.01, max = 1, step = 0.01),
      numericInput('statusmode', '% Status Most Likely', 0.3, min = 0.01, max = 1, step = 0.01),
      numericInput('statusmax', '% Status Max', 0.4, min = 0.01, max = 1, step = 0.01),
      numericInput('impactmin', '% Impact Min', 0.2, min = 0.01, max = 1, step = 0.01),
      numericInput('impactmode', '% Impact Most Likely', 0.4, min = 0.01, max = 1, step = 0.01),
      numericInput('impactmax', '% Impact Max', 0.64, min = 0.01, max = 1, step = 0.01)


    ),

    mainPanel(
      textOutput("BScall"),
      hr(),
      tabsetPanel(
        tabPanel("Calls", plotOutput("plotCall",width="100%"))
      )
    )
  )
))


shinyApp(ui = ui, server = server)

我正在尝试获得d1结果的直方图。

1 个答案:

答案 0 :(得分:0)

我在这段代码中发现了几个问题。正如Wil在评论中指出的那样,您将BS定义为具有多个参数的函数,但是调用BS()时并没有为其分配任何参数。

因此,我进行的第一个更改是定义一个名为result_d1的变量,该变量接收来自BS(n, births, cat2, statusmin, statusmode, statusmax, impactmin, impactmode, impactmax)的输出。然后,我将此变量传递给ggplot

此处:gg <- ggplot(data.frame(result_d1), aes(x=result_d1));

和此处:

gg + geom_histogram(aes(y = ..density..),color = "black", fill = "white",
                              binwidth = 2 * IQR(result_d1) / length(result_d1)^(1/3))

另一个问题是您打电话给input$births,但您的numericInput的ID是“出生”。我将其更改为“出生”。

即使在这些修复之后,我们也遇到函数rpart的问题,正如Wil指出的那样。我不熟悉此函数及其包,但是由于您说过要绘制Pert分布,因此我将使用dpert包中的函数mc2d来获取直方图的值。我不知道这是否正是您想要的,但是使用使用此dpert的有效代码,您可以进行必要的更改以使用rpart函数。

最后一件事,我将scale_x_continuous(labels = comma)更改为scale_x_continuous(labels = scales::comma)

这是完整的代码:

server <- function(input, output){

  BS = function(n, births, cat2, statusmin, statusmode, statusmax, impactmin, impactmode, impactmax){

    x.status <- seq(statusmin, statusmax, length.out= n)
    x.impact <- seq(impactmin, impactmax, length.out= n)

    d1 = births*cat2*
      dpert(x.status, min=statusmin, mode=statusmode, max=statusmax)*
      dpert(x.impact, min=impactmin, mode=impactmode, max=impactmax)

    return(d1)
  }

  output$plotCall <- renderPlot({
    n = input$n
    births = input$births
    cat2 = input$cat2
    statusmin = input$statusmin
    statusmode = input$statusmode
    statusmax = input$statusmax
    impactmin = input$impactmin
    impactmode = input$impactmode
    impactmax = input$impactmax

    result_d1 <- BS(n, births, cat2, statusmin, statusmode, statusmax, impactmin, impactmode, impactmax)

    gg <- ggplot(data.frame(result_d1), aes(x=result_d1))
    gg <- gg + geom_histogram(aes(y = ..density..),color = "black", fill = "white",
                              binwidth = 2 * IQR(result_d1) / length(result_d1)^(1/3))
    gg <- gg + geom_density(fill = "steelblue", alpha = 1/3)
    gg <- gg + scale_x_continuous(labels = scales::comma)
    gg <- gg + theme_bw()
    plot(gg, labels = TRUE, conf.level = .8)
  }
  )
}

ui <- shinyUI(fluidPage(

  titlePanel("ROI"),

  sidebarLayout(
    sidebarPanel(
      numericInput('n', 'Number of Simulations', 1000, min = 1, max = 1000, step = 1),
      numericInput('births', 'Number of Births', 6811, min = 1, max = 10000, step = 1),
      numericInput('cat2', 'Percentage of Category II Strips', 0.84, min = 0.01, max = 1, step = 0.01),
      numericInput('statusmin', '% Status Min', 0.1, min = 0.01, max = 1, step = 0.01),
      numericInput('statusmode', '% Status Most Likely', 0.3, min = 0.01, max = 1, step = 0.01),
      numericInput('statusmax', '% Status Max', 0.4, min = 0.01, max = 1, step = 0.01),
      numericInput('impactmin', '% Impact Min', 0.2, min = 0.01, max = 1, step = 0.01),
      numericInput('impactmode', '% Impact Most Likely', 0.4, min = 0.01, max = 1, step = 0.01),
      numericInput('impactmax', '% Impact Max', 0.64, min = 0.01, max = 1, step = 0.01)


    ),

    mainPanel(
      textOutput("BScall"),
      hr(),
      tabsetPanel(
        tabPanel("Calls", plotOutput("plotCall",width="100%"))
      )
    )
  )
))


shinyApp(ui = ui, server = server)

输出:

enter image description here