[R] Problem in shiny writing a .txt file

Mathew Guilfoyle mrguilfoyle at gmail.com
Wed Jul 19 12:16:26 CEST 2017


Is res.path usually empty? If the res.path directory is empty (i.e. dir(res.path) is an empty vector) the file.remove operation will remove the directory.  This behaviour is documented in the help for file.remove.  When your subsequent function tries to write to that directory it does not exist hence the error. 

wd = tempdir()
res.path = paste0(wd,'/OUT/')
dir.create(res.path)
#this will delete the directory as res.path is empty
file.remove(paste0(res.path,dir(res.path))) 

dir.create(res.path)
file.create(paste0(res.path,'test.txt')
#this will delete the test.txt file and leave the directory in place
file.remove(paste0(res.path,dir(res.path))) 

Does your function work if you don't do the file.remove beforehand?

HTH

> On 19 Jul 2017, at 09:19, Ana Belén Marín <anabelen.marin4 at um.es> wrote:
> 
> Hi all!
> 
> I'm developing a shiny app and I have problems when I wanna write a .txt 
> file.
> 
> First of all, I change the directory in order to work in a temporal one:
> 
> wd   <- tempdir()
> setwd( wd )
> res.path <- paste0( wd, "/OUT/" )
> dir.create( res.path )
> 
> Just before calling the function that fails, I remove, if exist, the old 
> files of the directory:
> 
> file.remove( paste0( res.path, dir( res.path ) ) )
> 
> Then, I call f.texto, whose code is as follows:
> 
> f.texto <- function( l, res, a ){
>   myfile <- res
>   write( paste( "SAIC50. ", a, ", ", date(), sep = "" ), file = paste( 
> res.path, myfile, sep = "" ) )
>   write( "----------------------------------", file = paste (res.path, 
> myfile, sep = ""), append = T )
>   write( "\r\n", file = paste( res.path, myfile, sep = "" ), append = T )
>   write( l[[6]], file = paste( res.path, myfile, sep = "" ), append = T )
>   write( "\r\n", file = paste( res.path, myfile, sep = "" ), append = T )
> }
> 
> and I get the next error the first time I run the app and choose the 
> wanted option  (then, the error disappears):
> 
> Error in file: cannot open the connection.
> 
> Warning message:
> 
> In file(file, ifelse(append, "a", "w")) :
> 
> cannot open the file 
> '/tmp/RtmpSQOYnV/OUT/Resultado-ajuste5-ic50-2017-07-19.txt': No such 
> file or director
> 
> It's like the first time running the app, the file is not created with 
> the command /write("...", file = "...") in spite of the option append is 
> false. /
> 
> Thanks for your help,
> 
> Anab.
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list