RStudio UI to create a table


#1

Hey there!
This is perhaps a niche pain. Sometimes, I find myself needing to quickly cook up a small dimensional table or a test table: a handful of rows, a handful of columns. Nothing serious.

Of course, it is not too hard to do, and there are many ways to get there, including my favorite tibble().

However, I find it easier to do using spreadsheets. Especially, if you go beyond a 2 x 3 table…
Now, it is madness to create a small table in Excel and let R read the file. Obviously. Table generation should stay in the script.

I was wondering is next versions of RStudio could have a simple mechanism of generating a table. Or rather generating a short tibble script with user’s input.

Here is an example of how it could work. It comes from Microsoft Power Query / Power BI, where I spend a lot of time at work… :persevere:

First, you get an interface to enter your data:

…which generates, you’ve guessed it, a table:
image

…while what happens behind the scenes is impossible to understand, but I guess it basically generates a long encrypted string, and then decodes it as a JSON string, and makes it a table

(in my imagination, in RStudio that’s where a tibble code would go)

It is faster and more intuitive, and leaves an actual script behind, and no external files. A win-win?

So, any thoughts? Too much? Unnecessary for most? Not too bad?


#2

This is a great suggestion. I often need to make quick tables in markdown format so I can add them to my R Markdown pipelines. I’ve been using a nice interactive site called Tables Generator which gives you a nice UI for making markdown tables with an intuitive interface. It looks like the site uses a few javascript and jquery frameworks, so one approach might be to wrap this in to an RStudio add-in?


#3

While it might be another great solution (especially a great one for markdown: thank you for this!), I’d rather have R stay R. Transforming user input into an R script within an R script kinda makes sense :slight_smile:


#4

Did you see this discussion?

You could do

code_for_tab <- function(){
  DF = data.frame()
  fix(DF)
  dput(DF)
}

code_for_tab()

That will open a (base R) window to edit a table. When you close the window, code to make the table will be printed to the console. Not pretty code, admittedly. This SO Q&A might help with that, if desired.


#5

What about something like:

tibble() %>% edit() %>% as_data_frame() %>% dput(file = "my_df.txt")

That would create a human-readable file that can be read in with dget. Alternatively, if you don’t need readability in the output file, saveRDS would work instead of dput.

Edit(^2?): @frank, edit calls the same interface as fix without requiring a named variable.


#6

I think tibble::tribble() is also a nice compromise for small data.

Edit, just saw the datapasta-pkg, also nice :slight_smile: https://github.com/MilesMcBain/datapasta


#7

Thanks, yup, it’s clearly a better way to go here.

I was unaware of the differences (having never used either of them).


#8

@Frank, @nick, thanks! My mind is blown!


#9

Now, can we make it a button in UI? :smile:


#10

This seems like an excellent use of a snippet:

I would suggest something like:

snippet mktbl
	tibble() %>% edit() %>% as_data_frame() %>% dput(file = "${1:name}")

Make sure to use a tab for indenting instead of spaces.