How can I build a table result using "eventReactive" in Shiny

Asked

Viewed 106 times

1

How can I create a "table result" for each relationship I chose in selectInput "Col" and "Row"? Dinamicaly, for every time you press the 'ok' button'.

library(shiny)
shinyUI(fluidPage(
  h4("Give a valor between 0 to 5, to each col/row relationship"),
  hr(),
  uiOutput("colrow"),
  hr(),
  h5("Result:"),
  tableOutput("result")
))
shinyServer(function(input, output, session) {
  cols <<- c("Select"="", "col01" = "c01", "col02" = "c02")
  rows <<- c("Select"="", "row01" = "r01", "row02" = "r02")
  values <<- c("Select"="", 1:5)

output$colrow <- renderUI({
  div(selectInput("ipt_col", label = "Col",
                  choices = c(cols),
                  selected = cols[1],
                  width = "50%"),
      selectInput("ipt_row", label = "Row",
                  choices = c(rows),
                  selected = rows[1],
                  width = "50%"),
      selectInput("ipt_vlr", label = "Value",
                  choices = c(values),
                  selected = ""),
      hr(),
      actionButton("bt_ok", "ok")
  )
})

colrow_vlr <- eventReactive(input$bt_ok, {      
  as.data.frame(matrix(input$ipt_vlr, 1,1, dimnames = list(input$ipt_row,input$ipt_col)))
})

output$result <- renderTable({
  colrow_vlr()
})
})

1 answer

1

Let me get this straight. You want to go fill in the table?

(0) beginning

      c01    c02
r01 
r02 

(1) Col='col02'; Row='row01' and Value=1 ---> ok

      c01    c02
r01            1
r02 

(2) Col='col01'; Row='row02' and Value=5 ---> ok

      c01    c02
r01            1
r02     5

And so on and so forth?

Because the code now only produces:

(0) beginning

 ... 

(1) Col='col02'; Row='row01' and Value=1 ---> ok

c02
 1 

(2) Col='col01'; Row='row02' and Value=5 ---> ok

c01 
  5

But if your problem was just to make the row number appear in the table then use the option rownames=TRUE of renderTable():

output$result <- renderTable(colrow_vlr(),rownames = TRUE)

This results in the following:

inserir a descrição da imagem aqui

  • I want to go filling the table, as you noted in the first line of your comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.