Access, Update and Run an R script from another R script - r

I would like to access, update and run an R script from another R script. Specifically, I would like to create a loop that reads in the lines of another R script in order to update some variable names by adding increments of 1 to each of them. Once the variables have been updated, I would like to run the R script.
Currently my code for this is as follows:
for (i in 1:n) {
x <- readLines("Mscript0.R")
y <- gsub(paste0("Mtrain",i),paste0("Mtrain",i+1), x)
cat(y, file="Mscript0.R", sep="\n")
source("Mscript0.R")
}
Please note that the string "Mtrain" in the source script takes on various different forms such as:
Mtrain4 <- read.csv("Mtrain4.csv",header=T,sep=",")
s <- Mtrain4$Weight
Any Ideas?
Thanks

Related

Download multiple files using loop (and a variable) in R

I need to download a lot of files from an "HTML" link using R.
The links look like:
http://bioinf-applied.charite.de/supernatural_new/src/download_mol.php?sn_id=SN00000001
with the number after id= incrementing for each subsequent file. I want to download the first 1000 files, from: ...id=SN00000001 to ...id=SN00001000
I´m trying to use a loop with a variable to download all those files, but I have no idea how to construct this code in R.
Something like this:
for(i in 1:1000){
x <- sprintf("%08d", i)
myPath <- paste0("http://bioinf-applied.charite.de/supernatural_new/src/download_mol.php?sn_id=SN", x)
download.file(myPath, paste0("SN", x, ".mol"))
}

Open a R script inside another R script [Tableau case]

Just a question... supposing I have the following R file, called script.R:
calculation1 <- function(){
# multiple calculations goes here.
}
calculation2 <- function(){
# multiple calculations goes here.
}
And I want to open this file on another file in order to use calculation1 and calculation2 multiple times in different cases.
Is that possible? I mean, something like the following.
calculation_script <- Some way, function or method that instantiate script.R in this variable.
calculation_script.calculation1()
calculation_script.calculation2()
I need to do this because I need to open script.R file in a Tableau Calculated field.

Run shell script in shiny

I think there is a problem in my shiny script, with executing a shell comand and I was wondering if there maybe is a way to do this command within shiny.
Outside of shiny my code functions.
#Function to calculate maxentscore
calculate_maxent <- function(seq_vector,maxent_type){
#Save working directory from before
Current_wkdir <- getwd()
#First, change working directory, to perl script location
setwd("S:/Arbeitsgruppen VIRO/AG_Sch/Johannes Ptok_JoPt/Rpackages/rnaSEQ/data/maxent")
#Create new text file with the sequences saved
cat(seq_vector,file="Input_sequences",sep="\n",append=TRUE)
#Execute the respective Perl script with the respective Sequence file
if(maxent_type == 3) cmd <- paste("score3.pl", "Input_sequences")
if(maxent_type == 5) cmd <- paste("score5.pl", "Input_sequences")
#Save the calculated Maxent score file
x <- shell(cmd,intern=TRUE)
#Reset the working directory
setwd(Current_wkdir)
#Substracting the Scores from the Maxent Score Table
x <- substr(x,(regexpr("\t",x)[[1]]+1),nchar(x))
#Returning the maxent table
return(x)
}
So basically I just try to execute following code:
shell("score5.pl Input_sequences")
This does seem to not be possible that way within shiny
I do not know the shell command, but executing shell commands is possible via system(). It even uses the current working directory set by R.
So you might try:
x <- system(cmd, intern=True)

How to write result of a program into a .csv file

I have written a R script for binning on the specific parameters of several .csv files in the same folder. I used the smbinning package. When I execute the script, it produces detailed results. I do not need all of them. I want to take a specific part of the results and write into a .csv file automatically. Can someone tell me how can I do this? My R script, details results, and wanted parts of result is as follows
My R script is as follows:
library(smbinning) 
files <- list.files(pattern = "0.csv")
cutpoint <- rep(0,length(files))
for(i in 1:length(files)){
data <- read.csv(files[i],header=T)
df.train <- data.frame(data)
df.train_amp <-rbind(df.train)
cutpoint[i] <- smbinning(df=df.train_amp, y="cvflg",x="dwell")
}
result <- cbind(files,cutpoint)
write.csv(result,"result_dwell.csv")
You can use View(result) to see if the variable contains exactly what your require. Else there is something wrong in your logic.
There is function sink in R which writes the output of a program to a file.
https://stat.ethz.ch/R-manual/R-devel/library/base/html/sink.html

Loop to import arguments into R

I am new to R and I am trying to have a script get arguments from a file. I am using the following code within my R script:
args <- commandArgs(TRUE)
covr <- args[1]
rpts <- args[2]
The arguments will come from a parameters.tsv which will have two fields, one for each argument.
I want to run the R script with the parameters given in a line from parameters.tsv until all lines have been used to run the R script.
The end result will be qsub'ing a bash script to run each line into the R script.
This is what I came up with:
#!/bin/bash
cat parameters.tsv | while read v1 v2; do RScript --slave ‘--args $v1 $v2’ myscript.R; done
It's currently terminating almost immediately after i submit it and i don't understand why.
Any help is greatly appreciated since i am very new to this and anything i read prior did not explain in enough detail to grasp.
How about something like:
var_df <- read.csv([your_file_here]) # or read table with correct specs
for (i in 1:dim(var_df)[1]) { # vectorise for speed; doing it with loops to
# make this clearer
this_var_a <- var_df[i,1]
this_var_b <- var_df[i,2]
source([Rscript file here], local=TRUE) #set local=T as otherwise the vars
# will not be visible to the script's operations
}

Resources