繁体   English   中英

错误:- R 的 Shiny 应用程序中的“数学函数中的非数字参数”

[英]Error:- “Non numeric argument in maths function” in Shiny application of R

假设我有以下 function

abc<-function(obs, dist, dir)
{
n=obs
if(dist=='normal1')
x<-rnorm(n, mean=0, sd=1)

if(dist=='normal2')
x<-rnorm(n, mean=0, sd=2)

if(dir=='right')
y<-qbinom(1-0.05, n, 0.5, lower.tail=TRUE, log.p=FALSE)
if(dir=='left')
y<-qbinom(0.05, n, 0.5, lower.tail=TRUE, log.p=FALSE)
if(dir=='both')
y<-qbinom(c(0.05/2, 1-(0.05/2)), n, 0.5, lower.tail=TRUE, log.p=FALSE)


P<-data.frame("mean"=mean(x), "observation"=n, "direction"=y)
return(P)
}

我在 markdown 文档中使用此 function 使用 shiny 制作交互式数据框。 我使用了以下代码:


library(shiny)
shinyApp(
ui <- fluidPage(
  titlePanel("comparison of means"),
  fluidRow( 
    column(3,
           radioButtons("dist", h3("Distribution"),
                        choices = c("normal1" , "normal2"
                                      ))),   

  column(4,
           radioButtons("dir", h4("Direction"),
                        choices = c("left" , "right", "both"
                                      ))), 

  column(5,
         radioButtons("obs", h5("observation"),
                      choices = c(1,2,3,4,5 
                      )))


  ),  
  tableOutput("table")

),

server<-function(input, output){
output$table<-renderTable( {abc(input$obs, input$dist, input$dir)})


}
)

当我运行应用程序时,我收到以下错误:-

Warning: Error in qbinom: Non-numeric argument to mathematical function

在计算qbinom function 时,字符参数dir可能会产生一些问题。 任何人都可以告诉我我在哪里犯错了吗?

radioButtons提供一个字符值。 您必须在 renderTable 中将 input$ input$obs替换为renderTable as.numeric(input$obs)

对于观察组件, numericInput可能比radioButtons更适合(如果使用numericInput as.numeric

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM