R Shiny: matrix with numericInputs

567 views
Skip to first unread message

Robert

unread,
Dec 8, 2014, 4:59:56 AM12/8/14
to shiny-...@googlegroups.com

Dear All,

My question relates to question already asked here: R shiny: how to create a matrix with numericInput as elements. Ive used the proposed solution there, but it does not fully solve my problem.

Please have a look at my code and help me with the following issues.

The idea is to create an app where user enters some a-priori information into table/matrix. The number of rows can be different, depending on a number of analysed groups, and elements of the matrix are numerical and positive only. Now:

1) I can add the rows, but cannot decrease their number. Some relevant actionButton might be useful.

2) I would like to carry out some calculation on the resulting table/matrix columns, but not on individual elements (input$c1n1, input$c1n2 ...).

3) The sum of the first column elements is calculated as the elements are entered. I would like to do it when the matrix is ready by clicking a button ('Calculate 1st column sum').

Your help is much appreciated. 

Robert

shinyServer(function(input,output){
observe({

    if (input $ new.group == 0)
        return()else{
    isolate({
        output $ table <- renderTable({
            numImp1 <- paste0("<input id='c1n", 1:input $ new.group, "' class='shiny-bound-input' type='number' value='' min='0' step=.01>")
            numImp2 <- paste0("<input id='c2n", 1:input $ new.group, "' class='shiny-bound-input' type='number' value='' min='0' step=.01>")
            table = data.frame(Value1 = numImp1, Value2 = numImp2)
    rownames(table) = paste('Group',1:nrow(table), sep='')
    table
        }, sanitize.text.function = function(x){x})


        output $ Sum.V1 <-  renderPrint({
            sum.V1 <- sum(input $ c1n1, input $ c1n2, input $ c1n3, input $ c1n4, input $ c1n5, input $ c1n6, input $ c1n7, input $ c1n8, na.rm=T)
            return(sum.V1)
        })
    })}
})

})

shinyUI(pageWithSidebar(
headerPanel(""),
sidebarPanel(
    actionButton("new.group", "Add group"),
    actionButton("calc.sum", "Calculate 1st column sum")
    ),
mainPanel(
    h3('Table'),
    tableOutput("table"),
    h3('Resulting sum of the first column'),
    verbatimTextOutput('Sum.V1')
    )
)

)

Joe Cheng

unread,
Dec 8, 2014, 1:06:32 PM12/8/14
to Robert, shiny-...@googlegroups.com
I'll answer the easy ones at least :)

2) You could c/cbind them together I guess, and use sapply so it's not so onerous. But it'd be simpler to just use the matrixInput function from Shiny Incubator.

3) Something like this? See this article for an explanation.
        output $ Sum.V1 <-  renderPrint({
            validate(need(input$calc.sum, FALSE))
            isolate({
                sum.V1 <- sum(input $ c1n1, input $ c1n2, input $ c1n3, input $ c1n4, input $ c1n5, input $ c1n6, input $ c1n7, input $ c1n8, na.rm=T)
                return(sum.V1)
            })
        })

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/65340045-cb92-49a2-a693-6bd09d04505a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Robert

unread,
Dec 10, 2014, 5:00:58 AM12/10/14
to shiny-...@googlegroups.com, r.koz...@gmail.com
Dear Joe, 

Thank you for your help. 
It works!

Best, Robert
Reply all
Reply to author
Forward
0 new messages