R2HTML cannot open connection - r

I am producing a repetitive HTML report displaying tables and graphs computed with R. I am using functions from the R2HTML package, embedded in a for loop. I do not have admin rights on my machine.
At some point in the computations, I sometimes get the following error :
Error in file(file, ifelse(append, "a", "w")) :
cannot open the connection
In addition: Warning message:
In file(file, ifelse(append, "a", "w")) :
cannot open file 'P:/My Documents/R/test.html': Permission denied
This does not happen at every run, nor at the same point in the code.
I can get the same error when I try to run the following simple code :
require( R2HTML)
.HTML.file = file.path( 'P:/My Documents/R' , 'test.html' )
# To set the access path to the HTML file that will be created
cat(paste("<html><head><title>", 'test.html', "</title></head>", sep = ""), file = .HTML.file , append = FALSE )
# To create and initiate the HTML file
for( i in 1:100 )
{
HTML('Test')
}
# To write the same line to the HTML file repeteadly
Most of the time this loop can run without any problems, but sometimes it will produce the above error (sometimes at the beginning of the loop, sometimes at the end).
Does anybody know what causes this? I am using R-2.15.0 on a Windows xp pro machine. If anybody can help me with this, I would be most greatful.
Thanks.

As suggested by Andrie in the comments, the solution is to capture the output of the R2HTML functions in string objects and use only one call to the HTML function to create the HTML file after the loop.

Related

Error while running R script in taskscheduleR, but everything works well while running code in R Studio

So I have an R script which do some basic data analysis, then save a csv file, jpegs and text file. I ran the scheduling in the add-in in R-studio. The error code I'm getting is:
Error in file(file, ifelse(append, "a", "w")) :
cannot open the connection
Calls: write.csv -> eval.parent -> eval -> eval -> write.table -> file
In addition: Warning message:
In file(file, ifelse(append, "a", "w")) :
cannot open file '2021-07-14sudanpost.csv': Permission denied
Execution halted
I'm using the following function to save a dataframe as csv. They work while running in R-Studio, but I'm guessing this is where the above error occurs while using taskscheduleR.
csvname <- as.character(Sys.Date())
csvname2 <- paste0(csvname, "sudanpost.csv")
write.csv(sudan_post, csvname2)
I am running the script daily and am using Sys.Date() to differentiate the names. Not sure if it's "best practice". The script should also save some jpeg files, although I haven't get to that point yet. Any insight on if it would work?
jpegname <- paste0(csvname, "headlines_word_frequency.jpeg")
jpeg(jpegname)
bar <- barplot(dtm_d[1:10,]$freq, las = 2, names.arg = dtm_d[1:10,]$word,
col ="steelblue", main ="Top 10 most frequent words",
ylab = "Word frequencies")
dev.off()

How to eliminate security concern while accessing to the file through program in R in Windows?

While accessing to CSV file from disk with the help of the R program, where a path to the CSV file is provided in the configuration file ( A path is like "testData/Amazon S3/Inventory/Accounts.csv" which is provided in the Configuration file and cfig[2]$save.location is variable who is having value of this path accessed from Configuration file). Few lines of code are below
path <- cfig[2]$save.location
test_data <- fread(path,stringsAsFactors = FALSE,drop=col_ignor,blank.lines.skip = TRUE)
but it gave the message below:
Taking input= as a system command ('testData/Amazon
S3/Inventory/Accounts.csv') and a variable has been used in the
expression passed to input=. Please use fread(cmd=...). There is a
security concern if you are creating an app and the app could have a
malicious user and the app is not running in a secure environment;
e.g. the app is running as root. Please read item 5 in the NEWS file
for v1.11.6 for more information and for the option to suppress this
message.
'testData' is not recognized as an internal or external
command, operable program or batch file. Warning messages:
1: In (if
(.Platform$OS.type == "unix") system else shell)(paste0("(", :
'(testData/Amazon S3/Inventory/Accounts.csv) >
C:\Users\sharmb5\AppData\Local\Temp\RtmpOa25kH\filea78b5351f1'
execution failed with error code 1.
2: In fread(cfig[2]$save.location,
stringsAsFactors = FALSE, drop = col_ignor, : File
'C:\Users\sharmb5\AppData\Local\Temp\RtmpOa25kH\filea78b5351f1' has
size 0. Returning a NULL data.table.
when the following line of code executes,
config[4]$save.location <- stri_replace_all(config[4]$save.location, cp_val, fixed = cp_key)
It gives an error like as, Error in [<-.data.table(*tmp*, j, value = list(TestCaseID = "C419760", : Supplied 14 columns to be assigned 15 items. Please see NEWS for v1.12.2.
The above error was a warning but after manually updating of packages. This warning turns into an error. What will be the reason behind this issue and how to solve it? Thanks for Advance!!!

How do I use strings in functions for R ? (file,rt error)

I am trying to load data into a function to use for analysis later on and there seems to be issues inserting a string (i.e. a filename) into my function. Here is what I am working with.
hist_sep<- function(dex_file,etoh_file,sep_parameter) {
dex<-read.csv("dex_file")$sep_parameter
etoh<-read.csv("etoh_file")$sep_parameter
}
The code below outputs this error
hist_sep(RT_3h_Amp_A.csv , RT_3h_Amp_EtOH_A.csv , FL1.A)
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
However, if I input the filenames myself (as below) then everything works great. So everything is in the correct directory.
dex<-read.csv("RT_3h_Amp_A.csv")$FL1.A
etoh<-read.csv("RT_3h_Amp_EtOH_A.csv")$FL1.A
Any ideas how I could get around this problem?
Remove the quotes around the filenames in your function (e.g., should be dex<-read.csv(dex_file)$sep_parameter, not dex<-read.csv("dex_file")$sep_parameter). Otherwise, it's trying to download a file CALLED "dex_file".

R {ape} clustal cannot find clustalw2

I am a beginner in R.
I would like to use R clustal function in package ape to process my DNA sequence, so I can us {pegas} to perform further analysis.
However, when I first try the example provided in the manual:
clustal(woodmouse, pw.gapopen = 1, pw.gapext = 1, exec = "clustalw2")
But I got an error message:
/bin/sh: clustalw2: command not found
Error in file(file, "r") : cannot open the connection. In addition: Warning massage: In file(file, "r") : cannot open file '/var/folders/vm/fzyykk3x21g55fdvpctj7s900000gn/T//Rtmp0RjsMr/input_clustal.aln' :No such file or directory
I would like to know how to fix this problem? By the way, the OS I use is Mac OS 10.9. Thank you.
Make sure that you have clustalw2 installed. Download *.msi from here:
http://www.clustal.org/download/current/
Then run
require(ape)
fix(clustal)
Make sure that the following part of the clustal function:
shortPathName("C:/Program Files/ClustalW2/clustalw2.exe")
...matches the placement of clustalw2.exe in your system perfectly. Also exhange all the "/" with "\".
Hope that works - worked for me...

"Error in file" instead of "Error in eval" when specifying output file in knit2html

I am making several html reports from one Rmd file called "template.Rmd".
When using the function knit2html(), there is a misleading error message, "cannot open the connection", while in fact the error is due to a code mistake, such as a missing variable. Let me illustrate by a little example below:
Template.Rmd contains this inline R code:
`r missing_variable`
When I don't specify the output file, I get a useful error message
> knit2html("docs/template.Rmd")
Quitting from lines 2-4 (docs/template.Rmd)
Error in eval(expr, envir, enclos) : object 'missing_variable' not found
When I specify the output file, I get a misleading error message
> knit2html("docs/template.Rmd", "docs/template.html")
Warning in file(file, ifelse(append, "a", "w")) :
cannot open file 'docs/template.html': No such file or directory
Error in file(file, ifelse(append, "a", "w")) :
cannot open the connection
Is this a knitr related problem?
I cannot reproduce your problem with the most recent versions of R and knitr on CRAN, but in most cases, you do not need to specify the output argument, and it is strongly not recommended to use an output path that contains a directory name, such as docs/template.html (see the Note section in ?knit). In your case, just switch the working directory to docs and run knit2html(), e.g.
setwd('docs')
knit2html('template.Rmd') # generates template.html

Resources