Knit function and dynamic file - r

I have some problems with function knit. I would like to create a report in different file everyday. So I would like to have a different name of this file each day.
And here is my problem:
When I want to do something like this:
knit("Documents/20170726_xyz.Rnw",
"Documents/20170726_xyz.tex",encoding = 'UTF-8')
Everything is ok in this case.
But when I do something like this:
if (wday(Sys.Date())==2){
date2<-Sys.Date()-3
} else {
date2<-Sys.Date()-1
}
knit(paste("Documents/",date2,"_xyz.Rnw", sep = ''",
paste("Documents/",date2,"_xyz.tex", sep = ''",encoding = 'UTF-8')
I receive an error:
Error in readLines(if (is.character(input2)) { :
cannot open the connection
In addition: Warning message:
In readLines(if (is.character(input2)) { :
cannot open file 'Dokumenty_2017-08-23_xyz.Rnw': No such file or directory
How could I do the second example properly?

Related

filterAndTrim : Error in add(bin) : record does not start with '#'

I'm using dada 2 version ‘1.22.0’ on windows 10, i have list of compressed (.gz ) fastq files, when i use the function filterAndTrim i get this error message :
Error in add(bin) : record does not start with '#'
But when i want to see if i can read th .gz file with library(ShortRead) :
library(ShortRead)
fn <- "path/to/example.fastq.gz"
reads <- readFastq(fn)
it doesn't give any error message message
I don't understand why the function filterAndTrim give the error message
Error in add(bin) : record does not start with '#'
Do you have any solution?

Reading a vcf.bgz file in R

I downloaded some data from gnomad - https://gnomad.broadinstitute.org/downloads. it comes in the form of VCF.bgz file and I would like to read it as a vcf file.
i wrote some code:
install.packages("R.utils")
library("R.utils")
df=gunzip("gnomad.exomes.r2.1.1.sites.4.vcf.bgz", "gnomad.exomes.r2.1.1.sites.4.vcf")
install.packages("vcfR")
library("vcfR")
vc=read.vcfR("gnomad.exomes.r2.1.1.sites.4.vcf.bgz")
but it doesn't work -- it doesn't convert it to appropriate VCF file.
Warning message:
In file.remove(filename) :
cannot remove file 'gnomad.exomes.r2.1.1.sites.4.vcf.bgz', reason 'Permission denied'
Error in read.vcfR(df) :
File: gnomad.exomes.r2.1.1.sites.4.vcf does not appear to be a VCF file.
First line of file:
gnomad.exomes.r2.1.1.sites.4.vcf
Should begin with:
##fileformat=VCFv
In addition: Warning message:
In scan(file = file, what = character(), nmax = 1, sep = "\n", quiet = TRUE, :
embedded nul(s) found in input
would appreciate any help, thank you:)

Error when trying to prase a HTTP-Request in R

im using R package httr to get a HTTP-Response for a specific link.
When trying to parse the content of the response im getting the Error:
Fehler in parse(text = script_content) : <text>:1:10: Unerwartete(s) '['
1: {"lines":[
Translated to enlgish it says something like this (sorry for my error messages being in German):
Error in parsing(text = script_content) : <text>1:10: Unexpected '['
1: {"lines":[
It seems as there is a problem with the format/encoding of the text. Here is my code:
script <-
GET(
url = "https://my_url.which_origin_is_not_important/my_script.R",
authenticate(username, pass)
)
script_content <- content(script, as = "text", encoding = "ISO-8859-1")
parsed_condent <- parse(text = script_content )
The value of script_content looks like this:
"{\"lines\":[{\"text\":\"################## FUNCTION ##################\"},{\"text\":\"\"},{\"text\":\"library(log4r)\"}],\"start\":0,\"size\":32,\"isLastPage\":true,\"limit\":500,\"nextPageStart\":null}"
Some more background to this operation: Im trying to source a code, which is currently inside of a private repository. I wrote the code myself i'm trying to source. I made sure, that the issue is not coming from within th code.
I got the solution from: Sourcing R files in a private github folder
Thanks for any advice!!

openxlsx::saveWorkbook -- write error during file append

I have a script that has been running smoothly for months. The last line of code basically goes as follows:
saveWorkbook(Wb, 'address/filename.xlsx'), overwrite = TRUE)
I run this script weekly (Mondays, unimportant), so I go to run it this week and I'm now getting this error when I go to save this created workbook:
Warning message:
In file.append(to[okay], from[okay]) : write error during file append
The address for this file is on a shared drive for work, so one of my first thoughts was maybe there were some new permissions for the shared drive, since saving this on local drives seems okay. But, I can save csv files on the shared drive still (using data.table::fwrite).
I'm a bit at a loss here. I've updated R, RTools, and RStudio and all my packages.
Has anyone come across this, or a similar, issue before? I could possibly be looking for some more information concerning the "write error during file append". I'm actually creating a whole new file when I run this and not appending anything to an existing file. But, I haven't been able to find anything explaining situations that could cause this error.
I have the same output but only inside a blob container on azure
overwrite instruction fails
packageVersion("openxlsx")
[1] ‘4.2.5’
> openxlsx::write.xlsx(x = mtcars,file ="mtcars.xlsx")
> openxlsx::write.xlsx(x = mtcars,file ="mtcars.xlsx")
Warning message:
In file.append(to[okay], from[okay]) : write error during file append
> openxlsx::write.xlsx(x = mtcars,file ="mtcars.xlsx",overwrite = T)
Warning message:
In file.append(to[okay], from[okay]) : write error during file append
> openxlsx::write.xlsx(x = mtcars,file ="mtcars.xlsx",overwrite = F)
Error in saveWorkbook(wb, file = file, overwrite = overwrite) :
File already exists!
Same code on my desktop produces diferent output
> packageVersion("openxlsx")
[1] ‘4.2.5’
> openxlsx::write.xlsx(x = mtcars,file ="mtcars.xlsx")
> openxlsx::write.xlsx(x = mtcars,file ="mtcars.xlsx")
> openxlsx::write.xlsx(x = mtcars,file ="mtcars.xlsx",overwrite = T)
> openxlsx::write.xlsx(x = mtcars,file ="mtcars.xlsx",overwrite = F)
Error in saveWorkbook(wb, file = file, overwrite = overwrite) :
File already exists!
As we can't fix Azure I recomend delete first and the write
> filexls= "mtcars.xlsx"
> if (file.exists(filexls)) {
+ file.remove(filexls)
+ }
[1] TRUE
> openxlsx::write.xlsx(x = mtcars,file ="mtcars.xlsx",overwrite = T)

R read.csv from URL error in knitr

When I run this code in the R console, it works fine:
read.csv('https://courses.edx.org/c4x/MITx/15.071x_2/asset/WHO.csv')
But when I try to put in into an R markdown document and knit it, I get the following:
Error in open.connection(file, "rt") : cannot open the connection
Calls: <Anonymous> ... eval -> read.csv -> read.table -> open -> open.connection
Execution halted
I also tried using http and url(), but neither helped
read.csv('http://courses.edx.org/c4x/MITx/15.071x_2/asset/WHO.csv')
read.csv(url('http://courses.edx.org/c4x/MITx/15.071x_2/asset/WHO.csv'))
Both work fine in usual R sessions.
How can knitr? I hope there are ways that avoid downloading the file and placing it somewhere into working directory.
For linking purposes: It's the same issue as in read.table() and read.csv both Error in Rmd, but it my case I'm trying to read from an url, not from a file.
Thus adapting #Thomas's answer to your data, the following does the trick.
library(RCurl)
data <- getURL("https://courses.edx.org/c4x/MITx/15.071x_2/asset/WHO.csv",
ssl.verifypeer=0L, followlocation=1L)
read.csv(text=data)
You may also check Error when knitr has to download a zip file, where dropping https for http helped.
Answer of #puslet88 helped, but I modified it slightly to be less "intrusive".
So what I did was putting the following at the beginning of the Rmd fine:
```{r, echo=FALSE, warning=FALSE, message=FALSE}
library(RCurl)
read.csv.orig = read.csv
read.csv = function(file, ...) {
if (is.character(file)) {
if (grepl('^https://', file)) {
data = getURL(file, ssl.verifypeer=0L, followlocation=1L)
return (read.csv.orig(text=data, ...))
} else if (grepl('^http://', file)) {
data = getURL(file)
return (read.csv.orig(text=data, ...))
} else {
return (read.csv.orig(file, ...))
}
} else {
return (read.csv.orig(file, ...))
}
}
```
Now I don't have to change all the calls to read.csv in my R markdown document, I use it as before:
read.csv('https://courses.edx.org/c4x/MITx/15.071x_2/asset/WHO.csv')

Resources