R error using Knitr

I am attempting to compile code using Knitr in R.

My code below is returning the following error, and causes errors in the rest of the document.

miss<-sample$sensor_glucose[!is.na(sample$sensor_glucose)]
# Error: "## Warning: is.na() applied to non-(list or vector) of type 'NULL'"

str(miss)
# int [1:103] 213 113 46 268 186 196 187 153 43 175 ...


Does anyone know how to remedy this problem?

• Have you tried running that code in pure R, not Knitr? It seems to be more of an R problem and rather than a rendering to graphics problem. – mike1886 Aug 7 '14 at 15:14
• The code produces no error when run in R. – Ivoire Aug 7 '14 at 15:40
• As it stands, this example is not reproducible and therefore nobody can help you. Can you provide a minimal working example (MWE) of your .Rmd file that replicates the error? Chances are, constructing the MWE will also help you figure out what the problem is. However, my guess is that you aren't loading your data anywhere in the code. Knitr searches a new environment and not the current global environment, so you will need to re-load all packages and objects you plan to use. – shadowtalker Aug 7 '14 at 16:45
• As @ssdecontrol suggests, you probably haven't created or loaded sample in the R in your document subsequent to this line. – conjugateprior Aug 17 '14 at 14:21

I agree with @ssdecontrol that a minimal reproducible example would be the most helpful. However, looking at your code (pay attention to the sequence Error: ... Warning: ...), I believe that the issue you are experiencing is due to an inappropriate setting of R's global warn option. It appears that your current setting is likely 2, which refers to converting warnings to errors, whereas, you, most likely want the setting 1, which is to treat warnings as such, without converting them to errors. If that is the case, you just need to set the option appropriately:
options(warn=1)  # print warnings as they occur