knitr package loading error stringr - r

I get the following error:
1Error in library(stringr) :
Package 'stringr' version 1.2.0 cannot be unloaded
when loading the following packages in my knitr document:
library(checkpoint)
checkpoint("2017-01-01")
library(stringr)
library(plyr)
library(tidyr)
library(dplyr)
library(knitr)
library(readr)
library(readxl)
library(ggplot2)
library(scales)
library(ggthemes)
library(lubridate)
library(xtable)
library(zoo)
library(gridExtra)
when I remove stringr then I get the error:
Error in mutate_impl(.data, dots) : could not find function "str_sub"
...any ideas? I am using Mac OSX Mavericks. I copied the files over from a Windows 7 (where it worked) and I'm assuming that has something to do with it.
Thank you in advance!

There is a conflict due to the stringr package being newer (2017-02-18) than your checkpoint date of 2017-01-01.
You could clear the conflict by moving the date you use to something later, i.e.
checkpoint("2017-02-18") # or later
Alternatively, if you find you have mixed dependencies that rely on packages that were not all current on the same date, I would recommend having a look at packrat (link), which is designed for that purpose.

Related

Error in library(sp) : there is no package called 'sp'

The sp package was installed using RStudio (through CRAN) but when R evaluates the code:
library(sp)
It throws an error
Error in library(sp) : there is no package called 'sp'
I noticed that it works for R versions 4.* but does not work for versions 3.*. What could be causing this?
If you first time to use 'sp', install.... :)
install.packages("sp")
library(sp)

could not find function "unnest_tokens"

I'm trying to split a column into tokens using the tokenizers package but I keep receiving an error: could not find function "unnest_tokens". I am using R 3.5.3 and have installed and reinstalled dplyr, tidytext, tidyverse, tokenizers, tidyr, but still keep receiving the error.
I have also quit and restarted R and RStudio.
comments_tidy <- comments %>%
unnest_tokens(word, txt) %>% #Break the comments into individual words
filter(!word %in% undesirable_words) %>% #Remove undesirables
anti_join(stop_words) #Data provided by the tidytext package
I receive the following:
Error in unnest_tokens(., word, txt) :
could not find function "unnest_tokens"
As mentioned in the comments, you may want to expand your code with library(x) statements. In addition, make sure that all the packages and their dependencies are installed. The following snippet would look up a given package (in this case dplyr) and install it if needed.
if ("dplyr" %in% installed.packages()[, "Package"]){
cat("'dplyr' is installed.")
} else {
install.packages("dplyr",dependencies=T)
}
library(dplyr)
The command installed.packages()[, "Package"])? gives you a list of all installed packages, that is a nice trick for debugging all kind of Function foo not found errors.
Just be sure to run this first
install.packages("tidytext")
library(tidytext)
You only need to run the first line once - it will install the package.
You need to run the second line in each new R session, since that will load the package.
Calling library (tidytext) and tidytext::unnest_tokens() solved the problem for me.

R Error in today() : could not find function "today"

I have an R script that just prints todays date. It runs just fine in Rstudio but when set as a task within a batch file it produces the following error
Warning message:
package 'dplyr' was built under R version 3.4.4
Loading required package: NLP
Warning message:
package 'tm' was built under R version 3.4.4
Error in today() : could not find function "today"
Execution halted
Here is the script:
library(rvest)
library(dplyr)
library(tm)
yesterday <- today()
yesterday <- gsub("-", "", yesterday, fixed=TRUE)
print(yesterday)
Batch File:
"C:\Program Files\R\R-3.4.2\bin\R.exe" CMD BATCH --vanilla --slave "C:\Users\mike\Desktop\Make_Task\TEST_YESTERDAY.R"
timeout /t 5
When you don't know where an R function comes from, I'd recommend searching rdocumentation.org for the name of the function. In these results, you can see that today is from the lubridate package.
Personally, I would recommend removing the external dependency by using the built-in Sys.Date() instead. But adding library(lubridate) to the top of your script should also work (assuming lubridate is installed).
The today() function is from the lubridate package. You probably loaded the package manually inside RStudio, so it's not in your code. Just add library(lubridate) to the beginning of your script and it should be fine.
Alternatively, you could also use Sys.Date() from r-base
Try with lubridate first and then try today() or now() you will get the answer.
install.packages("lubridate")
library("lubridate")
today()
[1] "2021-04-26"
now()
[1] "2021-04-26 07:09:37 UTC"

tidyverse interfering with ggplot2? cannot access map_data

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

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