Running pivot_longer() while knit Rmd always reports an error - r

I wrote a very simple R code and it also works fine when I run R-chunks direct in Rmd.
library(tidyr)
AirPassengers <- AirPassengers %>%
pivot_longer(-year, names_to = "month", values_to = "passengers")
But as soon as I try to knit this Rmd file, the following error is reported. Why is this and what does it mean to report an error?
Error in UseMethod("pivot_longer") :
no applicable method for 'pivot_longer' applied to an object of class "list"
Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval -> %>% -> pivot_longer
In addition: Warning messages:
1: replacing previous import 'lifecycle::last_warnings' by 'rlang::last_warnings' when loading 'pillar'
2: replacing previous import 'lifecycle::last_warnings' by 'rlang::last_warnings' when loading 'tibble'
3: package 'pammtools' was built under R version 4.0.5
4: In AirPassengers$year <- as.numeric(rownames(AirPassengers)) :
Coercing LHS to a list
Execution halted
Any help would be greatly appreciated!
I googled and didn't find any useful answers.
I'm curious why it works fine in the current chunk and gets the desired result but not in knit.
Also: the line referred to in the error report is only the code to load the package tidyr.

Related

Errors During R Notebook Compilation

How to resolve the following errors occurring when compiling/knitting R Notebook into a document? All the required packages are successfully loaded, still, the issue persists.
I restarted RStudio several times. Loaded all the necessary packages. Explored several websites for the resolution, but in vain.
Code:
# pipe operator
x<-5:15
mean(x)
x%>%mean
sum(z,na.rm=TRUE)
z%>%sum(na.rm=TRUE)
sum(is.na(z))
z%>%is.na%>%sum
mean(is.na(z))
z%>%is.na%>%mean
Output:
|................................... | 67% (unnamed-chunk-1)
processing file: qwerty.Rmd
Quitting from lines 13-124 (qwerty.Rmd)
Error in x %>% mean : could not find function "%>%"
Calls: <Anonymous> ... withVisible -> eval_with_user_handlers -> eval -> eval
Execution halted
Code:
data(diamonds)
View(diamonds)
# base histogram(s)
hist(x=diamonds$carat,main="FREQUENCY vs. CARAT",xlab="carat",ylab="frequency")
# base scatter plot(s)
(plot(x=diamonds$carat,y=diamonds$price,xlab="carat",ylab="price"))
Output:
|................................... | 67% (unnamed-chunk-1)
processing file: qwerty.Rmd
Quitting from lines 13-24 (qwerty.Rmd)
Error in as.data.frame(x) : object 'diamonds' not found
Calls: <Anonymous> ... eval_with_user_handlers -> eval -> eval -> View -> as.data.frame
Execution halted
The error is "Error in x %>% mean : could not find function "%>%""
Try to add library(tidyverse) before this code (you need to load all the required packages in the .Rmd file so it can knit)
Also, you will be able to use the diamonds dataset
Hope this can help you!

Error in R Markdown: could not find function "split_chain"

Hello I am using R markdown and have ran into trouble.
When I try to knit this section of my document (the "example" variable is a data frame with a column called "text" which contains strings):
library(qdap)
frequent_terms <- freq_terms(example$text, 4)
frequent_terms
I get this error message:
Error in split_chain(match.callQ), env = env) :
could not find function "split_chain"
Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval -> %>%
I have updated Rstudio and the relevant packages(such as magrittr) but I am still running into this issue, one I have never encountered.
How do I fix this error, or how to I interpret the error message, I am at a loss here. Thank you.
According to this issue, your problem should be solved if you add qdap first, and magrittr / tidyverse afterward because gdap uses an old version of the pipe and masks the most recent magrittr version.
However, it is not entirely clear since we do not have a complete reproducible example

Dplyr's select function throws an error in Rscript

I was trying to run Rscript dosth.R in the command line. In the script, I used select function from dplyr package. I got the following error message:
Error in UseMethod("select_") :
no applicable method for 'select_' applied to an object of class "factor"
Calls: %>% ... withVisible -> -> select -> select.default -> select_
Execution halted
However, I could successfully run the main function inside this "dosth.R" script in RStudio.
I want to solve this problem because eventually I would like to put all the codes in a script which can be run in the command line.
I wonder whether you have met this problem and would greatly appreciate your kind help.
The problem is that somewhere in your code you redifined data.frame object into factor. The simulation below throws exactly the same error as you defined:
library(dplyr)
data(iris)
iris <- factor(1:10)
iris %>% select(Sepal.Width)
Error in UseMethod("select_") : no applicable method for 'select_'
applied to an object of class "factor" Calls: %>% ... withVisible ->
-> select -> select.default -> select_ Execution halted
So please check and remove data.frame -> factor transformation from dosth.R file.

Trying to Knit but can't because read excel function is not being recognized in R

My whole program is working but when I try to knit to a document the same error is being thrown.
Quitting from lines 3-78 (Untitled.spin.Rmd)
Error in eval(expr, envir, enclos) : could not find function "read_excel"
Calls: <Anonymous> ... handle -> withCallingHandlers -> withVisible -> eval -> eval
Execution halted
I don't under stand why. Where are the first 5 lines of my code.
Dataset_Stats1Project <- read_excel(Users/jamiestokes16/Desktop/Dataset_WomeninGov5.xlsx)
#Descriptive stats for important variables
summary(Dataset_WomeninGov$Wcongress)
summary(Dataset_WomeninGov$Wgov)
summary(Dataset_WomeninGov$Wleg)
Any help would be appreciated.
When you knit a document, knitr essentially creates a new R session for the code to run in. So any packages loaded session will not be accessible unless you load them directly within the .Rmd file.
I always find it easiest to include a chunk at the start of the document loading any packages used:
```{r LoadPackages, include = FALSE}
library(readlx)
# add other packages here
```

Cryptic dplyr error when trying to knit html

I have a snippet that depends on a package that I wrote. I'm trying to knit a markdown document and am getting an error:
Error in eval(expr, envir, enclos) : not a vector
Calls: <Anonymous> ... freduce -> <Anonymous> -> bind_rows -> rbind_all -> .Call
The same snippet runs in the console, and the previous line is a lapply that returns a list of data frames. There's confidential data and it'd take a while to come up with a reproducible example so I thought I'd see if others have any tips.

Resources