Unable to use has_goog_key() in R - r

Below simple lines before start to use 'ggmap'
install.packages("devtools")
devtools::install_github("dkahle/ggmap")
library(devtools)
library(dplyr)
library(purrr)
library(ggmap)
library(gmapsdistance)
api_key = Sys.getenv("A_GOOGLE_API_KEY")
register_google(key = api_key)
set.api.key(api_key)
has_goog_key()
It returns:
Error in has_goog_key() : could not find function "has_goog_key"
What went wrong, and how can I check if the given Goole API key is valid?

has_goog_key is a function in a different ggmap package, which you can find via devtools::install_github("fresques/ggmap"). In the package you're using, the equivalent function would be has_google_key.

Related

I get an error when I read a Github code in R

I Found this code on Github, but when I run it:
Tracking <- ReadDLCDataFromCSV(file = "example/OFT/DLC_Data/OFT_3.csv", fps = 25)'
I get this error:
Error in last(strsplit(file, split = "/")[[1]]) :
could not find function "last"
Thank you
In the source code of the function ReadDLCDataFromCSV(), from the GitHub link you linked, there is a function called last that is called. Line 34:
out$filename <- last(strsplit(file,split = "/")[[1]])
The error that you get is because the function last is not found. The function is probably from the data.table package. So install the data.table package with:
install.packages("data.table")
and then load the package with:
library("data.table")
The ReadDLCDataFromCSV() function should then work. The package data.table is noted as a dependency to the DLCAnalyzer collection as noted in the README.md.

Unable to find an inherited method for function ‘select’ for signature ‘"data.frame"’

I'm trying to select columns from a data frame by the following code.
library(dplyr)
dv %>% select(LGA)
select(dv, LGA)
Both of them will fail with the error
Unable to find an inherited method for function ‘select’ for signature ‘"data.frame"’
But the following code will be fine.
dplyr::select(dv, LGA)
Is this a function confliction in packages?
All libraries imported are as the following.
library(jsonlite)
library(geojsonio)
library(dplyr)
library(ggmap)
library(geojson)
library(leaflet)
library(mapview)
library(RColorBrewer)
library(scales)
I'm new to R, so super confused how you guys deal with problems like this?
There's a great package that helps with package conflicts called conflicted.
If you type search() into your console, you'll see an ordered vector of packages called the "search list". When you call select, R searches through this "search path" and matches the first function called select. When you call dplyr::select you're calling it directly from the namespace dplyr, so the function works as expected.
Here's an example using conflicted. We'll load up raster and dplyr, which both have a select function.
library(dplyr)
library(raster)
library(conflicted)
d <- data.frame(a = 1:10, b = 1:10)
Now when we call select, we're prompted with the exact conflict:
> select(d, a)
Error: [conflicted] `select` found in 2 packages.
Either pick the one you want with `::`
* raster::select
* dplyr::select
Or declare a preference with `conflict_prefer()`
* conflict_prefer("select", "raster")
* conflict_prefer("select", "dplyr")
This function dplyr::select solved my problem.

Error in get_sentiments function

Has anyone used 'tidytextmining' for sentiment analysis in R?
Tidytextmining
I am using R V 3.4.1 and I am getting the following error for this piece of code.
library(tidytext)
library(dplyr)
get_sentiments("afinn")
Error - Error in get_sentiments("afinn") :
could not find function "get_sentiments"
I have the right package installed and the library reference. What am I missing?
I tried your code and it's working just fine. Are you sure you have the right library reference? I would double-check!

Cannot run map_data("state")

I am trying to work on a US map in R. I have done it a lot of times but this time it gives me this error when I try to load:
us<- map_data("state")
Error in .C(C_map_type, as.character(mapbase), integer(1)) :
Incorrect number of arguments (2), expecting 0 for ''
I have ggmap and ggplot2 libraries loaded. Where am I going wrong?
You need the 'maps' package along with ggmap.
library(maps)
library(ggmap)
us<- map_data("state")
This should work
It looks like there are bugs in tidyverse which interferes with ggplot2 maps functionality. See this related question.
This works in a clean, freshly-restarted R session:
us <- ggplot2::map_data("state")
However, this does not:
library(tidyverse)
us2 = ggplot2::map_data("state")

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