How to "save as" programmatically in R? - r

I've got a Shiny app that produces a product that I'd like my users to be able to save in whatever location they'd like to save it and with whatever name they'd like. In order to do this, I'm currently using file.choose to allow the user to choose a location and file name. On Windows, if it's a new file, the user will be prompted with a message saying this file does not exist and asks if it should be created. On Ubuntu, I believe it was the same outcome. However, this option doesn't work on Mac as there's no way to enter in a new file name and an existing file must be chosen. Additionally, the dialog box says "Open" as opposed to "Save", which is confusing to some users.
Is there a way to trigger the "Save As" dialog box that opens when you save a new file? save, write, and all other functions either come with a predefined file name or expect one to be passed.
Edit
I've come across some questions on SO that attempt to address this with the tcltk package, but I'm not sure I like this solution since there seem to be some issues on Mac OSX with this package. Others recommended using choose.files as there are some customization options you can pass, but this is Windows specific. I'm hoping for a cross-platform option that is intuitive.

Related

Creating a file with write.table and file.choose on Mac

I am a PC user and have used the write.table (and write.csv) extensively.
I am teaching students how to use R, but some of them use a Mac.
On a PC, the following code:
write.table(mydata, file.choose(), row.names=F, col.names=T)
opens up a browser window, allowing the user to specify the folder where to save the new file, and specify the file name and extension. (Strangely enough, the user is then prompted to click the button "Open" to create a file.)
On a Mac however, it appears there is no way of specifying the name and extension of the file being created, once the window opens. In other words, there does not seem to be a way to actually create a file.
Am I missing something?
Is the solution simply to replace the "file.choose()"
with the full path to the new file?
LGTM:
Having said that, if your student is using RStudio then you would normally need to go to their github repo and file an issue but I've already done that https://github.com/rstudio/rstudio/issues/3903. They seem to intercept the call and pass it to Qt which is not honoring it (it's not a macOS issue but a Qt issue).

How to access the script/source history in RStudio?

I would like to access the history of what have been typed in the source panel in RStudio.
I'm interested in the way we learn and type code. Three things I would like to analyse are: i) the way a single person type code, ii) how different persons type code, iii) the way a beginner improve typing.
Grabbing the history of commands is quite satisfying as first attempt in this way but I would like to reach a finer granularity and thus access the successive changes, within a single line in a way.
So, to be clear, I'm neither looking for the history of commands or for a diff between different versions of and .R file.
What I would like to access is really the successive alterations to the source panel that are visible when you recursively press Ctrl+Z. I do not know if there is a more accurate word for what I describe, but again what I'm interested in is how bits of code are added/moved/deleted/corrected/improved in the source panel but not necessary passed to the Console and thus absent from the history of command.
This must be somewhere/somehow saved by RStudio as it is accessible by the later. This may be saved in a quite hidden/private/memory/process/... way and I have a very vague idea of how a GUI works. I do not know it if would be easily accessible, then programmaticaly analyzed, typically if we could save a file from it. Timestamps would be the cherry on top but I would be happy without.
Do you have idea how to access this history?
RStudio's source panel is essentially a view to an Ace Editor. As such you'd need to access the editor session's editSession and use getDocument or getWordRange along with the undo of the editSession's undoManager instance.
I don't think you'll be doing that from within RStudio without hacking on the RStudio code unless the RStudio Addin api is made to pass-thru editor events in the future.
It might be easier to write a session recorder as changes are made rather than try to mess with the undo history. I imagine you could write an Addin that calls a javascript to communicate over the existing RStudio port using the Ace Editor's events (ie. onChange).
As #GegznaV said, RStudio saves code history to a ".RHistory" file. It's in the "Documents" folder on my computer. It's probably the same folder if you're using Windows. If you do not know the location, you can find the file by searching.
It also allows saving RStudio history to a file manually. There is a "Save" button in the History panel. So you can also get a timestamp. You can ask different users to save their code history after they have finished writing code. It may be indirectly useful to your research.

Is there an Rstudio keyboard shortcut to open up the file that contains the source code to a function you've written?

I have multiple packages of my own making that I usually have loaded into my R sessions, as well as various functions specific to a small project stored in various utils files. Say I know the name of a function but want to open the particular file housing that function, for reading & debugging purposes. In pyCharm, for example, you can just select the name of that function and press ctrl-b. Is there any sort of keyboard shortcut or function to find (and ideally autmatically open) the file / line that contains the definition of my function of interest?
Thanks!
If you are within a package then F2 will navigate to the source file of functions defined within that package (it would be nice if you could also go to other packages but that doesn't work yet). You can also use Ctrl+. to do a typeahead search of all functions in the package (and navigate from the list).
The only soltuion that I am aware of is that you can select a function name in RStudio (it is actually enough to place the curser somewhere inside the function name) and then press F2. This will open up a tab called Source Viewer in the source pane, where you can look at the function definition. It does not, however, open the file where the function was defined. This means that you can not edit and save the function.
I don't know for sure that there is no funcionality to open the file where the function is defined, but I have good reason to suspect that there is not. When you source a script, the R expressions in that script are evaluated. If it contains function (or variable) definitions, these are stored in memory and will be available in the R session for further use. These R objects do not know where the code that defined them is stored (or whether they were just defined from the command line), so I don't see an immediate way how RStudio could know, where to look for the file containing the definition and open it.
In case you are looking for shortcut to access file, press CTRL key and double click the file name to get it opened.

Anyone know how to set Notepad++ as default text editor for SAS, Stata and R?

Anyone know how to set Notepad++ as default text editor for SAS, Stata and R?
I'm having the hardest time finding anything online (particularly for SAS). Looking for ability to run code from Notepad++ as well as setup color coding.
Thanks.
Why not just go the GUI route: right click, e.g., any foo.R file and select "Open with...", then pick Np++ and click the "always open this file type.." button. Rinse, lather, repeat.
BTW, for R, if you don't already have it, get "NppToR" , a little app which lets you execute code directly from the Npp window.
On windows you can either:
manually edit the keys for the file type to set the value of HKEY_CLASSES_ROOT\FILE_TYPE\shell\edit\command where FILE_TYPE is the appropriate one for each of the files extensions you wish to change this is quite hard work or
you can use a program that does it for you such as the free Default Programs Editor
In either case the first thing to do is to back up the registry.
I would seriously suggest looking into using one of the many, some free IDEs as they will automatically include the run from edit environment, syntax highlighting, code completion and, in many cases, debugging as well.
Notepad++ is my favorite editor and I was in the same shoes as you are now.
I am assuming that you are in Windows (Notepad ++ doesn't exist in Mac, saddest thing for me)
Please follow the steps mentioned in the below webpage and do as the author says.
http://hafniumcity.com/notepad_color.php
Now for in-line editing , it may be not be the best choice for SAS. Although I haven't tried it much so not a good person to comment on it.

Automatically select a submenu in excel

I'm trying to automate some excel processing. With autoit I open several Excel with an add-in. This add-in is used to update data in this excel.
In autoit I managed to open the excel files with the add-in but now I need to select the menuitem to update the excel automatically. I don't have any clue howto do this in autoit3.
I can't find any tutorials or manual howto do this.
It sounds like you would be better off with an Excel macro instead. If you click "tools>macros>record new macro" and set the name of that auto_open then select the cells you want to update and update them and then stop recording, you will have a macro that refreshes the cells every time you open them. If you want a separate file that refreshes all the files you can make a new spreadsheet and start recording and use file open to have opening (or closing) a spread sheet included in the code. Don't forget to have that macro save too.
PS- If you need to open the file without the macro running you need to open excel first then hold shift when you open the file using the "file>open" path.
If you know the keystroke(s) for the menu command, you can use the AutoIt Send function.
For example, the Save function in Excel is CTRL+s.
In AutoIt, that would be:
Send("^s")
I see from your other question on StackOverflow that you are using the ExcelCOM library. You must realize that the MenuItem you are trying to click does some operation on the underlying code in the same way as the ExcelCOM library.
The best solution for your problem is to find out which methods the MenuItem calls, and then reproduce the same behavior with the ExcelCOM library. What you mean by "Update the excel automatically" I don't understand, otherwise I would have found out how to reproduce the behavior by ExcelCOM for you.
If you're using excel 2007 (I am not sure about earlier versions) and higher there is a combination of letters you can use in queue to call (probably) every menu option you want. When you have the excel open - press and release Alt button and you will see letters below main menu options, like in the picture, http://img.photobucket.com/albums/v479/vaeryn/Excel1_zps98c2f450.png Then letter by letter you may dig down the menu option you wish.

Resources