简体   繁体   中英

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

Suppose I have the following 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)
}

I am using this function in a markdown document to make an interactive data frame using shiny. I used the following code:


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)})


}
)

While I run the application I get the following error:-

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

Probably the character argument dir is making some problem while computing the qbinom function. Could it be possible for anyone to show me where I am making mistake?

radioButtons provides a character value. You have to replace input$obs by as.numeric(input$obs) in renderTable .

numericInput might be better suited than radioButtons for the observations component (no need of as.numeric if you use numericInput ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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