【问题标题】:Error in RShiny UI.r: argument "variable" is missing, with no defaultRShiny UI.r 中的错误:缺少参数“变量”,没有默认值
【发布时间】:2019-09-09 04:38:48
【问题描述】:

尝试创建一个用于显示 Pert 分布的 ui.r 文件

我收到以下错误: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 结果的直方图。

【问题讨论】:

  • 我正在尝试获取 d1 结果的直方图。
  • 您希望d1 代表什么?调用rpart() 返回一个类rpart 的对象,并且您将参数传递给rpart() 的方式不是有效调用。查看documentation 以了解如何为 rpart 输入公式。它将返回一个模型对象,因此将其包含在乘法调用中将不起作用。此外,您将BS() 定义为一个函数,但在您对ggplot() 的调用中,您没有为您的函数BS 提供任何参数,因此它将无法计算任何内容。

标签: r ggplot2 shiny rpart


【解决方案1】:

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

所以,我所做的第一个更改是定义一个名为result_d1 的变量,它接收来自BS(n, births, cat2, statusmin, statusmode, statusmax, impactmin, impactmode, impactmax) 的输出。然后我把这个变量传给ggplot

这里:gg &lt;- 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 是“出生”。我把它改成了“出生”。

正如 Wil 指出的那样,即使在进行了这些修复之后,函数 rpart 仍然存在问题。我不熟悉这个函数,也不熟悉它的包,但既然你说你想绘制一个 Pert 分布,我将使用 mc2d 包中的函数 dpert 来获取直方图的值。我不知道这是否正是您想要的,但是通过使用此 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)

还有输出:

【讨论】:

    猜你喜欢
    • 2016-06-08
    • 2017-12-09
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-29
    • 1970-01-01
    相关资源
    最近更新 更多