【问题标题】:Error:- "Non numeric argument in maths function" in Shiny application of R错误:- R 的闪亮应用中的“数学函数中的非数字参数”
【发布时间】:2020-04-08 23:51:25
【问题描述】:

假设我有以下功能

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 文档中使用这个函数来制作一个使用闪亮的交互式数据框。我使用了以下代码:


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

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

【问题讨论】:

    标签: r shiny


    【解决方案1】:

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

    numericInput 可能比radioButtons 更适合观察组件(如果您使用numericInput,则不需要as.numeric)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-24
      • 1970-01-01
      • 1970-01-01
      • 2015-06-05
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      相关资源
      最近更新 更多