IDEMSInternational / R-Instat

A statistics software package powered by R
http://r-instat.org/
GNU General Public License v3.0
38 stars 103 forks source link

Executing commands from the script file #4381

Closed rdstern closed 2 years ago

rdstern commented 6 years ago

Having got this far with the script file I would like to go a bit further - of course - or at least understand better why we can't.

Suppose I would like to execute a new R command that is not yet available in R-Instat. The t.test command is one example and the prop.test command is another and wilcok.test is a third. They are all in the main stats package. And we can't yet do this simple inference.

I know I can go into RStudio, but I don't want to do this for just one command - and there are many users who (I strongly suggest) would like to be able to do this sort of thing in R-Instat itself.

  1. The obvious was (to me) is to execute a similar command and then edit the script file and run. (I accept the dangers!). So I used the samrain data from the Instat library and did a Model > One Variable > Fit Model dialogue. I opted to not have any graphs then got the numerical results fine, then put the code into the script file. It is quite complicated code but easy to see what to change. Nice that it gives the name of the package. So I substituted:

last_model <- stats::wilcox.test(Strt1)

for the call to the fitdistrplus package.
This runs fine, but (as Danny has told me) it produces no results in the output window. It will do graphs. So (undaunted) I went to method 2!

2) I put stats::wilcox.test(Strt1) into the calculator and opted not to store the results. This now gives just what I wanted. Yipee. The code in the script window is quite simple too.

So I can do it. Is Version 2 to be our route? Or at least one of our routes? If so then could we have another keyboard with these functions? They are designed simply to put results into the output window. If so, the one useful additional feature could be to disable the saving when that keyboard is selected. These commands simply put results into the output window. So they don't add anything to the Instat object - which the last_model line in method 1 is trying to do.

3) If we are to enhance the calculator as our route to the execution of these simple commands, then we might want to use a command from a package that is not currently in R-Instat. Currently we have to load it through RStudio. Could we have a facility to load a package through R-Instat

dannyparsons commented 6 years ago

From looking into this it is now possible to display output through the script window, and I have updated it to allow this. So now by default it expects output from every line and tries to display it. This doesn't seem to cause any issues when there is no output to display, but this should now be tested to see if there are issues. If there are, we could add a "suppress output" checkbox on the script window to prevent this.

Separately, we could also have a keyboard for these functions as long as we don't try to save this back to the data.

It is possible to install and load packages through R-Instat using the R commands, although I would still be recommending users don't do this in case of unintended consequences.

rdstern commented 6 years ago

I suggest a few more features for the script window. I do understand the dangers, but find it very useful. (David has also admitted he is now sometimes using it in preference to going to RStudio! Wow!)

  1. I like the improvements that Maxwell has made.
  2. He tells me that Undo and Redo will be easy to add in this control - I believe they sort of come automatically!
  3. Could we also have Run Current Line? And when we do this, it will usually be repeatedly, so it will ideally then go to the next line. And it would not give the warning message.
  4. Danny only wanted keyboard short-cuts for more standard things. In Genstat "Run Current Line is . That would be nice! I am happy to stop there, but they also have for Run the window and for running selected text.
  5. Could we also have a Help option. I'll find a number if so.
  6. And we are getting to a stage with the right-click that a few horizontal lines to split them up would be useful. Perhaps under the Undo/Redo. Then under the Run set. (Do we also add Run window - even though it is also at the top of the window?
rdstern commented 6 years ago

I would like a few more keyboard commands to make the use of the script window easier. It is good to be consistent with something, so here are the keyboard short-cuts for RStudio.

If only 1, then RStudio has Ctrl + Enter as the short-cut for running the current line, or the selection. Could we add that (at least for running a selection). And would it be possible that when you run with the short-cut then it doesn't give you the warning?

Maxwell has tried to explain why running the current line is not trivial to add. And also that sometimes it is a mess, because the script file should run command-by-command and sometimes commands go over several lines.

I would still find it very useful to be able to run line-by-line (when commands are on a single line - and without carefully having to select that line first. Can this action be made easier? (This action is consistent with running the script file being "flaky", because I often want to find up to which line/command it is all OK.)

dannyparsons commented 6 years ago

Finding all the text on a current line should be simple in a textbox so I don't see any problem to doing this.

Patowhiz commented 6 years ago

I agree this would really be helpful. Even for us when debugging the dialogs.

Patowhiz commented 6 years ago

I'm now working on this

Patowhiz commented 6 years ago

To be specific, I'll be solving the issues outlined in this comment

I suggest a few more features for the script window. I do understand the dangers, but find it very useful. (David has also admitted he is now sometimes using it in preference to going to RStudio! Wow!)

I like the improvements that Maxwell has made. He tells me that Undo and Redo will be easy to add in this control - I believe they sort of come automatically! Could we also have Run Current Line? And when we do this, it will usually be repeatedly, so it will ideally then go to the next line. And it would not give the warning message. Danny only wanted keyboard short-cuts for more standard things. In Genstat "Run Current Line is . That would be nice! I am happy to stop there, but they also have for Run the window and for running selected text. Could we also have a Help option. I'll find a number if so. And we are getting to a stage with the right-click that a few horizontal lines to split them up would be useful. Perhaps under the Undo/Redo. Then under the Run set. (Do we also add Run window - even though it is also at the top of the window?

Patowhiz commented 6 years ago

@rdstern @dannyparsons after having all the suggested shortcuts and the right click options, do we still need the Run All button? I think we won't need, it's taking up space

Patowhiz commented 6 years ago

@rdstern @dannyparsons to successfully complement the current Run selected text option, I'm thinking of adding Run current line (Ctrl+Enter). The Run current line will work like in RStudio, it will help eliminate the need of selecting a command to run everytime. The following are the shortcuts I think I could use, I have outsourced some from Rstudio shortcuts. Which one do you suggest I change?

  1. Run Current line (Ctrl+Enter)
  2. Run Selected Text (Ctrl+Alt+T)
  3. Run All Text (Ctrl+Alt+R)
  4. Open Script as File (Ctrl+O)
  5. Load Script from File (Ctrl+Shift+O)
  6. Save Script... (Ctrl+S)
  7. Clear Script (Ctrl+L)
dannyparsons commented 6 years ago

They look good apart from the saving and loading ones: 4,5,6. I think this will get confusing with the opening and saving of data files, they have similar shortcuts. These can be on the right click but without shortcuts.

I'm ok with 7 as a shortcut but possibly should have a warning to confirm clearing.

Patowhiz commented 6 years ago

That's how the right click menu will look like. Would you guys like me to add a Help menu item?

script window
rdstern commented 6 years ago

That looks great.

Humpf, improving the help is getting to the top of my list. I am afraid yes please. Perhaps a line and the Help item.

Patowhiz commented 6 years ago

@dannyparsons @rdstern there are 2 ways of solving the run current line depending on the how complex we want to make it. The easiest way to achieve this with less work would involve

  1. Not allowing the output window wrap the text i.e all texts in a line to fit in a line,no breaking it to the next line. This will help whenever the user reduces the size of the output window.
  2. The entire current line will be executed regardless of whether it's a command or not. This will work well with the output from the dialog, since the ToScript method of the RCodeStructure already formats our commands to line by line. However when the user types in a command and breaks it to span over 2 lines, there will be an error. That means a user will have to write or add to a command but have it strictly on a single line.

The hardest way will be to build a syntax parser that tries to determine if the current line is a complete command and if not to concatenate it with next line until when it's a full command. This will take me time.

The first alternative has restriction but it's easy to achieve at the moment, I'm looking into the second option which is how RStudio does it

dannyparsons commented 6 years ago

Currently the "run all text" runs the script line by line so if any single line is not a complete command it already doesn't work. We are not trying to build something that's a full proof code editor and parser here - that's what RStudio is for. So users will need to put a command on a single line. As you say this is the output from all our dialogs so I don't see it has a big problem and would take too long to implement.

There's a big difference between text wrapping and line breaks. The script window wraps the text so that if a line is very long it stretches over two "visible lines" on the screen but it's in fact still on the same line of the textbox. Wrapping is just a visual component and doesn't effect what we call a line of code in the script window. A line break, where the user hits return is different and should be used for a separate line of code.

I think something like this will do what's needed for this:

textbox1.Lines(textbox1.GetLineFromCharIndex(textbox1.SelectionStart))

Patowhiz commented 6 years ago

@dannyparsons what I meant is with wrapping set to true, if a user unintentionally places a cursor before the wrapped character instead of before the actual start of the line, the returned line will be the one that follows it. This may lead to a slight confusion(unexpected) on the users side.

PS. Sorry for poor demonstration. I lost my pen

wrapping
Patowhiz commented 6 years ago

@rdstern where should the help option point to ?

Patowhiz commented 6 years ago

And should I just leave the Run All button? Now that we have that option in our keyboard shortcuts and context menu?

dannyparsons commented 6 years ago

I don't think the wrapping is such a serious issue, but we could turn off wrapping if people will find it clearer.

dannyparsons commented 6 years ago

I think the Run All button is still useful.

Patowhiz commented 6 years ago

This is the final outlook for the script window right click. I'm still not sure on where the help option should point to.

script window
dannyparsons commented 6 years ago

Looks good. Help is probably disabled for now.

rdstern commented 6 years ago

During discussions Patrick mentioned that changing the fonts and colours of the text in the script window could make it clearer and more attractive. This seems a nice idea. Perhaps it could be consistent with the fonts/colours used in the output window.

I assume this isn't a big job - assuming it can be done quickly, then I like the idea. If it is consistent with the output window, then the options for changing those (in Tools > Options) would also automatically apply here.

maxwellfundi commented 6 years ago

@Patowhiz I think looks good. I suggest every item here that has a shortcut has to have it indicated at the end. I see some do not have the shortcut indicated.

Patowhiz commented 6 years ago

@maxwellfundi some don't have the shortcuts because @dannyparsons was of the opinion that

They look good apart from the saving and loading ones: 4,5,6. I think this will get confusing with the opening and saving of data files, they have similar shortcuts. These can be on the right click but without shortcuts.

I'm ok with 7 as a shortcut but possibly should have a warning to confirm clearing.

dannyparsons commented 6 years ago

A different colour for comments would be a nice feature. I think it would a bit of work to get working well whether the text was typed or pasted or imported, taking care of trailing spaces etc. And it should only be done if done properly and robustly. I would put this along with other improvements to colouring like RStudio can do, line numbers etc.

I would guess that @rdstern's other suggestions for the next few weeks would take priority over this. Either way this should be written up as an issue so we don't forget it.

image

Patowhiz commented 6 years ago

Yep. I've opened this as a separate issue #4932. Then I can do it in my spare time(in parallel with the other tasks of course). I really like it when things look nice and attractive. It makes a user feel energised to use the application. The final output for the log, script and output window will be similar to Rstudio. If running the commands in the script window is that important, then enhancing its look and feel will better suit that functionality.

dannyparsons commented 6 years ago

Sounds good. Though this will require changing the textboxes to richtextboxes most likely. Yes if you're stuck with nothing to do this would be good to work on, but likely all other tasks at the moment should be done before this.

rdstern commented 6 years ago

And one of the next challenges (which I hope you will like, is a similar sort of textbox from the File > Open dialogue see #4439. This could (eventually) be treated similarly to the script window.

maxwellfundi commented 6 years ago

@Patowhiz could this be closed then now that you opened another isssue on other things that need to be done?

Patowhiz commented 6 years ago

Probably, once the script window PR is merged

lilyclements commented 2 years ago

Probably, once the script window PR is merged

@Patowhiz this was a comment from 2018. Did merging that PR resolve the issues in this issue?

Patowhiz commented 2 years ago

@lilyclements not yet. After going through the discussions. There are still items on this issue that need to be addressed.

lloyddewit commented 2 years ago

@rdstern @Patowhiz I think all the items in this PR have been fixed, are OTBE or are reflected in issue #7632. I think it will be easier to manage the script window enhancements if we close this issue. If you think there are additional enhancements/fixes in this PR not covered elsewhere, then please could you add them to issue #7632 ? Thanks

Patowhiz commented 2 years ago

@lloyddewit agreed.