Problem in shiny writing a .txt file

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem in shiny writing a .txt file

Ana Belén Marín
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Problem in shiny writing a .txt file

Rolf Turner

On 19/07/17 19:19, Ana Belén Marín wrote:

> Hi all!
>
> I'm developing a shiny app and I have problems when I wanna write a .txt
> file.

<SNIP>

" ... when I *want to* write ..."

The language of this mailing list is *English*, not Valspeak.

cheers,

Rolf Turner

--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Problem in shiny writing a .txt file

Ulrik Stervbo-2
Hi Ana,

The path is most likely wrong.

How does f.texto() know the res.path? Do you manage to remove the old path
and create a new one but f.texto() doesn't know?

Not reasons for your problem, but curious: Why do you change the working
directory? What is the intention behind appending dir(res.path) to
res.path? Why don't you create a vector with the lines and use writeline()
in f.texto() instead of opening and closing the file several times?

HTH
Ulrik

On Wed, 19 Jul 2017 at 11:32 Rolf Turner <[hidden email]> wrote:

>
> On 19/07/17 19:19, Ana Belén Marín wrote:
>
> > Hi all!
> >
> > I'm developing a shiny app and I have problems when I wanna write a .txt
> > file.
>
> <SNIP>
>
> " ... when I *want to* write ..."
>
> The language of this mailing list is *English*, not Valspeak.
>
> cheers,
>
> Rolf Turner
>
> --
> Technical Editor ANZJS
> Department of Statistics
> University of Auckland
> Phone: +64-9-373-7599 ext. 88276 <+64%209-373%207599>
>
> ______________________________________________
> [hidden email] 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.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Problem in shiny writing a .txt file

Mathew Guilfoyle
In reply to this post by Ana Belén Marín
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 <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.