tidyverse interfering with ggplot2? cannot access map_data - r

Running these commands in the console, the output is:
> cty0 = ggplot2::map_data("county")
> library(tidyverse)
Loading tidyverse: ggplot2
Loading tidyverse: tibble
Loading tidyverse: tidyr
Loading tidyverse: readr
Loading tidyverse: purrr
Loading tidyverse: dplyr
Conflicts with tidy packages -----------------------------------------------------------------------------------------------
filter(): dplyr, stats
lag(): dplyr, stats
map(): purrr, maps
> cty0 = ggplot2::map_data("county")
Error: ggplot2 doesn't know how to deal with data of class list
I can call map_data("county") fine until tidyverseis loaded, then it fails. How do I load a county map data with tidyverse loaded?

Transferred the comment from above after testing:
I'm guessing that the items below the dashed line are from the console messages , but you really should clarify that . Seems likely that the map function in 'purrr' is masking the map function in the 'maps' package. You could reverse the order of loading tidyverse and maps if there were a reason that you needed the (geographic notion of) "mapping" more than you needed the (functional-computer language notion of) "mapping". You probably need to start a new session for that to succeed. The library function checks to see of a package is already loaded and if so does nothing.
A comment on terminology. My guess is that the computer operation of "mapping" is actually a contraction from "multiple application" (of a function to interim results). If there were a chance to go back and rename it to something that would be similar to a geographic concept, it might be named route()-ing. A geographic "map" would seem to be a static two or three-dimensional object or "mapping" to be placement of positions on such an object.
Seems to succeed:
# In a fresh session (and my profile attaches ggplot2 by default)
> library(tidyverse)
Loading tidyverse: tibble
Loading tidyverse: tidyr
Loading tidyverse: readr
Loading tidyverse: purrr
Loading tidyverse: dplyr
Conflicts with tidy packages ---------------------------------
combine(): dplyr, Hmisc # loaded in my .Rprofile; also attaches gglot2
filter(): dplyr, stats
lag(): dplyr, stats
matches(): dplyr, sos #from .Rprofile; doesn't seem to clobber findFn function
src(): dplyr, Hmisc
summarize(): dplyr, Hmisc
> cty0 = ggplot2::map_data("county")
Attaching package: ‘maps’
The following object is masked from ‘package:purrr’:
map

Related

Masked objects loading dplyr

Everytime I load the dplyr package the console shows a warning message.
warning message Some objetcs are masked from other packages. I think this is because the objects have the same name. For example:
Filter has this usage in dplyr packagefilter(.data, ..., .preserve = FALSE)
Filter has this usage in stats package filter(x, filter, method = c("convolution", "recursive"), sides = 2, circular = FALSE, init)
How can I unmask the filter object from stats package if I need to use it?
Regards
You are correct that they are simply packages that share the same name. The comments above basically answer the question already. Theoretically if you have a conflict of functions you want to avoid, you can also select which you prefer, like so:
library(conflicted)
conflict_prefer("slice", # the function
"dplyr") # the package
And R will tell you which it will use as your primary:
[conflicted] Will prefer dplyr::slice over any other package
However that is an extra step and I prefer usually to name it explicitly like dplyr::slice as mentioned in the comments instead.

conflicting filter command in R

I am using the usual filter R command. However, when I run this on some data.frame, such basic as filter(data,data$entry==some_data), the output is a time serie. This is obviously related to the time series libraries I imported. How can I fix it ?
I imported the following libraries
library(ggplot2)
library(dplyr)
library(zoo)
library(stringi)
library(gridExtra)
library(rCharts)
library(xts)
library(tseries)
library(forecast)
library(curl)
library(vars)
library(astsa)
library(urca)
library(fGarch)
The default filter when you start R is stats::filter, it is used on time series. dplyr should mask it when loaded, so maybe you didn't load dplyr? Or maybe another package you loaded afterwards masked the dplyr version...
You can always specify the version you need by using package::function notation, e.g., dplyr::filter(data, ...). You can also check on conflicts (multiple definitions of objects) with conflicts().
As a side note, you should not be using $ inside dplyr::filter for the data you pass in, it is built to work with unquoted column names:
filter(data,data$entry==some_data) # bad
filter(data, entry == some_data) # good

What is the purpose of dtplyr and the reason for the warning 'Please library(dtplyr)!'?

On loading the latest version of data.table (1.10.4) I get this message:
> library(data.table)
data.table 1.10.4
...
---------------------------------------------------------------------------------------------------------------------------------------------
data.table + dplyr code now lives in dtplyr.
Please library(dtplyr)!
---------------------------------------------------------------------------------------------------------------------------------------------
Attaching package: ‘data.table’
The following objects are masked from ‘package:dplyr’:
between, first, last
The following object is masked from ‘package:purrr’:
transpose
The message is not very helpful to explain why is it useful to use the dtplyr package. As far as I can see, as long as I avoid the listed conflicts I can use the data.table package normally? In fact, I can't library(dtplyr) without having the package installed, so the message itself does not provide very good instructions in this case.
The package dtplyr is really performant, it could really speed up the calculation when my dataframe is larger than 10^5 rows.
What I did is:
library(data.table)
library(dplyr)
library(dtplyr)
Best regards

Lubridate Objects Masked After Loading Data.Table

When I load the data.table package after having already loaded the lubridate package, I get the following error message:
Loading required package: data.table
data.table 1.9.4 For help type: ?data.table
*** NB: by=.EACHI is now explicit. See README to restore previous behaviour.
Attaching package: ‘data.table’
The following objects are masked from ‘package:lubridate’:
hour, mday, month, quarter, wday, week, yday, year
Does anyone know a) what's causing this issue and b) how to prevent these objects within lubridate from being masked?
UPDATE:
The issue associated with the above is that I'm using the quarter function from the lubridate package and, after loading the data.table package, I can no longer do so in the same way.
Specifically, when I run quarter(Date, with_year=TRUE) (where Date is a vector of class = Dates), I now get the following error: Error in quarter(Date, with_year = TRUE) : unused argument (with_year = TRUE).
If I simply, quarter(Date), then I can get the desired output without the attached year. For example, if Date is set as simply May 15, 2015 (today), then quarter(Date) will yield 2 (since we're in the 2nd quarter of 2015), but I'd like it to yield 2015.2, hence the importance of the with_year = TRUE option.
Obviously, I can overcome this by using paste to bind together the year and the output of quarter(Date), but I'd prefer to avoid that work-around.
An object name in a package namespace is masked when a new object is defined with the same name. This can be done by the user assigning the name, or by attaching another package that has an object of the same name.
data.table and lubridate have overlapping function names. If you want the lubridate version to be the default, then the easiest solution is to load data.table first, then load lubridate---thus it will be the data.table versions of these functions that is masked by the "newer" lubridate versions.
library(data.table)
library(lubridate)
Otherwise, the solution is to use :: (as in package::function) to fully specify which version of the function you want to use, for example:
lubridate::quarter(Date, with_year = T)
Another option, which involves a little less typing but is perhaps a little less clear as well, would be to alias the lubridate functions you want in the global environment at the start of your script.
quarter = lubridate::quarter
Any use of quarter() later in the script will use the lubridate version of the function.
Yet another option is the conflicted package, which provides a system for preferring a function from one package. It is a bit more intense and intentional, you should definitely read the documentation before using it, but your script might include something like this:
library(conflicted)
conflict_prefer("quarter", "lubridate")
The package conflicted provides various alternatives and is a good practice to use it while loading libraries to be clear on the masking.
https://github.com/r-lib/conflicted

How to use require(googlesheets) properly?

I recently downloaded googlesheets via
devtools::install_github("jennybc/googlesheets")
and experience some difficulties. When running the script as mentioned in
https://github.com/jennybc/googlesheets I get always:
Error: could not find function "%>%"
How can I solve that problem?
Reproducible example:
Download:
devtools::install_github("jennybc/googlesheets")
require(googlesheets)
Data:
gap_key <- "1HT5B8SgkKqHdqHJmn5xiuaC04Ngb7dG9Tv94004vezA"
copy_ss(key = gap_key, to = "Gapminder")
gap <- register_ss("Gapminder")
Error occurs:
oceania_csv <- gap %>% get_via_csv(ws = "Oceania")
Load the dplyr package first, which provides the %>% operator. This is noted here in the README you link to (suppressMessages is optional):
googlesheets is designed for use with the %>% pipe operator and, to a lesser extent, the data-wrangling mentality of dplyr. The examples here use both, but we'll soon develop a vignette that shows usage with plain vanilla R. googlesheets uses dplyr internally but does not require the user to do so.
library("googlesheets")
suppressMessages(library("dplyr"))
You can install dplyr with
install.packages("dplyr")
See here for more about the pipe operator (%>%).

Resources