Community Spring Cleaning week is here! Join your fellow Maveryx in digging through your old posts and marking comments on them as solved. Learn more here!

Alteryx Designer Desktop Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Desktop and Intelligence Suite.
SOLVED

Create a Directory using the R Tool

Korri
7 - Meteor

I have been doing some file manipulation via Alteryx and when it became apparent that there wasn't an easy way to create multiple directories if they don't already exist, and to then copy the appropriate files to both an archive destination and the production destination.  I wanted to reach out to inquire about a method to do this via the R Tool and not through the use of a .bat file.  The reason .bat files via the Run Command won't work is due to security constraints within my environment.  Here is what I am attempting to  do:

 

Each Month I receive 2 files from a vendor with a unique naming convention that I can parse through via Alteryx to create the date range of the data within the file and the name of my archive folder.

 

For these two files, each has a different destination directory, which is an archive folder.  

 

Also, for those same 2 files, I need to rename them to two different names and then MOVE them to a production folder.  

 

 

I have used RStudio to run the exact code that I need for this to work and it runs smoothly.  When I attempt to do this in Alteryx itself, I get errors or I don't get an error and it seems to work but truly doesn't.  I read my 2 rows into the R tool as a list and have attempted to use lapply(x, dir.create), which works in RStudio yet not in Alteryx (R Tool).  I have also done this not using lapply and via just a dir.create(unlist(x)) and a few other variations that work in RStudio yet not in Alteryx despite accounting for the fact that my input from Alteryx is handled as a list.  Any thoughts on how to handle this?  

6 REPLIES 6
MarqueeCrew
20 - Arcturus
20 - Arcturus

@Korri,

 

I understand the objection to the .bat file not working.  But I was wondering if a command solution might still be possible.  If you use the Run Command and pass it an argument to create the directory, will that work?  I am attaching a sample run command workflow.

 

Capture.png

 

As a Read Result, I have a dummy file that literally contains the word dummy on line 1 and line 2.  With just this one tool on the workflow I can create the subdirectory.  If this is a macro, you can update the directory as part of the configuration.

 

Please give it a try.

 

Cheers,

Mark

Alteryx ACE & Top Community Contributor

Chaos reigns within. Repent, reflect and restart. Order shall return.
Please Subscribe to my youTube channel.
JordanB
Alteryx
Alteryx

Hi @Korri

 

Can you please make sure you have used our install R libraries app to add the lapply in the right location for Alteryx to reference within a workflow.

 

Best,

 

Jordan Barker

Solutions Consultant

Korri
7 - Meteor

@JordanB,

 

Thank you for the insight!  Fortunately, the functions that I have been using with R are base components of R.  The lapply, file.copy, unlist, and dir.create are all a part of the base installation.  These are the only arguments I am attempting to utilize for this exercise.  Can you think of any reason as to why I would get the hangups that I have had attempting to use base packages?  

 

Thanks again!

Korri
7 - Meteor

@MarqueeCrew,

 

I think the Command line solution might be a feasible method.  I am getting no security errors, which works well for me.  I will have to spend some time refreshing myself on command line and check the forums for a good way to pass dynamic parameters from my Alteryx flow into it correctly before being able to call this one finished.  I suppose this is a good opportunity to create a macro as well.  As I re-engineer this to accommodate the Run Tool, I will reach back out if I get any issues on my end.  

 

It may not be through R, but if it works cohesively, I will roll with it.  

 

Thanks,

suli
9 - Comet

Hi,

 

I have a macro (using command line) to create folders dynamically if they do not exist. It was created to when using 'take file/table name from field' output option.

 

Simply put it before output tool, point column containing directory (without file name). Also need to point out a directory to save working file.

 

Let me know if it helps.

 

 

Korri
7 - Meteor

Thanks for the additional thoughts on this.  I ended up taking a blended approach using the thoughts throughout this thread.  I created a process which systematically built an "If not exist" statement for all of the files within the source folder and fed those into a similar macro to yours, albeit a smidge simpler.  With the code being generated via formula tools, I am able to do a check to ensure that I am not creating the same directories with the "if not exist" statements, as well as via the macro, I am able to use command line (cmd.exe, not a batch file) to process each statement systematically and quickly.  

 

Overall, it works like a charm and I don't get errors if it the folder doesn't exist.  I wanted to do this via R, but this seems easier to execute on and share with others.

Thanks again for the insight and assistance to each and every one of you!

Labels