Using file and cat to create log files in Base R - r

I am running a script for some batch copying/renaming operations using R. The script is executed using a .bat file for executing using command prompt in MS Windows. Note that the destination computer where this will be run does not allow external connections (internet of otherwise) to install new packages so the solution should be in base R.
I can print comments to screen using cat and the output shows up in the .Rout file generated after the script is run. The .Rout file gets overwritten each time the script is executed and I would like to create a separate log file instead.
Here's the relevant of code:
if(copy_files){
# tried different modes for the file statement below
log_con <- file(paste(format(Sys.time(), '%d-%b-%Y %H:%M:%S'),
'move duplicates.txt'),
open = 'at')
cat('Parent directory:\t\t' , file = log_con, append= F)
cat(parent_folder , file = log_con, append= T)
cat('\nSubfolder to copy files to:\t ', file = log_con, append= T)
cat(subfolder_old, file = log_con, append= T)
cat('\n\n', file = log_con, append= T)
# copying operations here - omitted for brevity
}
Using only the cat statements without the file and append argument works fine but the above code returns the following error messages:
> log_con <- file(paste(format(Sys.time(), '%d-%b-%Y %H:%M:%S'), 'move duplicates.txt'), open = 'at')
Error in file(paste(format(Sys.time(), "%d-%b-%Y %H:%M:%S"), "move duplicates.txt"), :
cannot open the connection
In addition: Warning message:
In file(paste(format(Sys.time(), "%d-%b-%Y %H:%M:%S"), "move duplicates.txt"), :
cannot open file '07-Aug-2018 15:50:36 move duplicates.txt': Invalid argument
> cat('Parent directory:\t\t' , file = log_con, append= F)
Error in cat("Parent directory:\t\t", file = log_con, append = F) :
cannot open the connection
In addition: Warning message:
In cat("Parent directory:\t\t", file = log_con, append = F) :
cannot open file '07-Aug-2018 15:48:11 move duplicates.txt': Invalid argument
From what I understand the error stems from the fact that the log file does not exist at the beginning and the connection can not be opened.
Looking at the documentation and the answer for How to create periodically send text to a "log file" while printing normal output to console? seems to suggest that including append = F in the first cat statement ought to work. I have tried versions where different/no mode is specified for file command with the same result. Answers for Add lines to a file seem to suggest the same. Am I missing something?
I could create a file and have R append lines to it each time but I want a unique log for each time the script is run.

one way to solve this is to use sink, at the start of the code put sink("R_code.log") once the whole process is done put sink() at the last line of code .I hope this will solve your problem and if you want to name it dynamically use paste0 inside sink function.

Related

what is the solution about R programming?

I try to solve this problem. but I don't know how to solve it.
I wrote this codes.
algae<-read.table('Analysis.txt', header=F, dec = '.',
col.names=c('season','size','speed','mxPH','mnO2','C1','NO3','NH4','oPO4',
'Chla','a1','a2','a3','a4','a5','a6','a7'), na.strings=c('XXXXXXX'))
but it is not working.
R just showed this.
Error in file(file, "rt") : cannot open the connection In addition:
Warning message: In file(file, "rt") : cannot open file
'Analysis.txt': No such file or directory
what's the problem?
Often (especially on Windows) it is hard to know the path to a file. I usually do something like
filename <- file.choose()
which opens the dialog to let me find the file, then use filename instead of typing the file name explicitly, i.e.
algae<-read.table(filename, header = FALSE, dec = '.',
col.names = c('season', 'size', 'speed', 'mxPH', 'mnO2', 'C1',
'NO3', 'NH4', 'oPO4', 'Chla', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7'),
na.strings = 'XXXXXXX')
I've also changed F to FALSE: F is a variable, and might not be FALSE.
The file is likely not in your working directory, as others have mentioned. You can use getwd() to figure out what is currently set as your directory.
If you would like to define your working directory, say, to the folder where Analysis.txt is held currently, you can use the code below to change the wd and then call on it to load your file.
setwd("C:/User/folder/subfolder/") # set directory
mytable <- read.table(paste0(getwd(), "file.txt"), header=F, dec=".") # call wd & grab file
# load using your code
algae<-read.table(paste0(getwd(), '/Analysis.txt'), header=F, dec = '.',
col.names=c('season','size','speed','mxPH','mnO2','C1','NO3','NH4','oPO4',
'Chla','a1','a2','a3','a4','a5','a6','a7'), na.strings=c('XXXXXXX'))
Note that I changed your line by adding paste0(getwd(), yourfile.txt) to call upon that working directory when reading the file in. Another note here: R prefers you use / instead of \ but on a Windows machine copying the path will use the latter, so you'll likely need to go through and change each of them in your file path.

Error Handling and logging in R

I have written a function in R to print any message both to log file and console. But if there is any unexpected error while running the code, then error is displayed only to console. Is there any way to write error message to both console and log file? Here is the function..
log_con <- file("Text1.txt", open="a")
loggerfn<-function(Message,LogConnection=log_con){
cat(Message, file = LogConnection)
cat(Message)
}
Here is the sample code to...
for (i in 1:10)
{
loggerfn("loop begins\n",log_con)
a <- rnorm(n = 100, mean = i, sd = 5)
loggerfn(mean(a),log_con)
loggerfn("loop Completed\n",log_con)
if(i==8){
sdfs
}
}
In above code I have intentionally introduced error by providing undefined object sdfd.Below provided Error message is shown only in console, is there any way to write error message to both console and logfile?
Error: object 'sdfs' not found
use sink() to divert messages as well as warnings to a file. The trick is to set the argument type="message"
refer http://www.statmethods.net/interface/io.html
and Output error/warning log (txt file) when running R script under command line
https://stat.ethz.ch/R-manual/R-devel/library/base/html/sink.html
The sink( ) function defines the direction of the output.
Description
sink diverts R output to a connection (and stops such diversions).
sink.number()
reports how many diversions are in use.
sink.number(type = "message") reports the number of the connection currently being used for error messages.
Usage
sink(file = NULL, append = FALSE, type = c("output", "message"),
split = FALSE)
sink.number(type = c("output", "message"))
direct output to a file
sink("myfile", append=FALSE, split=FALSE)
return output to the terminal
sink()
The append option controls whether output overwrites or adds to a file. The split option determines if output is also sent to the screen as well as the output file.
Here are some examples of the sink() function.
output directed to output.txt in c:\projects directory.
output overwrites existing file. no output to terminal.
sink("c:/projects/output.txt")
output directed to myfile.txt in cwd. output is appended
to existing file. output also send to terminal.
sink("myfile.txt", append=TRUE, split=TRUE)
When redirecting output, use the cat( ) function to annotate the output.

R how to create character vector of paths

I am following this tool set called IUTA that needs to create a vector of the character of paths.
The example in the program shows that I have to do the following:
## set the paths for the BAM file and GTF file
## notice that the gtf file contains correct gene_id information
bam.list.1<-system.file("bamdata",paste("sample_",1:3,".bam",sep=""),
package="IUTA")
bam.list.2<-system.file("bamdata",paste("sample_",4:6,".bam",sep=""),
package="IUTA")
transcript.info<-system.file("gtf","mm10_kg_sample_IUTA.gtf",
package="IUTA")
Ok I tried that, but its not creating moving the path for all of my bam files nor my gtf file.
I get the following error, when I try to run the program as it can not find nor open my bam files.
IUTA(bam.list.1, bam.list.2, transcript.info,
+ rep.info.1 = rep(1, length(bam.list.1)),
+ rep.info.2 = rep(1, length(bam.list.2)),
+ output.dir = paste(getwd(), "/IUTA", sep = ""),
+ output.na = TRUE,
+ genes.interested = "all")
All 8 cores on the machine will be used.
Preparing indices of bam files ......
open: No such file or directory
Error in FUN(X[[i]], ...) : failed to open SAM/BAM file
file: ''
I was wondering what exactly, I am doing wrong. Also I am working from an external hard drive and I am in that particular directory as well.
Many thanks.
http://www.niehs.nih.gov/research/resources/assets/docs/iuta_pdf_manual_508.pdf

Error in file(file, "rt") : cannot open the connection [duplicate]

This question already has answers here:
How to open CSV file in R when R says "no such file or directory"?
(16 answers)
Closed 2 years ago.
I'm new to R, and after researching this error extensively, I'm still not able to find a solution for it. Here's the code. I've checked my working directory, and made sure the files are in the right directory. Appreciate it. Thanks
pollutantmean <- function(directory, pollutant = "nitrate", id= 1:332)
{ if(grep("specdata",directory) ==1)
{
directory <- ("./specdata")
}
mean_polldata <- c()
specdatafiles <- as.character(list.files(directory))
specdatapaths <- paste(directory, specdatafiles, sep="")
for(i in id)
{
curr_file <- read.csv(specdatapaths[i], header=T, sep=",")
head(curr_file)
pollutant
remove_na <- curr_file[!is.na(curr_file[, pollutant]), pollutant]
mean_polldata <- c(mean_polldata, remove_na)
}
{
mean_results <- mean(mean_polldata)
return(round(mean_results, 3))
}
}
The error I'm getting is below:
Error in file(file, "rt") : cannot open the connection
file(file, "rt")
read.table(file = file, header = header, sep = sep, quote = quote,
dec = dec, fill = fill, comment.char = comment.char, ...)
read.csv(specdatapaths[i], header = T, sep = ",")
pollutantmean3("specdata", "sulfate", 1:10)
In addition: Warning message:
In file(file, "rt") :
cannot open file './specdata001.csv': No such file or directory
You need to change directory <- ("./specdata") to directory <- ("./specdata/")
Relative to your current working directory, you are looking for the file 001.csv, which is in your specdata directory.
This question is nearly impossible to answer without any context, since you have not provided us with the structure of your working directory here. Fortunately for you, I have already taken R Programming on Coursera, so I already did this homework question.
The reason why you see this error I guess is because RStudio lost the path of your working directory.
(1) Go to session...
(2) Set working directory...
(3) Choose directory...
--> Then you can see a window pops up.
--> Choose the folder where you store your data.
This is the way without any code that you change your working directory.
Hope this can help you.
Set your working directory one level/folder higher. For example, if it is already set as:
setwd("C:/Users/Z/Desktop/Files/RStudio/Coursera/specdata")
go up one level back and set it as:
setwd("C:/Users/Z/Desktop/Files/RStudio/Coursera")
In other words, do not make "specdata" folder as your working directory.
I just spent a lot of time trying to understand what was wrong on my code too...
And it seems to be simple if you are using windows.
When you name your file "blabla.txt" then windows name it "blabla.txt.txt"...
That's the same with .CSV files so windows create a file named "001.csv.csv" if you called it "001.csv"
So, when you create your .csv file, just rename it "001" and open it in R using read.table("/absolute/path/of/directory/with/required/001.csv")
It works for me.
close your R studio and run it again as an administrator. That did the magic for me. Hope it works for you and anyone going through this too.
One better check that can be done if you get such error while accessing a file is to use the file.exists("file_path/file_name") function. This function will return TRUE if the file is existing and accessible, else False.
If running on Windows try running R or R Studio as administrator to avoid Windows OS file system constraints.
Use setwd() to change to appropriate directory.
Use only filename to access any file in the working directory.
Navigate a folder above by using "../<filename>".
Got this error and found that RStudio on my Windows machine try to use \ as escape symbol, so had to replace it with \\ to deal with it.
Try file.exists function with your path, e.g.:
file.exists("D:\\R\\path_to_file.csv")
Error in file(file, "rt") :
I just faced the same error and resolved by removing spacing in address using paste0 instead of paste
filepath=paste0(directory,"/",filename[1],sep="")
I got this same error message and fixed it in the easiest way I could. I put my .csv file into a file folder on my desktop, opened desktop in the window next to console on RStudio, and then opened my file there, and checked the box next to my .csv file, then I used the "more" pull down menu at the top of this window to set this as my working directory...probably the easiest thing for SUPER beginners like me :)
This error also occurs when you try to use the result of getwd() directly in the path. The problem is the missingness of the "/" at the end. Try the following code:
projectFolder <- paste(getwd(), "/", sep = '')
The paste() is to concatenate the forward slash at the end.
I got my R code file from a friend and was not able to run read.csv command but If I copy the same command(read.csv ) to a new R script file, it ran fine.
Below command was not running in R code file shared by my friend, working directory,file name etc were all correct because If I created a new R script file and ran below command ,it worked.
df <- read.csv("file.csv",header=TRUE,stringsAsFactors = FALSE,strip.white =
TRUE,sep = ',')
issue/resolution:
I right clicked the R code file and unblocked the file and click save button and issue got resolved.
I your R code file is in Downloads folder in windows , then move to some other folder.
I had a same issue .I removed the extension from the file name.Example my file name was saved as xyz. csv. i saved it as xyz.
Error in file(file, "rt")
Created a .r file and saved it in Desktop together with a sample_10000.csv file.
Once trying to read it
heisenberg <- read.csv(file="sample_100000.csv")
was getting the same error as you
heisenberg <- read.csv(file="sample_10000")
Error in file(file, "rt") : cannot open the connection In addition: Warning message:
In file(file, "rt") : cannot open file 'sample_10000': No such file or
directory
I knew at least two ways to fix this, one using the absolute path and the other changing the working directory.
Absolute path
I fixed it adding the absolute path to the file, more precisely
heisenberg <- read.csv(file="C:/Users/tiago/Desktop/sample_100000.csv")
Working directory
This error shows up because RStudio has a specific working directory defined which isn't necessarily the place the .r file is at.
So, to fix using this approach I've gone to Session > Set Working Directory > Chose Directory (CTRL + Shift + H) and selected Desktop, where the .csv file was at. That way running the following command also worked
heisenberg <- read.csv(file="sample_100000.csv")
I was getting the same error when trying to import 10,000 files. I tried opening a single file with Excel and Excel gave me an error message: "the file path is too long".
The file was buried in 6 nested folders, which is a problem of itself. Moving all the files to a desktop folder solved the issue without having to change any code.
I came across a solution based on a few answers popped in the thread.
(windows 10)
setwd("D:/path to folder where the files are")
directory <- getwd()
myfile<- "a_file_in_the_folder.txt"
filepath=paste0(directory,"/",myfile[1],sep="")
table <- read.table(table, sep = "\t", header=T, row.names = 1, dec=",")

Is it possible to run (execute) an r script from inside of an r script?

first time here and maybe I am out of place with this question, but I was unable to found an answer
I want to run a Shiny App with only one button, that runs an R script in order to generate a PPT file, something like what happens in this link, just instead of the MSWord output the PPT.
I want to avoid to have all the lines inside the ReporteRs-PPT_v3.R script inside the Shiny script and just call it or execute it like in any other lenguage.
I was trying something like this:
observe({
source("C:/Data/Example/Shiny/App-Ficha/ReporteRs-PPT_v3.R")
})
Also tried this other option:
observe({
R CMD BATCH /ReporteRs-PPT_v3.R
})
Where ReporteRs-PPT_v3.R is an existent script that by itself generates a PPT file using ReporteRs
Any idea on where can I keep looking?
Thanks.
[UPDATE]
The error when try the first option:
Listening on http://127.0.0.1:6158
Error in source(serverR, local = new.env(parent = globalenv()), keep.source = TRUE) :
C:\Data\Example\Shiny\App-Ficha/server.R:219:0: unexpected end of input
217: }
218: )
^
The error with the second option:
Listening on http://127.0.0.1:6158
Error in source(serverR, local = new.env(parent = globalenv()), keep.source = TRUE) :
C:\Data\Example\Shiny\App-Ficha/server.R:161:9: unexpected symbol
160: #source("C:/Data/Example/Shiny/App-Ficha/ReporteRs-PPT_v3.R")
161: R CMD
^
Also, when I click the button in the Shiny App, nothing happens.

Resources