R Script running successfully on local machine, not on EC2 instance

Viewed 322

I have an R script (an R plumber API) that I have deployed to an EC2 instance and managing with pm2, and I am running into a struggling issue. I have pinpointed the exact location of the error, and am hoping to understand this error a bit better.

When I run the script on my local machine (RStudio on my Mac) it works okay. When I run the script using Rscript myrfile.R from the EC2 instance command line, it breaks.

I have pinpointed that the line of code that breaks the script the on EC2 instance, as well as its error, are:

my_df <- my_df %>%
  dplyr::mutate(AwayScore = ifelse(dplyr::row_number() == 1, 0, AwayScore),
                HomeScore = ifelse(dplyr::row_number() == 1, 0, HomeScore)) 

# with the following error
<Rcpp::eval_error in mutate_impl(.data, dots): Evaluation error: argument "x" is missing, with no default.>

I am 100% sure that dplyr is installed on the EC2 instance, since my script uses it throughout. I am also 100% sure that the my_df dataframe here has the columns AwayScore and homeScore, and also that my_df doesnt have any other issues.

I am left to assume that this error is specifically due to the dplyr::row_number() function, which the EC2 instance does not seem to be able to handle, although I am not positive on this.

Any thoughts / help / things I should try / etc. would be greatly appreciated on this, thanks!!

2 Answers

It was easy enough for me to simply change my code to the following:

  if(is.na(my_df$AwayScore[1])) { my_df$AwayScore[1] = 0 }
  if(is.na(my_df$HomeScore[1])) { my_df$HomeScore[1] = 0 }

... so I will likely not waste too much more time trying to debug this.

While I appreciate you have avoided the problem by not requiring the library, at some point you may find you want to run codes in a similar way where loading a library will be necessary.

I ran into a similar problem using R script. I found it could not find the libraries I had installed. It is possible to use R.exe instead of Rscript.exe, but this causes other headaches. I found that the environment when using Rscript doesn't contain the R_LIBS_USER path

If you append the following code to the top of your R script it should work

p <- "\directory path of local R packages"

.libPaths(c(p,.libPaths()))

putting the folder path to where your libraries are found on the computer. This is the path that would be returned by Sys.getenv("R_LIBS_USER") if running R in the GUI

Related