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

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".

Related

RShiny: Pass in a FilePath

Trying to do something pretty simple here.. I just want to hard code a file path and pass it into "read.csv" but on run it's removing the "\t" from the string.
filePath = toString("C:\test.csv")
scenario_data <<- read.csv(filePath, stringsAsFactors=F)
Error:
Warning in file(file, "rt") :
cannot open file 'C: est.csv': Invalid argument
Warning: Error in file: cannot open the connection
1: runApp
I know in Python I can use r"this is a string" so that this doesn't happen. Is there something similar in RShiny?

Import Excel data into R using openxlsx: Error in file(con, "r") : invalid 'description' argument

I am trying to import the data in my Excel file into R using Openxlsx library:
library(openxlsx)
data <- read.xlsx("datafile.xlsx", sheet = "Sheet1")
However, I get the following error:
Error in file(con, "r") : invalid 'description' argument
In addition: Warning message:
In unzip(xlsxFile, exdir = xmlDir) : error 1 in extracting from zip file
This error is thrown because your Excel file is open.
Save and close the Excel file and try again, it will work.
There's also another possibility: the XLSX file could be password protected. If you delete the password, then this can fix the error.
I think the best way to solve this problem is to reset the pathway of your data source. Please do not include any characters without English in your pathway.
setwd("C:\\Users\\your path way (where you store datafile.xlsx)")
P.S.
Rstudio2021 seem not friendly to non-English user ☺☺☺

R read.zoo() can's accept object's value input as an argument. R

I created one data.frame called data1 and read it by read.zoo function as follows works perfectly.
data2<-read.zoo(data1)
now I do this for the whole list of existing data.frames by using following code .
names_of_dataframes <- ls.str(mode = "list")
data2<-read.zoo(name_of_dataframes[1])
This gives the following error.
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") : cannot open file 'data1': No such file or directory
Thought it's character string issue then tried the following but seems not working
data2<-read.zoo(as.character(name_of_dataframes[1]))
Now how can I pass the value of 'data1' to read.zoo?
there are about 100 data and want to make it automated from the list of dataframes.
Thank you R Gurus,
Kaz

"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

R2HTML cannot open connection

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.

Resources