The error: Rstudio is flagging an error where there is none. All code runs and works as you'd expect.
I'm using the Whickham data set, categorical data on whether smokers lived or not. Most conveniently found in the mosaicData package.
These are the functions within two lines of the error in both directions, including the blank line and the comment. There are no with() or attach() blocks or anything silly like that in play.
The xtabs() function asks for the formula to be entered as you see. The help page says to place them to the right of the tilde, connected by a plus sign.
EDIT: From comments below, I can tell that I am not communicating well. The error I'm speaking of is the one that RStudio is marking on the xtabs() line. You see the yellow triangular caution symbol there, line 12, and if you hover over the icon, you get the message in the hint box that's also pictured. The code itself works just fine; it's the non-error error that I'm reporting.
My recommendation is to press Ctrl + Shift + F10 to restart R from within RStudio, save your file and check if the error persists. Also, I understand why you only provided a snapshot of your code (i.e. you only wanted to show the part of the code that matters); however, it would help if you provided the full code. I have a hunch, which has a fairly low probability, but "Did you by any chance load a package that contains another xtabs function which masks the base R xtabs() function? " The reason why I am asking is because the help file for the base R xtabs() function states that the subset argument is optional!
I'm happy to provide the rest of my code; there's almost nothing there as I've just started writing the chapter that involves categorical models, and I'm stalled until I figure out how to specify colors in vcd's mosaic() function.
Restarting R didn't help. I'll restart everything when I'm at a point where I've got everything saved (I have not figured out how to use projects yet due to a Catch-22: My publishing deadlines are so short that I haven't got time to learn something new, even though I think using projects would save me time and worry).
Here's the entire set of code I've built up:
require(mosaicData)
require(vcd)
require(datasets)
#### Look at some data ####
levels(Whickham$outcome)
levels(Whickham$smoker)
table(Whickham$smoker, Whickham$outcome) # No column names
with(Whickham, table(outcome, smoker)) # Column names appear
xtabs(~ outcome + smoker, data = Whickham) # Same appearance as with(table())
#### Graphical Methods ####
WhickTable = table(Whickham$smoker, Whickham$outcome)
mosaicplot(WhickTable)
vcd doesn't have an xtabs() function, and I'm guessing datasets and mosaicData don't either.
For updating in regrards @apreshill answer, I run the code from the linux console. In Rstudio Version 1.1.463 I cannot reproduce the message that appear on the screencapt either. It works fine.
> require(mosaicData)
Loading required package: mosaicData
> require(vcd)
Loading required package: vcd
Loading required package: grid
> require(datasets)
>
> #### Look at some data ####
> levels(Whickham$outcome)
[1] "Alive" "Dead"
> levels(Whickham$smoker)
[1] "No" "Yes"
>
> table(Whickham$smoker, Whickham$outcome) # No column names
Alive Dead
No 502 230
Yes 443 139
> with(Whickham, table(outcome, smoker)) # Column names appear
smoker
outcome No Yes
Alive 502 443
Dead 230 139
> xtabs(~ outcome + smoker, data = Whickham) # Same appearance as with(table())
smoker
outcome No Yes
Alive 502 443
Dead 230 139
>
> #### Graphical Methods ####
> WhickTable = table(Whickham$smoker, Whickham$outcome)
> mosaicplot(WhickTable)
> sessionInfo()
R version 3.4.4 (2018-03-15)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Linux Mint 18.3
Matrix products: default
BLAS: /usr/lib/openblas-base/libblas.so.3
LAPACK: /usr/lib/libopenblasp-r0.2.18.so
locale:
[1] LC_CTYPE=en_AU.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_AU.UTF-8 LC_COLLATE=en_AU.UTF-8
[5] LC_MONETARY=en_AU.UTF-8 LC_MESSAGES=en_AU.UTF-8
[7] LC_PAPER=en_AU.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_AU.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] grid stats graphics grDevices utils datasets methods
[8] base
other attached packages:
[1] vcd_1.4-4 mosaicData_0.17.0
loaded via a namespace (and not attached):
[1] zoo_1.8-5 colorspace_1.4-1 MASS_7.3-50 compiler_3.4.4
[5] lmtest_0.9-36 lattice_0.20-38
>
Thanks @apreshill! I edited the original post to be clearer about what I am reporting.
I'm writing a book about R that is supposed to be applicable for any R 3.5, whether the reader is using an IDE or not. I've used R in a lot of places, but I've no experience with R on Windows. Rather than worry about needing to load datasets or not, I put the call in there just to be over careful.
No other packages loaded aside from those that are automatically loaded without my intervention. In fact, I cleared out absolutely everything when I started this chapter.
I realised what you mean after my answer, so I updated there.
Here is my screen-capt. No sign of any warning on the code, or yellow triangle with Rstudio Version 1.1.463 at least in my laptop.
This isn't really code meant to be run from start to finish. It's basically a log of every command I execute in the text of my chapter. So, at some point, I have the reader look at the levels() of the two variables of interest. I also show three ways to generate a summary table.
That's the end of the introduction section. I'm now writing the first real section of the chapter, showing graphical methods of looking at contingency tables. The vcd package has a mosaic plot command mosaic() that I'm struggling with. I can't seem to figure out how to specify colors in it. Once frustrated enough, I stopped for the day, at which point I filed the error here.
@leecreighton Have you tried to run your code in the R Gui? You should not get an error there. If the error only shows up in RStudio, then you should update as @apreshill suggested.
Also, not really related to the original issue, but if you are teaching students how to use R, I suggest you teach them to load packages with library() rather than require(). For more explanations, check out the following:
Regarding this bit, there is literally nothing to learn about using projects. When I first started using RStudio (after using RGui for some time prior to this) I brushed them aside as they looked like another distraction without any obvious benefits that I could see.
Then I created one and have literally never done any work without using them since. I'm not aware of any downsides and there are plenty of great benefits. And no learning required: just File > New Project ...
I tried that, but couldn't identify all the file types that are created, and I wasn't able to answer whether this blocked people who used another IDE, or no IDE, on other platforms.
I've also got to synchronize everytjing using GitHub, which was a second layer of unknowns.
I'm sure it's all perfectly easy and logical, and it's on my list of things to learn after the book is completely.
Creating a project from RStudio just creates one file (with the .Rproj extension) and it has no impact on your work other than making it convenient to work from RStudio. People who use other IDEs will still be able to use the .R files without any problem.
Using Github is still not an issue and it is actually good practice to also add the .Rproj file to the files you synch to Github.
Glad to know that using projects is on your list of things to learn... but there's really not much to learn as mentioned by @martin.R.
Would you mind filing this as a bug report at https://github.com/rstudio/rstudio/issues? It looks like the RStudio static analyzer fails to understand how the xtabs function uses the subset argument.