0

Please I need assistant concerning a shiny code. I want to manipulate a data frame input by separating them into column vector for computation but I keep getting this error

Warning in <reactive>(...): NAs introduced by coercion

the code is as follows

 library(shiny)
ui <- fluidPage(

  # dataset
  data <- data.frame(e1 = c(3, 7, 2, 14, 66),
             e2 = c(2, 16, 15, 66, 30),
             n1 = c(18, 25, 45, 62, 81), 
             n2= c(20, 30, 79, 64, 89))   
# Application title
titlePanel("Demo"),

# Sidebar with a slider input for number of bins 
sidebarLayout(
  sidebarPanel(
     # Input: Upload file
    fileInput(inputId = 'file', label = 'upload the file')
    ),

 # Display Output
  mainPanel(
    uiOutput("final")
    )
  )
 )
# Define server logic required to draw a histogram
server <- function(input, output) {

   # separating the dataframe into 4 column vectors
   e1 <- reactive(as.numeric(input$file[,1]))
   e2 <- reactive(as.numeric(input$file[,2]))
   n1 <- reactive(as.numeric(input$file[,3]))
   n2 <- reactive(as.numeric(input$file[,4]))

   # File Upload function
   data <- reactive({
   file1 <- input$file
   if(is.null(file1)){return()}
   read.table(file = file1$datapath, sep = ',', header = TRUE)
   })


   output$result <- renderUI({
     y <- (e1()/n1()) - (e2()/n2())
    lg_y <- log(y)
    v2 <- ((n1() - e1())/e1() * n1()) + ((n2() - e2())/e2() * n2())
    w <- 1/v2
    w1 <- sum(w)
    w2 <- sum(w^2)
    c <- w1 - (w2/w1)
    s2 <- w * lg_y
    ybar <- sum(s2)/sum(w)
    Q <- sum(w*((lg_y - ybar)^{2}))# Cochrane homogeneity test statistic
    Q.pval <- reactive(pchisq(Q, k() - 1,lower.tail = FALSE))
    Isqd <- max(100*((Q-(k()-1))/Q),0)
   })
 }
 # Run the application 
 shinyApp(ui = ui, server = server)

I have searched almost every question on this forum and haven't seen where the question was answered. please I look forward to your help

gbenga
  • 3
  • 4

1 Answers1

0

Still can't run the code above because you don't define function k(). Also FYI, your renderUI is set for "result" but your uiOutput is set for "final".

You get the warning Warning in <reactive>(...): NAs introduced by coercion because your true data set probably includes a non-numeric in it. I did not get any issues with the data set you provided above.

There are a couple ways forward:

1) Write a function to remove all non-numerics before you process the data. See here for a few examples.

2) Just keep the warning, it is a warning after all so it won't stop your code from running. Currently it turns your non-numerics into NA

3) Use suppressWarnings() but that is usually not recommended.

I do have a suggestion to clean up your code though:

   # File Upload function
   data <- reactive({
   file1 <- input$file
   if(is.null(file1)){return()}
   read.table(file = file1$datapath, sep = ',', header = TRUE, stringsAsFactors = FALSE)
   })

# separating the dataframe into 4 column vectors
   e1 <- reactive(as.numeric(data()[,1]))
   e2 <- reactive(as.numeric(data()[,2]))
   n1 <- reactive(as.numeric(data()[,3]))
   n2 <- reactive(as.numeric(data()[,4]))
Kevin
  • 1,974
  • 1
  • 19
  • 51
  • Thanks kelvin. I cleaned up my code as suggested, it worked – gbenga Jan 05 '20 at 20:53
  • No problem! Feel free to click the green check mark that my answer was correct so it can help other people viewing know it worked. – Kevin Jan 05 '20 at 23:07