Parsing a command line variable as data frame column - r

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.

Related

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

Code runs fine in R but Knit shows an error

I'm having a problem with the following code in an Rmd file, the problem is that if I run the commands in the console, the code runs perfectly fine, and also there is no mistakes in the syntax, I have change several styles but I'm still receiving the same error when I knit the file.
Raw_Data96_11$EVTYPE <- as.character(Raw_Data96_11$EVTYPE)
List_Events <- count(Raw_Data96_11, vars = "EVTYPE")
List_Events <- arrange(List_Events,desc(n))
List_Events <- mutate(List_Events, percentage = trunc((100*(n/653530))))
Top_10_Events <- List_Events[1:10,2:3]
I had to change the syntax for count, when I run the command in the console, I did not had to use vars, but now I cannot run the command arrange, this is the error :
Quitting from lines 98-117 (PA_2_Reproducible_Research.Rmd)
Error in order(List_Events$n) : argument 1 is not a vector
Calls: ... withVisible -> eval -> eval -> arrange -> eval -> eval -> order
Execution halted
I have included the libraries in that chunk but I'm still receiving an error, does anyone know what is going, is it the syntax or what else do I need to do?

Error in UseMethod("select_") in Blogdown

I am using Blogdown to create a new post and I am getting the following error when trying to preview.
The code works well in my Rmarkdown file but I cannot update it to my blog. Do anyone know where the problem is?
Quitting from lines 36-47
Error in UseMethod("select_") :
no applicable method for 'select_' applied to an object of class "function"
Calls: local ... freduce -> -> select -> select.default -> select_
Execution halted
Here is my code in lines 36-47;
library(corrplot)
library(RColorBrewer)
library(tidyverse)
corrplot(cor(df %>% select(Sales, Customers, Store,
Open, SchoolHoliday,
DayOfWeek, month, year,
CompetitionDistance,
Promo, Promo2_active) %>%
filter(!is.na(Sales), !is.na(CompetitionDistance))),
type="upper", order="original",
col=brewer.pal(n=8, name="RdYlBu"))
Thanks a lot.
I think you're getting this error because you don't have an object called df in your global environment. Either your data frame hasn't been created yet or it is called something else. There is a little-known function called df in the stats package, which is on the search path when you start an R session. You can check this by starting a new R session and typing df into the console. You will see the body of the function stats::df.
You are therefore getting the error because you are trying to subset a function, not a data frame. To resolve the error, make sure you create a data frame called df before your call to corrplot

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.

Function to loop through nc_open: Calls: lapply -> FUN -> nc_open r

I'm trying to simplify my scripts, and therefore trying to loop through a set of netcdf-data I have. There are five I have, outside the loop this command works:
data <- nc_open('/specific_1/data.nc')
data_var <- ncvar_get(data, "var")
data_var <- data_var[50:164]
Now, trying to loop through the five sets I tried the following:
dflist <- c("1","2","3","4","5")
lapply(dflist, function(df) {
data <- nc_open(paste("'/specific_",df,"/data.nc',",sep=""))
data_var <- ncvar_get(data, "var")
data_var <- data_var[50:164]
NULL
})
I gives me the error
Error in R_nc4_open: No such file or directory
Error in nc_open(paste("'/specific_", :
Error in nc_open trying to open file '/specific_1/data.nc',
Calls: lapply -> FUN -> nc_open
Execution halted
I mean, it is fairly obvious that r won't find my file. But why? It puts the path together correctly, no (Error in nc_open trying to open file '/specific_1/data.nc')? There probably is an easy solution, but I haven't used r a lot until now so I can't think of one.
I think I see at least one thing where I'm going wrong, or that at least should be improved: Do I need to assign individual names to the commands (e.g. nc_open(paste("'/specific_",df,"/data.nc',",sep="")))? I think it shouldnt solve the problem though, if it worked it would probably just overwrite the data that was read in earlier.

Resources