Dplyr's select function throws an error in Rscript - r

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.

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!

Running pivot_longer() while knit Rmd always reports an error

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.

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

Object: "DF" not found when knitting rmd

I get this error when knitting any rmd. file using this data set (Aviste_Affect_Data).(The problem also happens with other data frames when I tried to reproduce the problem) All of the code within the files run and the data set itself appears to be fine (it was imported from SPSS). The error occurs on the first line of code in each rmd. file and if I delete that line the error just happens again on the next line of code and so on.
This is the first line of code that the error occurs on, which again runs fine (the package dplyr is being used):
Data_na2014<-Aviste_Affect_Data %>% filter(!Year==14)
Error Message:
Error in eval(las, parent, parent) : object 'Aviste_Affect_Data' not found Calls: ...withCallingHandlers -> withVisible -> eval -> eval -> $>$ ->eval ->eval Execution halted
If I try different code as the first code, the error is mostly the same:
Aviste_Affect_Data$Worry<-as.numeric(Aviste_Affect_Data$x78)
Error Message:
Error in eval(expo,envir,enclos) : object 'Aviste_Affect_Data' not found Calls: ...handle-> withCallingHandlers -> withVisible -> eval -> eval Execution halted

Parsing a command line variable as data frame column

I am trying to run a R script file from command line. My code on the command line is as below:
Rscript command_test.R name_of-data_frame col_name
In my R script file, command_test.R, code is as below:
library(dplyr)
args <- commandArgs(trailingOnly = TRUE)
grpd_by_variable<-args[1]%>%
group_by(args[2])%>%
mutate(TOTAL=n())%>%
filter(row_number()==1)
grpd_by_variable
I would want to pass both the data frame name i.e. args[1] and as well as column from the data frame i.e. args[2] from the command line. However, when I run this script, I am seeing the following error.
Error in UseMethod("group_by_") :
no applicable method for 'group_by_' applied to an object of class "character"
Calls: %>% ... _fseq -> freduce -> <Anonymous> -> group_by -> group_by_
Execution halted
Any idea on how to work around this issue please? Seems like R is interpreting the command line parameter as string and hence the issue.
Many thanks in advance.

Resources