如何使用" eventReactive"来构建表结果?在闪亮

时间:2018-03-15 14:49:17

标签: dataframe matrix shiny rbind cbind

如何创建"表格结果"我在selectInput" Col"中假设的每个关系和"行"? Dinamicaly,每次按下“确定”后#39; botom。

library(shiny)
shinyUI(fluidPage(
    h4("Give a valor between 0 to 5, to each col/row relationship"),
    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 = ""),
         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 个答案:

答案 0 :(得分:0)

我稍微更改了你的代码,现在它可以工作了。我在更改的位置添加了注释。

library(shiny)

ui <- fluidPage(
  h4("Give a valor between 0 to 5, to each col/row relationship"),
  uiOutput("colrow"),
  hr(),
  h5("Result:"),
  # using DT which is recommended in shiny
  DT::dataTableOutput("result")
)

server <- function(input, output, session) {
  # no need to assign in the global env especially 'cols' is reserved
  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 = cols, # no need to wrap with c()
                    selected = cols[1],
                    width = "50%"),
        selectInput("ipt_row", label = "Row",
                    choices = rows,
                    selected = rows[1],
                    width = "50%"),
        selectInput("ipt_vlr", label = "Value",
                    choices = values,
                    selected = ""),
        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 <- DT::renderDataTable({    
    colrow_vlr()
  })
}

shinyApp(ui = ui, server = server)