input data into a matrix cell by cell

1,240 views
Skip to first unread message

Patrick Toche

unread,
Nov 9, 2013, 11:45:53 AM11/9/13
to shiny-...@googlegroups.com
Dear all,

Is there an example of a shiny app where the user can enter data into a matrix or table manually, cell by cell.

I have tried to place a uiOutput(input$cell) inside a data.frame inside renderTable in server.R, where

   output$cell <- renderUI({
      numericInput("cell", label = "Input", value = 0)
    })

but clearly there is more to it than that.

I could post my (lame) attempt on request.

Thanks,

Patrick.

Aaron Horowitz

unread,
Nov 9, 2013, 12:08:19 PM11/9/13
to shiny-...@googlegroups.com
Have you looked into the matrixInput function within the shiny-incubator package that Joe created?

Install instructions:
library(devtools)
install_github("shiny-incubator", "rstudio")

Example:
Code - https://gist.github.com/3960092
Run - shiny::runGist('https://gist.github.com/3960092')

Patrick Toche

unread,
Nov 9, 2013, 1:00:52 PM11/9/13
to shiny-...@googlegroups.com
That's exactly what I mean. Thanks a lot Aaron!

I hadn't come across it in my searches. I'll get started on my project right away.

Patrick Toche

unread,
Nov 9, 2013, 2:37:50 PM11/9/13
to shiny-...@googlegroups.com
A follow-up question, if I may:

I'd like to display column headers together with matrixInput, any idea how to do that?

In the following:

    matrixInput(inputId = 'data', label = 'Add/Remove Rows', data = df)

the colnames of data frame df are ignored.

Also how to display the table in the sidebarPanel to the mainPanel? The idea would be, say, to have 2 columns in the sidebarPanel and 3 in the mainPanel, where the third column would contain computations of some sort, e.g. the sum of the rows.

My approach was to try to copy the matrixInput to a data frame for further manipulations and display. But I wasn't able to "capture" the matrixInput.

I tried:

    output$table <- renderTable({data}

where 'data' is the 'inputId' of 'matrixInput'.

I tried also with the 'outputId' of the 'table'

some failed attempts are here:

runGist("https://gist.github.com/anonymous/7388846")

Any suggestions welcome

Thanks!

Stéphane Laurent

unread,
Nov 9, 2013, 4:35:14 PM11/9/13
to shiny-...@googlegroups.com
According to the usual syntax this should be renderTable({ input$data }), no? 

Patrick Toche

unread,
Nov 10, 2013, 3:12:25 AM11/10/13
to shiny-...@googlegroups.com

On Sunday, November 10, 2013 5:35:14 AM UTC+8, Stéphane Laurent wrote:
According to the usual syntax this should be renderTable({ input$data }), no?

yes, absolutely, thanks very much, I was quite confused, it's quite clear now.

Still confused about something: when I type input$data, is that the object returned by matrixInput?

in ui.R, I have: matrixInput(inputId = 'data', label = 'Add/Remove Rows', data = df)

How can I wrap it inside a data frame for further manipulations?

I've tried     df <- data.frame(input$data), but it doesn't seem to work.

What am I missing?

Thanks!

Stéphane Laurent

unread,
Nov 10, 2013, 3:31:56 AM11/10/13
to shiny-...@googlegroups.com
Sorry I don't see what you want to do. Here matrixInput creates a widget in which the user has to set the matrix. 

Patrick Toche

unread,
Nov 10, 2013, 3:49:52 AM11/10/13
to shiny-...@googlegroups.com
say I want to output the matrix that contains squares of each element of matrixInput?

Thanks!

Stéphane Laurent

unread,
Nov 10, 2013, 4:01:53 AM11/10/13
to shiny-...@googlegroups.com
Ok but you have to render the squared matrix in renderTable(). 

output$squaredmatrix <- renderTable({ input$data^2 })

and tableOutput("squaredmatrix").

Ok ? 

Patrick Toche

unread,
Nov 10, 2013, 7:39:52 AM11/10/13
to shiny-...@googlegroups.com
This is fantastic Stéphane, thanks again!

I'm guessing I can apply any function f to input$data, as in f(input$data), so that's very handy.

And how about assigning the first column of input$data to a data frame, is it as easy as this?

renderTable({
  df <- input$data[1]
})

I'll experiment and I guess I'll find out. Again, thanks!

Below is the updated version of this demo app, for the record:

library("shiny")
runGist("https://gist.github.com/anonymous/7397648")

Patrick.

Stéphane Laurent

unread,
Nov 10, 2013, 8:26:37 AM11/10/13
to shiny-...@googlegroups.com
Your last question has nothing to do with Shiny. Are you a R novice ? I understand it should be hard (but it is worth it). If df is a dataframe then df[,1] is its first column, and that's similar for matrices. Hence you could do :

renderTable({
  df[,1] <- input$data[,1] 
  df 
}) 

Below I offer you a slide of a presentation I'm currently making for non-proficient R users. I hope it helps. Once you get an input object (such as input$data) in server.R then you can treat it as a usual R object. Hence of course you can do f(input$data).


Patrick Toche

unread,
Nov 10, 2013, 9:04:36 AM11/10/13
to shiny-...@googlegroups.com
Thanks Stéphane!

Yes I'm a novice (in everything).

This is a great slide, superb reference to have, will keep it handy as I go on exploring shiny!

merci encore,

Patrick.
Reply all
Reply to author
Forward
0 new messages