How do I restart R in linux using a line of code? - r

I need to restart an R session running in linux using a line of code. When the R session restarts I need it to load an existing R script and for the new R session to be clear of previously loaded packages, variables etc.
I've managed to make this work from within an R terminal in Windows using the following code:
library(startup)
restart(args = c('source("existing_code.R")'))
However when the same code runs in a Linux environment I get the following error:
sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `'/opt/R/3.6.3/lib/R/bin/exec/R' source("
existing_code.R")'

Related

R: "internet routines cannot be loaded" when starting from RStudio

I am running Red Hat Enterprise Linux (RHEL) 8.5 with Linux kernel 4.18 and Gnome 3.32.2. In this system, I've got R 4.1.2 compiled with the tool asdf with shared libraries enabled. On top of that, I installed RStudio 2021.09.01-372 from an RPM from the official RStudio website.
When I start Rstudio, the first line of output after the usual R startup is an error:
Error in tools::startDynamicHelp() : internet routines cannot be loaded
I am unable to figure out what's causing this error, and with it I can't run things like refresh CRAN or update packages. But if I start a pure R session from the terminal (instead of Rstudio) this error does not occur.
Some things I tried:
Install the krb5 and libssh2 packages on my host system: Didn't help.
Starting a "pure" R session (both with and without the --vanilla argument) from the Terminal tab within Rstudio also gives this error. If I try to run update.packages() from this session, it pops up a window to select a CRAN mirror then fails with the following:
Warning: failed to download mirrors file (internet routines cannot be loaded); using local file '/home/[my username]/.asdf/installs/R/4.1.2/lib64/R/doc/CRAN_mirrors.csv'
Warning: unable to access index for repository https://cloud.r-project.org/src/contrib:
internet routines cannot be loaded
Warning message:
In download.file(url, destfile = f, quiet = TRUE) :
unable to load shared object '/home/penyuan/.asdf/installs/R/4.1.2/lib64/R/modules//internet.so':
/lib64/libssh.so.4: undefined symbol: EVP_KDF_ctrl, version OPENSSL_1_1_1b
But like I said, the strange thing is if I start an R session outside of Rstudio, these errors don't happen.
Within RStudio, the only workaround I can find is to run this command upon startup (suggested in this thread):
options(download.file.method="wget")
Once this is done, everything else seems to work, such as package updates.
However, I don't want to manually do this every time I start RStudio. So I tried to put it into ~/.Rprofile including a test print() as follows:
print("This is `~/.Rprofile`")
options(download.file.method="wget")
When I open RStudio, I can see the output from the print() call, but the options() command is not run because the original error shows up again. I still have to manually enter options(download.file.method="wget") every time.
I also tried to fold everything into a .First function in ~/.Rprofile as follows:
.First <- function() {
options(download.file.method="wget")
print("This is the `.First` function in `~/.Rprofile`")
}
Unfortunately, same result as before: print()'s output is seen, but options() is not run.
I also made sure that my ~/.Rprofile includes a trailing newline as discussed here. But this didn't help.
The above are the steps I've tried so far.
Why does this error only occur when running RStudio or a terminal within Rstudio? Why doesn't it happen if I start R from a terminal outside of Rstudio?
Is there a way to solve the problem so that the error doesn't happen in the first place? If it can't be solved, how do I set up my ~/.Rprofile so that options(download.file.method="wget") will be run?
Thank you.

How to invoke a system command in R on shared computing cluster?

I am attempting to call a system command from within an R script. The system command I want to call is a name of a software that I load as a module on a shared computing cluster (using singularity). The problem I am having is that the software is not able to run when I use the system command.
system_trial.R) has only one line:
system('STAR')
hostname[1] Rscript system_trial.R
sh: STAR: command not found
Warning message:
In system("STAR") : error in running command
Of course, the software works if I call it from the shell directly.
hostname[2] STAR
Usage: STAR [options]... --genomeDir /path/to/genome/index/ --readFilesIn R1.fq R2.fq
Spliced Transcripts Alignment to a Reference (c) Alexander Dobin, 2009-2020
If I run which STAR, I get singularity exec /apps/singularity-3/star/star-2.7.5a--0.sif STAR $#
Replacing system('STAR') with system('singularity exec /apps/singularity-3/star/star-2.7.5a--0.sif STAR $#') actually executes the software.
Replacing system('STAR') with system('which STAR'), returns which: no STAR in (/bin:etc...)
Using system2('STAR') gives sh: STAR: command not found.
I would like to simply use system('STAR'). How can I achieve this?
Related post without an answer: R: calling a system command

R system call returns error

I am trying to execute a python script from within R, with the following command:
system("python ~/speedtest-cli/speedtest-cli --simple")
The error I get is:
python: can't open file '/home/speedtest-cli/speedtest-cli': [Errno 2] No such file or directory
The same command works just fine on the terminal. Am I using the command wrong? Any help is appreciated.

Executing R script within R 3.3.1 installed on windows

Sorry for a basic question. I'm trying run a R script called cuffdiff_gtf_attributes (please find it at enter link description here in R 3.3.1 installed on the Windows 7. The script is started with the below line:
#!/usr/bin/env Rscript
When I type cuffdiff_gtf_attributes in R, it says Error: object 'cuffdiff_gtf_attributes' not found. Also, I tried Rscript cuffdiff_gtf_attributes that returned me: Error: unexpected symbol in "Rscript cuffdiff_gtf_attributes".
Moreover, I tried source('cuffdiff_gtf_attributes.R')that seems to work and returned the usage of the script as bellow
Error:
usage: cuffdiff_gtf_attributes --input=<inputGTF> [--output=outputGTF] | --help
But, when I add the arguments as source('cuffdiff_gtf_attributes.R') --input=file.gtf, it says that: Error: object 'file.gtf' not found. I also tried this command as source('cuffdiff_gtf_attributes.R') --input file.gtf, it says that Error: unexpected symbol in "source('cuffdiff_gtf_attributes.R') --input file.gtf"
Sorry, I couldn't post a sample GTF file, you can find a short sample of it at enter link description here
Everything is the current path. Could you please help me out to execute the script?
Thanks in advance
This is a script file. You should run using Rscript instead for Rgui.exe. From a command prompt, navigate to the directory where file.gtf is and run:
"%Programfiles%\R\R-3.3.3\bin\Rscript" cuffdiff_gtf_attributes.R --input=file.gtf

Write errors to file

Is there a way to write my R errors to a file? I run R on bash via:
R --vanilla < myprogram > myprogram.out &
When my program encounters an error (not a syntax error...like an illegal replacement or something) it stops but the error line isn't written to the output file and I don't know what the program was and a lot of the time I log out from the server while it runs.
Thanks,
Josh
Use the R CMD BATCH <infile> <outfile> syntax instead.

Resources