Why does dir.create give cannot create dir 'results', reason 'No such file or directory'? - r

After run my analysis I tried to create a new folder and save my results. I got the message:
dir.create("results")
Warning message:
In dir.create("results") :
cannot create dir 'results', reason 'No such file or directory'

In case you want to create a new nested directory, e.g. "D:/mytest/myresults", wehre D:/mytest needs to be created as well as the subdirectory /myresults, dir.create() will not work when you use:
dir.create("D:/mytest/myresults")
Becasue "D:/mytest" does not exist, you cannot create "D:/mytest/myresults" and you will receive the warning:
In dir.create("D:/mytest/myresults") : cannot create dir
'D:\mytest\myresults', reason 'No such file or directory'
Use recursive = TRUE insted:
dir.create("D:/mytest/myresults", recursive = TRUE)
Using recursive = TRUE is equivalent to creating the base directory first and the subdirectory after that, like:
dir.create("D:/mytest")
dir.create("D:/mytest/myresults")

Related

Error in: Could not find a root 'DESCRIPTION' file that starts with '^Package'

I'm developing a shinyapp with golem. From my computer everything works perfectly, but then I loaded the package on github and installed on another computer. The app has two main parts: the first where you can process raw data and save the output as a .rds file, and the second where you can analyze the elaborated data (you can also upload a .rds file already processed). The problem is in the first part and when I have to read multiple files. It gives me this error:
Error in: Could not find a root 'DESCRIPTION' file that starts with '^Package' in 'C:\Users\Della Rocca\Documents'.
Are you in your project directory, and does your project have a 'DESCRIPTION' file?
I can copy the server part where the problem appears:
# this makes the directory at the base of your computer.
volumes = c(Home = fs::path_home(), shinyFiles::getVolumes()())
####select the data folder where to read data
shinyFiles::shinyDirChoose(input, 'datafolder', roots = volumes, session = session)
data_path = reactive({
if(length(input$datafolder) != 1 ) {
shinyFiles::parseDirPath(volumes,input$datafolder)
}else{
NULL
}
})
stepb = eventReactive(input$readdatabttn,{
req(data_path(), targets(), analysis())
stepa = list(targets = targets(), analysis = analysis())
withProgress(message = "Reading data...", value=0, {
#external function that takes the path and reads multiple .txt files catching names from the target_file (a .xlsx file)
read_advise_lipidomics(out = stepa, datapath = data_path(), target_file = stepa$targets$targetfile_lipidomics)
})
})
data_path() it's ok because it has the correct path. The problem is when I trigger the eventReactive() with the button input$readdatabttn.
Do you know what is that error? I have the description file and running devtools::check() I have no errors and just a warning:
> checking Rd \usage sections ... WARNING
Undocumented arguments in documentation object 'calibplot_advise_lipidomics'
'plot_calibration'
Undocumented arguments in documentation object 'create_beautiful_radarchart'
'vlabels' 'title' '...'
Undocumented arguments in documentation object 'na_advise_lipidomics'
'na_filter_lip' 'na_filter_sam' 'imputation_met' 'imputation_val'
Undocumented arguments in documentation object 'read_advise_lipidomics'
'datapath'
Undocumented arguments in documentation object 'recovery_advise_lipidomics'
'intercept_flag'
Functions with \usage entries need to have the appropriate \alias
entries, and all their arguments documented.
The \usage entries must correspond to syntactically valid R code.
See chapter 'Writing R documentation files' in the 'Writing R
Extensions' manual.
I solved. The problem was that in the code I take the package version and save it in a variable. I used the function golem::get_golem_version() and this function works only in my dev project directory, in every other project (even on the same computer) doesn't work. For now I write by myself the version, but I would like to automate it.
EDIT:
I used packageVersion() with my package name inside and for now it works.

Copying files with R Package in Windows - Access is Denied - file.path()

I am attempting to fix the slidex package (converts Powerpoint [.pptx] to Xaringan) so it works on Windows. I keep hitting Access is denied errors when this line runs:
file.rename(file.path(basepath, "xml", "ppt", "media"),
file.path(basepath2, "assets", "img"))
Warning message:
In file.rename(file.path(basepath, "xml", "ppt", "media"), file.path(basepath2, :
cannot rename file 'C:\Users\rick2\AppData\Local\Temp\RtmpueUFV7/RCommunityOrg_useR2021/xml/ppt/media' to 'C:\Users\rick2\AppData\Roaming/RCommunityOrg_useR2021/assets/img', reason 'Access is denied'
I see both folders exist and the first has file. file.copy() instead of file.rename() also fails.
Now, I just discovered that adjusting every folder object with code resembling the following solves the problem (borrowed from R Markdown's code),
path <- gsub('/', '\\\\', path)
However, must I do this every time a folder is created with file.path()?
slidex package Github reference (has the file.rename() uses: https://github.com/datalorax/slidex/blob/1c282ee4b71f262605e3ee0bc6d241dac777c5ea/R/utils.R
Per #Parfait, adding:
In the aforementioned utils.R one sees:
dir.create(file.path(basepath, "assets"), showWarnings = FALSE)
dir.create(file.path(basepath, "assets", "img"), showWarnings = FALSE)
file.rename(file.path(basepath, "xml", "ppt", "media"),
file.path(basepath, "assets", "img"))
basepath is the following without the slash-conversion I added to try and eliminate the error:
"C:/Users/rick2/AppData/Local/Temp/RtmpOCoUB3/RCommunityOrg_useR2021"
file.rename() attempts to rename the first to the second here, and file. copy() also fails:
file.path(basepath, "xml", "ppt", "media")
"C:/Users/rick2/AppData/Local/Temp/RtmpOCoUB3/RCommunityOrg_useR2021/xml/ppt/media"
file.path(basepath, "assets", "img")
"C:/Users/rick2/AppData/Local/Temp/RtmpOCoUB3/RCommunityOrg_useR2021/assets/img"
I just discovered that my solution failed to copy the files so I will accordingly edit the above later.

Remove a directory in R invoking linux commands

I am writing code using R that invokes Linux commands to create a directory with multiple files which has to be deleted at the end.
I tried using file.remove(directory_name) which removed the directory only when its empty. If the directory has files, file.remove didn’t work. unlink(directory_name) didn't remove the directory.
My code:
dir.create("./dir1")
dir.create("./dir2")
............
............
............
file.remove("./dir1")
unlink("./dir1, recursive = TRUE")
file.remove() threw an error:
cannot remove file './dir1', reason 'Directory not empty'.
while, unlink() didn't remove the directory.
How do I delete a directory with files in it?
To unlink recursively, you need to pass the recursive = TRUE as an additional parameter:
This:
unlink("./dir1", recursive = TRUE)
Not this:
unlink("./dir1, recursive = TRUE")

path error for tree tagger with koRpus R package

I try to use treeTagger that I installed from here in R with the package koRpus.
library(koRpus)
tagged.results <- treetag(as.factor("salut ça va"), treetagger="manual", lang="fr", TT.options=list(path="C:\\TreeTagger\\bin\\tree-tagger.exe"))
generates the following error :
Erreur dans path.expand(path) : argument 'path' incorrect
Which I don't understand because I can see all the files in this path, which are : tree-tagger and tree-tagger-flush (application files), tag-french and chunk-french which are windows command file.
I also tried :
set.kRp.env(TT.cmd="C:\\TreeTagger\\bin\\tree-tagger.exe", lang="fr")
tagged.text <- treetag(as.factor("salut ça va"),lang="fr")
The second generates the same error
There are several issues here. First the as.factor("salut ca va") should be a file with that text in it. You are also missing a preset value inside of TT.options. You will want to put preset="fr" after the path argument. Finally the path itself should point to the root directory.
The documentation here states "TT.options
A list of options to configure how TreeTagger is called. You have two basic choices: Either you choose one of the pre-defined presets or you give a full set of valid options:
path Mandatory: The absolute path to the TreeTagger root directory. That is where its subfolders bin, cmd and lib are located."
You are pointing the path variable inside of the bin directory to the .exe file. Run the following code to point to the root directory where the bin directory is located as follows:
library(koRpus)
tagged.results <- treetag("test.txt", treetagger="manual", lang="fr", TT.options=list(path="C:\\TreeTagger", preset="fr"))

Trouble with posting slidify to Dropbox

I created a slidify slideshow, but when I am trying to drop in into my dropbox account into Public folder I get a warning:
publish("Slides", host = "dropbox")
Creating slide directory at ~/Dropbox/Public/Slides
Copying files to ~/Dropbox/Public/Slides
[1] FALSE
Warning messages:
1: In file.copy(".", drop_dir, overwrite = F, recursive = TRUE) :
'recursive' will be ignored as 'to' is not a single existing directory
2: In file.create(to[okay]) :
cannot create file '~/Dropbox/Public/Slides', reason 'No such file or directory'
I tried to create a hard symbolic link as http://thiagosilva.wordpress.com/2013/02/17/installing-slidify-on-a-windows-machine/ suggests.
But the warning is still there...
Any ideas?
Best Regards!

Resources