R: If (file.exists( - r

I am just learning R, so this might be a simple question.
I have a bunch of code that I want to run IF a certain file path that was generated earlier in the code exists. So...
1.A path is created
2.If the path exists, then the rest of the code should run
This is what this portion of the code looks like:
label_file=paste(label_folder,"Files/",as.matrix(babble_data)[n,3],label_extension,sep="")
if (file.exists(label_file){...
It is running without giving me any errors, but it is treating EVERY file path (even the ones that DO exist) like the don't exist.
Is my syntax incorrect? All the examples I can find look like this, so I have no idea why it isn't working as it is supposed to. Any idea? Thanks.

You are missing a closing round bracket in the if statement before the curly bracket. This can have all possible side effects, although a code parsing error is the most probable one.
Additionally, as.matrix(babble_data)[n,3] can produce a vector of results, so paste can produce a vector of filenames and if (file.exists( would fail.

Related

fread not reading all records and no warning message

I'm trying to load some data using fread. While loading it shows the correct number of records, but when its finished loading, the no. of records are comparatively less.
Surprising it doesn't show any warnings. Please can someone advise? see attached pic
click here
Thanks
One common reason is un-clean data with inappropriat un-ended quotations.
E.g., if you have data like this:
number_column,text_column
1,text data 1
2,"text with single quote here
3,text data 3
EVERYTHING after the single quote will be included in the text_column on the 2nd line. This is actually the correct way to interpret, it's just that your CSV/TSV file is broken.
The easiest solution is to use quote="" as a parameter, but the real solution is to go through your TSV/CSV file and fix all the issues manually, since the interpreter cannot know exactly what you want if the file is broken.

Track the exact place of a not encoded character in an R script file

more a tip question that can save lots of time in many cases. I have a script.R file which I try to save and get the error:
Not all of the characters in ~/folder/script.R could be encoded using ASCII. To save using a different encoding, choose "File | Save with Encoding..." from the main menu.
I was working on this file for months and today I was editing like crazy my code and got this error for the first time, so obviously I inserted a character that can not be encoded while I was working today.
My question is, can I track and find this specific character and where exactly in the document is?
There are about 1000 lines in my code and it's almost impossible to manually search it.
Use tools::showNonASCIIfile() to spot the non-ascii.
Let me suggest two slight improvements this.
Process:
Save your file using a different encoding (eg UTF-8)
set a variable 'f' to the name of that file. something like this f <- yourpath\\yourfile.R
Then use tools::showNonASCIIfile(f) to display the faulty characters.
Something to check:
I have a Markdown file which I run to output to Word document (not important).
Some of the packages I used to initialise overload previous functions. I have found that the warning messages sometimes have nonASCII characters and this seems to have caused this message for me - some fault put all that output at the end of the file and I had to delete it anyway!
Check where characters are coming back from Warnings!
Cheers
Expanding the accepted answer with this answer to another question, to check for offending characters in the script currently open in RStudio, you can use this:
tools::showNonASCIIfile(rstudioapi::getSourceEditorContext()$path)

How to converge multiple R files into one single file

Situation
I wrote an R program which I split up into multiple R-files for the sake of keeping a good code structure.
There is a Main.R file which references all the other R-files with the 'source()' command, like this:
source(paste(getwd(), dirname1, 'otherfile1.R', sep="/"))
source(paste(getwd(), dirname3, 'otherfile2.R', sep="/"))
...
As you can see, the working directory needs to be set correctly in advance, otherwise, this could go wrong.
Now, if I want to share this R program with someone else, I have to pass all the R files and folders in relative order of each other for things to work. Hence my next question.
Question
Is there a way to replace all the 'source' commands with the actual R script code which it refers to? That way, I have a SINGLE R script file, which I can simply pass along without having to worry about setting the working directory.
I'm not looking for a solution which is an 'R package' (which by the way is one single directory, so I would lose my own directory structure). I simply wondering if there is an easy way to combine these self-referencing R files into one single file.
Thanks,
Ok I think you could use something like scaning all the files and then writting them again in the same new one. This can be done using readLines and sink:
sink("mynewRfile.R")
for(i in Nfiles){
current_file = readLines(filedir[i])
cat("\n\n#### Current file:",filedir[i],"\n\n")
cat(current_file, sep ="\n")
}
sink()
Here I have supposed all your file directories are in a vector filedir with length Nfiles, I guess you can adapt that

questions about sink, in R

Lets say I want to use sink for writing to a file in R.
sink("outfile.txt")
cat("Line one.\n")
cat("Line two.")
sink()
question 1. I have seen people writing sink() at the end, why do we need this? Can something go wrong when we do not have this?
question 2. What is the best way to write many lines one by one to file with a for-loop, where you also need to format each line? That is I might need to have different number in each line, like in python I would use outfile.write("Line with number %.3f",1.231) etc.
Question 1:
The sink function redirects all text going to the stdout stream to the file handler you give to sink. This means that anything that would normally print out into your interactive R session, will now instead be written to the file in sink, in this case "outfile.txt".
When you call sink again without any arguments you are telling it to resume using stdout instead of "outfile.txt". So no, nothing will go wrong if you don't call sink() at the end, but you need to use it if you want to start seeing output again in your R session/
As #Roman has pointed out though, it is better to explicitly tell cat to output to the file. That way you get only what you want, and expect to see in the file, while still getting the rest ouf the output in the R session.
Question 2:
This also answers question two. R (as far as I am aware) does not have direct file handling like in python. Instead you can just use cat(..., file="outfile.txt") inside a for loop.

How can I retain the initial white space in a line when writing Rd documentation?

In conjunction with trying to find a solution for myself in regards to this question, I find myself plunged into trying to write valid Rd markup. What I want is to add a section named Raw Function Code and put the code of the function under it. I've achieved limited success in this regard by writing a script to modify the Rd files to include
\section{Raw Function Code}{\code{
# some piece of R script will eventally provide this part of the text
}}
However, even if I manually properly spaced text into the .Rd file (using either spaces or tabs), the initial white space of each line seems to get stripped away leaving an undesirable looking function. I've noticed that if I provide a starting character before the white space the white space is retained. However, I did not want to provide a starting character because I'd like people to be able to copy and paste directly from the produced PDF.
I have reviewed parseRd and I know there are three types of text LaTeX-
like, R-like, and verbatim. I have tried to put my function code in \code and in \verb and neither seemed to yield the desired results. What can I do to hold onto my initial white space?
The \section macro contains LaTeX type of text, but as you want to write code, you could use \synopsis macro, i.e.
\synopsis
# some piece of R script will eventally provide this part of the text
}
There is one problem with this though; you cannot give name to this section, it is automatically named as another usage section. Same thing could be achieved by using \examples macro, but now the name of the section is Examples, which is probably even more dubious (not to mention that you probably already have Examples section).
It isn't possible without modifying the usage or examples sections of your Rd code. See Hemmo's answer for a usable workaround. It produces text in the verbatim mode which is sub-optimal, but far better than nothing.
(This answer is set community Wiki in case this state of affairs changes. This result is current as of R-2.15.1)
If you want a super hacky way to do it, you can use \Sexpr to make zero width characters and add spaces between them:
#' first line \cr
#'\Sexpr{"\u200B"} \Sexpr{"\u200B"} \Sexpr{"\u200B"} \Sexpr{"\u200B"} indented line
A warning however - your package will build fine, but R CMD CHECK will throw a fit.

Resources