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

Add alignmment and wordwrap options to the grid? #7812

Closed rdstern closed 1 year ago

rdstern commented 2 years ago

I assume this might add to an entry in the toolbar and also possibly in the Edit menu.

With some character fields an entry can become very wide. I have also an extreme situation with the files supplied in #7811

The Excel file has a wide column (very wide - over 2500 characters). I would like the Excel options of being able to look at it left-justified, (possibly centred) or right-justified. I like the feature (also in Excel) of double-clicking at the end to make it full width. But then it is awkward to try and get it back to a more reasonable width. I don't know if there is a way to improve on this.

Word wrap is in Excel, and is also in R. It is under Prepare > Column: Text > Transform > Wrap. It works well in that it wraps in the grid, but it doesn't unwrap!
An example is the data in the library in the package statquotes. I used wrap 25 and later 70. The grid still keeps the setting of the wrapping from the smallest that was used. It doesn't unwrap the cell depth, though the text does. image

rdstern commented 2 years ago

@N-thony I assume alignment and word-wrap (perhaps unlike Excel) only apply to whole variables (columns). Alignment could be for multiple variables, while word-wrap will only be for a single variable in each data frame. I assume there will be entries in the Edit menu and perhaps an additional item, with a pull-down, in the toolbar? I am not so keen on adding to the right-click menu for these.

I describe 2 datasets where I would like an alignment option and also a word-wrap option. a) File > Open from Library > statquotes. This has a variable which is up to 700 characters. So both changing alignment and word-wrap would be useful. After wordwrap we need to be able to unwrap. b) File > New data frame > 1000 rows and x1 = 1,1000. Then use the Prepare > Calculator > Integer keyboard and Factorial to give the factorial of all numbers up to 1000!. This is huge - over 2000 places, so very wide and (unlike words) with no spaces. So when no spaces in blocks of the width specified, then it simply word-wraps, splitting the text at the specified width.

I assume wordwrap (unlike Excel) could perhaps just be for the current variable and (for simplicity) it would only be for 1 variable in a dataframe. If another is chosen , then first it is turned off if it is in wordwrap mode. If multiple variables are selected, then the word-wrap option may be disabled.

This option could also solve the problem of a very wide variable. Currently you can click and make a variable the full width. It is difficult then to return it to a reasonable width. We could now do this via word-wrap.

There are other issues on smaller limitations and problems in reogrid. Perhaps they can be tackled together, but I expect multiple pull-requests to be merged separately. They include #7811, #7765, #7754, #7697, #7630, #7349.

rdstern commented 2 years ago

I have looked a bit more at these 2 aspects of alignment in the data frame and also the possibility of a better wordwrap. I am a bit confused on what we should do here. I am no longer sure about the alignment. Also perhaps the wordwrap we have could be extended more easily and that would be enough. Could we use the statquotes data to improve the grid behavior.
So we have wordwrap, but there are problems with its current use. a) The current dialogue has a limit of 100. That needs to go. b) There is a simple version of the dialogue, which changes the existing variable, rather than writing a new one. c) We can then effectively take off the wordwrap, byt making the line length vary wide. d) But then currently the grid doesn't "close down" the line depth stays what it was. This is just a change needed in the reogrid, and presumably nothing to do with R? There is another issue on this problem. I hope that @Patowhiz could advise on this. See also #7765, where there is a similar problem. e) Could there also be a way of dictating the width of a variable. We can easily expand a variable to the full width by double-clicking on the right at the top of the variable. Then we get a solid line and 2 arrows. Once at full width was can make it smaller, but only by dragging. Could there be an option to return it to the default width. (I don't think there is an option to shrink in Excel?) Perhaps that could be a similar double-click, or maybe a simple dialogue, again in the edit menu? This would do nothing to wordwrap examples where there is no space - like very large numbers. I suggest we ignore that problem.

rdstern commented 2 years ago

And on alignment there is one change we should make. That is to left-align numeric variables - that are currently centred. Here is an example: image

The numbers are centred in R-Instat. They are left justified in the R-viewer.
They are a calculation 2^x1-1 with x1 being 1 to 100 in this file.

lilyclements commented 2 years ago

Unwrapping data set:

We can unwrap using the str_replace_all function in the stringr package. When we do the str_unwrap function, \n is added where we want new lines. To remove these, we just run str_replace_all.

E.g.:

# Setting working directory, sourcing R code and loading R packages
setwd(dir="C:/Users/lclem/source/repos/R-Instat/instat/bin/Debug/static/InstatObject/R")

source(file="Rsetup.R")

data_book <- DataBook$new()

options(dplyr.summarise.inform=FALSE)

# Option: Number of digits to display
options(digits=4)

# Option: Show stars on summary tables of coefficients
options(show.signif.stars=FALSE)

# Code generated by the dialog, New Data Frame
data1 <- read_corpora(data=rcorpora::corpora("governments/uk_political_parties"))
data_book$import_data(data_tables=list(data1=data1))

# Code generated by the dialog, Transform Text Column
list <- data_book$get_columns_from_data(data_name="data1", col_names="list", use_current_filter=FALSE)
list_transformed <- stringr::str_wrap(string=list, width=10)
data_book$add_columns_to_data(data_name="data1", col_name="list_transformed", col_data=list_transformed, before=FALSE, adjacent_column="list")

# Then to undo the string unwrap:
list_transformed <- data_book$get_columns_from_data(data_name = "data1", col_names = "list_transformed")
list_transformed <- stringr::str_replace_all(list_transformed, "\n", " ")
data_book$add_columns_to_data(data_name="data1", col_name="list_transformed", col_data=list_transformed, before=FALSE, adjacent_column="list")
lilyclements commented 2 years ago

@rdstern can you explain a bit more how you get the View data frame up? For me, I'm unable to replicate the bug.

image

N-thony commented 2 years ago

Unwrapping data set:

We can unwrap using the str_replace_all function in the stringr package. When we do the str_unwrap function, \n is added where we want new lines. To remove these, we just run str_replace_all.

E.g.:

# Setting working directory, sourcing R code and loading R packages
setwd(dir="C:/Users/lclem/source/repos/R-Instat/instat/bin/Debug/static/InstatObject/R")

source(file="Rsetup.R")

data_book <- DataBook$new()

options(dplyr.summarise.inform=FALSE)

# Option: Number of digits to display
options(digits=4)

# Option: Show stars on summary tables of coefficients
options(show.signif.stars=FALSE)

# Code generated by the dialog, New Data Frame
data1 <- read_corpora(data=rcorpora::corpora("governments/uk_political_parties"))
data_book$import_data(data_tables=list(data1=data1))

# Code generated by the dialog, Transform Text Column
list <- data_book$get_columns_from_data(data_name="data1", col_names="list", use_current_filter=FALSE)
list_transformed <- stringr::str_wrap(string=list, width=10)
data_book$add_columns_to_data(data_name="data1", col_name="list_transformed", col_data=list_transformed, before=FALSE, adjacent_column="list")

# Then to undo the string unwrap:
list_transformed <- data_book$get_columns_from_data(data_name = "data1", col_names = "list_transformed")
list_transformed <- stringr::str_replace_all(list_transformed, "\n", " ")
data_book$add_columns_to_data(data_name="data1", col_name="list_transformed", col_data=list_transformed, before=FALSE, adjacent_column="list")

@lilyclements thanks for your code, I noticed that unwrapping the grid cells maintains the width rather than returning to the default cells width before wrapping, **Wrap** image

**Unwrap**

image

@rdstern is this okay?

rdstern commented 2 years ago

I think that's fine. I am more concerned that the grid should go back to the original height if it can, so if no variable is wrapped?
And I would also like to be sure there is an easy way that a very wide column (say 2000 characters - or even 500 - like the statquotes) can be made narrower, without having to do it manually. I think you can make it full width by double-clicking on it. But how then could you reduce it to a narrow default width. Ideally - if it is full width could I double-click again? What happens in Excel - I think it may be a pain there too?

N-thony commented 2 years ago

Unwrapping data set:

We can unwrap using the str_replace_all function in the stringr package. When we do the str_unwrap function, \n is added where we want new lines. To remove these, we just run str_replace_all.

E.g.:

# Setting working directory, sourcing R code and loading R packages
setwd(dir="C:/Users/lclem/source/repos/R-Instat/instat/bin/Debug/static/InstatObject/R")

source(file="Rsetup.R")

data_book <- DataBook$new()

options(dplyr.summarise.inform=FALSE)

# Option: Number of digits to display
options(digits=4)

# Option: Show stars on summary tables of coefficients
options(show.signif.stars=FALSE)

# Code generated by the dialog, New Data Frame
data1 <- read_corpora(data=rcorpora::corpora("governments/uk_political_parties"))
data_book$import_data(data_tables=list(data1=data1))

# Code generated by the dialog, Transform Text Column
list <- data_book$get_columns_from_data(data_name="data1", col_names="list", use_current_filter=FALSE)
list_transformed <- stringr::str_wrap(string=list, width=10)
data_book$add_columns_to_data(data_name="data1", col_name="list_transformed", col_data=list_transformed, before=FALSE, adjacent_column="list")

# Then to undo the string unwrap:
list_transformed <- data_book$get_columns_from_data(data_name = "data1", col_names = "list_transformed")
list_transformed <- stringr::str_replace_all(list_transformed, "\n", " ")
data_book$add_columns_to_data(data_name="data1", col_name="list_transformed", col_data=list_transformed, before=FALSE, adjacent_column="list")

@derekagorhom can you implement this? Let me know if you face a problem.

N-thony commented 2 years ago

@derekagorhom in the options group box you can have two radio buttons, wrap and unwrap, the default is wrap which does what the dialogue is doing now, and unwrap will just run the code proposed by @lilyclements above. so when you click wrap it will display the nud control to enter the width. @rdstern what do you think? image

rdstern commented 2 years ago

I was thinking that this wrapping/unwrapping - like the alignment might go elsewhere - possibly in the Edit menu. Then we would delete the wrap option from that dialogue. For reference I also note the suggestion from David that alignment should become part of the column metadata?

derekagorhom commented 2 years ago

I was thinking that this wrapping/unwrapping - like the alignment might go elsewhere - possibly in the Edit menu. Then we would delete the wrap option from that dialogue. For reference I also note the suggestion from David that alignment should become part of the column metadata?

@rdstern please does this mean we are to create a new dialogue for wrap/unwrap?

Patowhiz commented 1 year ago

@derekagorhom, he means we have it as a menu item under the edit menu. And also include it as a column metadata. Then once that is done, we can remove the option from the Transform Text Column dialog. @N-thony including this as part of the column metadata, may require some R data book additions.

N-thony commented 1 year ago

@derekagorhom he means we have as a menu item under the edit menu. And also include it as a column metadata. Then once that is done, wee can remove the option from the Transform Text Column dialog. @N-thony including this as part of the column metadata, may requires some R data book additions.

@Patowhiz yes, sure.

N-thony commented 1 year ago

@rdstern can you explain a bit more how you get the View data frame up? For me, I'm unable to replicate the bug.

image

@lilyclements I think @rdstern just run the following code in the script window data1 <- data_book$get_data_frame(data_name = data1) View(data1)

lilyclements commented 1 year ago

@rdstern @N-thony - when I run that code, I can view my whole data frame. It is quite possibly to do with a setting I have put in R?

What value comes up if you run options()$scipen? I assume it will be 0, the default.

options(scipen = 0)
ran <- c(1.810032e+09, 4)
ran

options(scipen = 999)
ran

To avoid this we want to set scipen to be larger.

with help from stack overflow here

N-thony commented 1 year ago

@lilyclements I suspect now this issue is from Reogrid which doesn't allow data with exponential notation. I'm investigating this with the Reogrid documentation if we can allow these kind of data. You can try to enter for example 1.810032e+09 in any cell and it will remove the exponential notation.

rdstern commented 1 year ago

@N-thony the grid does allow e-notation for a variable. I think you organised and simplified the Scientific option in the column metadata. There, if you change the option to TRUE the variable will be in e-notation. In presentation, we don't have a fixed number of decimals yet, but we do have scientific. We don't (yet?) have any options that some numbers in a variable could be in scientific and others would not.

N-thony commented 1 year ago

2^x1-1

@rdstern yes, I got it, I will investigate now if it is possible that some numbers in variable could be in scientific and others not image

derekagorhom commented 1 year ago

@lilyclements and @rdstern do you have any R code for implementing alignment options also, i,e to implement left-align, right-align and center for column variables in the datasets.

Unwrapping data set:

We can unwrap using the str_replace_all function in the stringr package. When we do the str_unwrap function, \n is added where we want new lines. To remove these, we just run str_replace_all.

E.g.:

# Setting working directory, sourcing R code and loading R packages
setwd(dir="C:/Users/lclem/source/repos/R-Instat/instat/bin/Debug/static/InstatObject/R")

source(file="Rsetup.R")

data_book <- DataBook$new()

options(dplyr.summarise.inform=FALSE)

# Option: Number of digits to display
options(digits=4)

# Option: Show stars on summary tables of coefficients
options(show.signif.stars=FALSE)

# Code generated by the dialog, New Data Frame
data1 <- read_corpora(data=rcorpora::corpora("governments/uk_political_parties"))
data_book$import_data(data_tables=list(data1=data1))

# Code generated by the dialog, Transform Text Column
list <- data_book$get_columns_from_data(data_name="data1", col_names="list", use_current_filter=FALSE)
list_transformed <- stringr::str_wrap(string=list, width=10)
data_book$add_columns_to_data(data_name="data1", col_name="list_transformed", col_data=list_transformed, before=FALSE, adjacent_column="list")

# Then to undo the string unwrap:
list_transformed <- data_book$get_columns_from_data(data_name = "data1", col_names = "list_transformed")
list_transformed <- stringr::str_replace_all(list_transformed, "\n", " ")
data_book$add_columns_to_data(data_name="data1", col_name="list_transformed", col_data=list_transformed, before=FALSE, adjacent_column="list")
rdstern commented 1 year ago

@derekagorhom I also wonder whether you need R code for the alignment options to be able to do it in reogrid?
Perhaps you could check with the right-click at the bottom - and View data-frame on the R viewer for the data frame, to see whether this has the same alignment as reogrid, for example for numeric variables that are centred in reogrid? However, @lilyclements it does link to the R-Instat data book, because I assume we would need the column metadata to be able to specify the alignment? This may become a @dannyparsons question

lilyclements commented 1 year ago

@rdstern @derekagorhom I think that the alignment is to be done in the reogrid options, not in R.

Any functions I find that can do it in R change the class of the variables (or data frame).

E.g., from stackoverflow

x <- data.frame(c("Thing1", "Thing2", "Thing3", "Thing4", "Thing5"),
                c("Sizeable line of text", "more text", "I hope this aligns", "something", "help me"))
colnames(x) <- c("T", "Text")
class(x$Text)
x <- format(x, justify = "left")
class(x$Text)
# Class is now AsIs, not a character
x <- format(x, justify = "centre")
x
class(x$Text)
# Class is now AsIs, not a character

The above format options don't work for if we have numerical variables. E.g.

x <- data.frame(c("Thing1", "Thing2", "Thing3", "Thing4", "Thing5"),
                c("Sizeable line of text", "more text", "I hope this aligns", "something", "help me"),
                9:13)
colnames(x) <- c("T", "Text", "numeric")
format(x, justify = "left")

From a look up, it seems this is a reogrid option, not an R option.

N-thony commented 1 year ago

@derekagorhom how is it going? Since we can not handle the alignment with R, I think we can get rid of the alignment control from then dialogue, and think of the alignment option later and this can be merged as it is now.

rdstern commented 1 year ago

@Patowhiz (and maybe @derekagorhom ) I wonder if I have an example here of the need to have wordwrap working better.

I import a gadm shapeful - I can send them if you like, but don't want to share them on github. They are easy to find. The example here is from Malawi, but the problem seems to be general.

I import with the Climatic > File > Import Shapefile and it looks fine: Here it is:

image

I now save it as an rds file and then open it again. Now it looks like this (it still seems to work fine.

image

(The problem is the geometry variable which is long - look here:

image

My guess is that this may be a @Patowhiz or a @ChrisMarsh82 puzzle?

It would be good to solve, because then we could save the shape and station files together and make the mapping process much easier than before.