I agree that my example was not optimal, but I was trying to duplicate the existing behaviour with the exception of use of global variables.
On April 18, 2020 3:02:17 PM PDT, Duncan Murdoch <murdoch.dun...@gmail.com> wrote: >On 18/04/2020 5:55 p.m., Jeff Newmiller wrote: >> See below: >> >> On Sat, 18 Apr 2020, David Andres Zamora Avila wrote: >> >>> Thanks for your answers. >>> This a part of my script where it creates files and saves it in >anything >>> path. I am not pretty secure how use file.path() or tempdir(). Can >you give >>> me more details about it, please? >>> >>> if (!exists("path_pet")){ >>> path_pet <- getwd() >>> } else if (!exists("path_p")){ >>> path_p <- getwd() >>> } else if (!exists("path_p") | !exists("path_pet")){ >>> path_pet <- getwd() >>> path_p <- getwd() >>> } >> >> The above code should not exist. You are making a package, so you are >> putting it into a function and either looking for the existence of >global >> variables (side effects! requiring user to use special variable >names! >> this is not good!), or the "path_pet" and "path_p" variables were >created >> in your function and you should already know that they exist. >> >> If we assume that "path_pet" and "path_p" are parameters to this >package >> function you are writing, like so: >> >> myfunc <- function( x, y, ..., path_pet = getwd(), path_p = getwd() ) >{ >> ... >> } >> >> then the user can let the default be the current working directory, >or can >> specify which directories the function should work in. Specifically, >when >> you are writing your examples or test code and pretending to be a >user, >> you can create a temporary directory and use it like so: > >I think your advice would probably pass the checks, but it's bad >advice, >because it leaves the user vulnerable to having files wiped out by >myfunc(). > >Functions shouldn't write to the current directory unless the user >explicitly asks for it. > >A better header would be > >myfunc <- function( x, y, ..., path_pet = temp.dir(), path_p = >temp.dir() ) { > >Then the user has to explicitly code "path_pet = getwd()" to wipe out >their own files. > >Duncan Murdoch > >> >> workingdir <- tempdir(TRUE) >> result <- myfunc( A, B, path_pet = workingdir, path_p = workingdir ) >> >> which means you can then clean up after yourself with >> >> unlink( workingdir, recursive = TRUE ) >> >> I don't think this question is really about R packages anymore... if >you >> still cannot read the help files to figure out your questions about >how to >> do manage temporary files then you should probably make a small >> reproducible example(!!) and post a question on R-help. >> >> (end of comments) >> >>> if (file_type == "raster"){ >>> # ---- identify raster format and loading---- >>> if (format == "GTiff"){ >>> >>> if( length(list.files(path_pet, pattern = ".tif")) == 0 | >length( >>> list.files(path_p, pattern = ".tif")) == 0){ >>> stop("Not avaliable data of precipitation or >evapotranspiration") >>> } >>> pet_files <- list.files(path_pet) >>> pet <- raster::stack(paste(path_pet, pet_files, sep = "")) >>> p_files <- list.files(path_p) >>> Kind regards, >>> >>> David Zamora >>> >>> El jue., 16 abr. 2020 a las 17:12, Uwe Ligges (< >>> lig...@statistik.tu-dortmund.de>) escribi?: >>> >>>> >>>> >>>> On 16.04.2020 20:09, Duncan Murdoch wrote: >>>>> On 16/04/2020 1:11 p.m., David Andres Zamora Avila wrote: >>>>>> Hi, >>>>>> >>>>>> I submitted my package in CRAN and always appearance the next >NOTE. >>>>>> >>>>>> Flavor: r-devel-linux-x86_64-debian-gcc >>>>>> Check: for detritus in the temp directory, Result: NOTE >>>>>> Found the following files/directories: >>>>>> 'RtmpcDoRWjr.nc' >>>>>> >>>>>> I have tried to solve it in several ways (like if(interactive()), >but >>>>>> I not >>>>>> sure how can I solve it. >>>>> >>>>> How was the file created? What the check wants is that you delete >it >>>>> when you are finished with it. You can use the unlink() function >to do >>>>> that. >>>> >>>> Or better create it in tempdir(): I guess you used paste(tempdir(), >...) >>>> instead of file.path() and got afilename one level above tempdir()? >>>> >>>> Best, >>>> Uwe Ligges >>>> >>>> >>>>> >>>>> Duncan Murdoch >>>>> >>>>> ______________________________________________ >>>>> R-package-devel@r-project.org mailing list >>>>> https://stat.ethz.ch/mailman/listinfo/r-package-devel >>>> >>> >>> >>> -- >>> >>> David Zamora <https://sites.google.com/a/unal.edu.co/dazamoraa/> >>> PhD Student, Universidad Nacional de Colombia, Bogot? >>> phone: +571 3165000 ext 13406 >>> address: Av. NQS (Carrera 30) # 45-03, Hydraulics Lab (408-213) >>> email: dazamo...@unal.edu.co >>> >>> [[alternative HTML version deleted]] >>> >>> ______________________________________________ >>> R-package-devel@r-project.org mailing list >>> https://stat.ethz.ch/mailman/listinfo/r-package-devel >>> >> >> >--------------------------------------------------------------------------- >> Jeff Newmiller The ..... ..... Go >Live... >> DCN:<jdnew...@dcn.davis.ca.us> Basics: ##.#. ##.#. Live >Go... >> Live: OO#.. Dead: OO#.. >Playing >> Research Engineer (Solar/Batteries O.O#. #.O#. with >> /Software/Embedded Controllers) .OO#. .OO#. >rocks...1k >> >> ______________________________________________ >> R-package-devel@r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-package-devel >> -- Sent from my phone. Please excuse my brevity. ______________________________________________ R-package-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-package-devel