簡體   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