Debugging script, issue in reading in files at start - r

I am trying to debug a .R script, going through line by line to see where the bug is.
I am at the part where I input the datasets but am having trouble understanding what the code below is doing.
gtfile =grep("gt=",commandArgs())
if (length(gtfile)==0) { stop ("specify genotype file (gt=filename)\nRun script without arguments to see all options\n") }
gtfile=sub("gt=","", commandArgs()[gtfile])
Should I just be able to read in my gt file name below:
gt=read.table("~/clean.tab")
If I read the gt file in like above, I should then modify the mode to expect the gt object, like below?
gtfile =grep("gt",commandArgs())
if (length(gtfile)==0) { stop ("specify genotype file (gt=filename)\nRun script without arguments to see all options\n") }
gtfile=sub("gt","", commandArgs()[gtfile])
I get the following error, which makes me think it is not reading in the file gt as I hoped.
Error: specify genotype file (gt=filename)
Run script without arguments to see all options
Any advice on what the first block of code is doing would be appreciated.
I have modified the code block and read in the file.
I was expecting the gt file to be read in, then run the code block to grep the needed parts within the file.

Related

Set Rplots.pdf generation location

I am currently writing a bash file run.sh that runs some python code, followed by some code in an .R file. I call the file, data_analysis.r, using Rscript data_analysis.r inside run.sh.
Each time I run the script, it generates an Rplots.pdf file, which I am okay with, but I would like to set the file to generate at a custom location.
For now, I can simply add a line in run.sh that renames and moves the Rplots.pdf file to the intended location, but I'm not quite happy with this solution.
Does anyone know how to set a custom location for the Rplots.pdf file generated by Rscript?
There should be a line in data_analysis.r which says
pdf(file="RPlots.pdf")
You can edit this line to location you want and hard code it.
pdf(file="/mylocation/RPlots.pdf")

R Shiny: 'error cannot open the connection'

so I have a Shiny app where I'm trying to read in a user-identified input file.
Towards that end, my ui.R has the line fileInput("predictor2", label = "Predictor Values") and I try to read the file using the line predictor <- read.delim("input$predictor2") in my server.R file.
However, I get a message saying Error: Cannot open the connection. If I don't try to read in the file and use another matrix of values, the code works fine. Any advice for how to fix this problem or more detail that would be useful?
You code is looking for a file with the literal name input$predictor2 which presumably does not exist. You first need to remove the quotes from around it, then add which column of the return actually has the path to the data, e.g.:
read.delim(input$predictor2$datapath)
See the help for fileInput for an example that checks to make sure something has been uploaded first.

Calling Skim from inside R

I'm making a simple line in r to automatically open my generated plots.
I output the plots to a file called "plots.pdf" in the same directory as my r file, and at the end i use this two lines to try to open it:
dir <- paste("/Applications/Skim.app/Contents/MacOS/Skim ",getwd(),"/plots.pdf",sep="")
system(dir)
Basically, dir concatenates the full path of the skim app and the full path of the generated plot.
If i run the string stored at dir in a shell it works perfect, it opens the pdf file in Skim, but when i run it with system() from inside R it doesn't work (Skim says 'The document “plots.pdf” could not be opened.').
I believe this is a very little mistake somewhere in the syntax regarding the absolute/relative paths, but haven't managed to find it... Any advice is welcome! (Or a better way to achieve the same)
I found a way to bypass that problem, i just changed the path to Skim for the 'open' command and i let the system to assign the default app for pdf viewing. So:
dir <- paste("open ",getwd(),"/plots.pdf",sep="")
And it works.

How to write an R program that copies its source code to a file?

I'm writing an R script whose contents can change from time to time. It would be really helpful if I could insert a command that would copy the current contents of the script to a file, so I can go back later and see exactly what commands I executed during that run of the code.
How can I do this?
You can do this with the teaching demos package:
install.packages("TeachingDemos")
library(TeachingDemos)
#Will write to a file in the working directory
txtStart("captureCode.txt")
#This comment will not appear in the file, all commands and output will
Sys.Date()
#This command ends writing to the file
txtStop()
Source

Print plain text of help file to console [duplicate]

I'd like to be able to write the contents of a help file in R to a file from within R.
The following works from the command-line:
R --slave -e 'library(MASS); help(survey)' > survey.txt
This command writes the help file for the survey data file
--slave hides both the initial prompt and commands entered from the
resulting output
-e '...' sends the command to R
> survey.txt writes the output of R to the file survey.txt
However, this does not seem to work:
library(MASS)
sink("survey.txt")
help(survey)
sink()
How can I save the contents of a help file to a file from within R?
Looks like the two functions you would need are tools:::Rd2txt and utils:::.getHelpFile. This prints the help file to the console, but you may need to fiddle with the arguments to get it to write to a file in the way you want.
For example:
hs <- help(survey)
tools:::Rd2txt(utils:::.getHelpFile(as.character(hs)))
Since these functions aren't currently exported, I would not recommend you rely on them for any production code. It would be better to use them as a guide to create your own stable implementation.
While Joshua's instructions work perfectly, I stumbled upon another strategy for saving an R helpfile; So I thought I'd share it. It works on my computer (Ubuntu) where less is the R pager. It essentially just involves saving the file from within less.
help(survey)
Then follow these instructions to save less buffer to file
i.e., type g|$tee survey.txt
g goes to the top of the less buffer if you aren't already there
| pipes text between the range starting at current mark
and ending at $ which indicates the end of the buffer
to the shell command tee which allows standard out to be sent to a file

Resources