Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve parsing of multi-line R script #6744

Open
2 tasks done
lloyddewit opened this issue Sep 8, 2021 · 8 comments
Open
2 tasks done

Improve parsing of multi-line R script #6744

lloyddewit opened this issue Sep 8, 2021 · 8 comments

Comments

@lloyddewit
Copy link
Contributor

lloyddewit commented Sep 8, 2021

PR #6733 processes multiple lines of code in the 'Try' window. This PR works well for most cases. However, more sophisticated R parsing is available in the RScript library. I plan to create a NuGet package from this library in the coming months, and add it to R-Instat. I should then:

@lloyddewit lloyddewit added the bug label Sep 8, 2021
@lloyddewit lloyddewit self-assigned this Sep 8, 2021
@rdstern
Copy link
Collaborator

rdstern commented Sep 8, 2021

That sounds great.

@lloyddewit
Copy link
Contributor Author

I expanded this issue to include the proposal in PR #7610 :

RStudio has a good approach. They have an editor and a console. The user can type commands directly into the console (or paste/ctrl-enter commands from the editor). The console executes the lines sequentially. If the statement is incomplete then it changes the console prompt from '>' to '+'. If the user presses 'escape', then it clears the buffer and resets the prompt to '>'.

image

We could consider replacing the script window with a console window, and then using RScript to cover the whole R syntax. R-Instat's console window should then be nearly as good as the one in RStudio (we may omit some of its advanced features). I estimate that this would take around 40 hours including the changes to the RScript library (this is a very rough initial estimate).

@rdstern
Copy link
Collaborator

rdstern commented Jul 14, 2022

@lloyddewit this is an exciting advance. When you made the recent change (to include brackets in a multiline command) I also checked that this change also worked in the Edit > Script dialogue. I am assuming the new console may make this dialogue obsolete, but I wonder if it may still have a role, because the scripts needed will often have a particular structure, that is made easy through this dialogue, namely:
a) Using particular packages
b) Getting data from an R-Instat data book
c) Doing something
d) Saving results back into the same data book, either adding to the data sheet, or maybe adding another data sheet into the data book
e) Tidying up - mainly deleting unneeded objects.

@lloyddewit
Copy link
Contributor Author

@rdstern
RStudio has both a console window and an editor window. I think we need the same.

Initially, we can leave the R-Instat Edit > Script dialog functionality unchanged. If the user clicks the 'To Script' button, then the contents of the editor shall be sent to the new console window. The console shall then process the script just as if it was typed in line by line by the user.

In the future, we could enhance the Edit > Script dialog to allow single lines or selected text to be sent to the console.

I suggest that we look together at how the RStudio editor and console interact and clarify if/how we should implement this in R-Instat.

Do you have any time tomorrow morning (or next week)?

Thanks

@rdstern
Copy link
Collaborator

rdstern commented Nov 13, 2022

@lloyddewit Here is a script - well a function from the DescTools package documentation, function Fibonacci. It gives an error.

# Fibonacci numbers without iteration
fibo <- function(n) {
phi <- (sqrt(5) + 1)/2
fib <- (phi^(n+1) - (1-phi)^(n+1)) / (2*phi - 1)
round(fib)
}
fibo(30:33)

When I put it into the script window - development version - the function part. It gives an error and says:

image

@rdstern
Copy link
Collaborator

rdstern commented Nov 23, 2022

While I am thinking about it, I list some examples of sample scripts that could be well documented and available, for teaching purposes, and to facilitate tasks that are not so simple directly from the dialogues. These are just to record them and this does not affect the timing/priority of these improvements in R-Instat.
a) We have a tweaking guide - and tweaking dialogues, i.e. starting with a dialogue and then editing it slightly, will remain a key area. This will give examples where the starting point is a dialogue (or more dialogues), rather than a sample script file.
b) An example from there is the skew boxplot. Of course we might have a complete script for those who prefer to start from a script.
c) The example from the Text-Mining with R book, by Silge. This is neat R to read the 6 Jane Austen books and add information, including chapter headings for each book. It is an example to automate the tidying of the data.
d) Simple example of random sampling using the wakefield package. I give an example in the File > New Data Frame. There is a keyboard in the calculator, which works well, but I don't see how to use it easily to give variables containing missing values.
e) Examples that add "looping" when needed. I hope we will soon be able to do some of this with our new select objects in R-Instat dialogues, but it should be simple to repeat operations on similar variables, e.g. labelling factors, or columns generally, or analyses, using scripts designed for that purpose.
f) Examples of "topping and tailing". Taking a general script and adding the way of getting data from a data book, and writing results/data back into a data book. This links to one of my jobs, namely to document the R-Instat functions that do that.
g) Crop simulation modelling in R-Instat. This is being done by Sophie using RStudio. An example of the way this can then be done easily in an R-Instat script.
h) Same for our work on processing climatic data. Possibly adding homogen could be an example, but using Danny's scripts in R-Instat will be very useful.
i) More to come!

@lloyddewit
Copy link
Contributor Author

lloyddewit commented Sep 30, 2023

Note:

PR #8551:

  • Runs the Fibonacci numbers script above correctlt
  • Resolves the tasks in the opening comment above

@rdstern
Copy link
Collaborator

rdstern commented Sep 30, 2023

Great! I am also reminded of this function, which I like, because we use it in the calculator practical.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants