【问题标题】:How can I build a table result with "eventReactive" in shiny如何在闪亮的“eventReactive”中构建表格结果
【发布时间】:2018-03-15 14:49:17
【问题描述】:

如何为我在 selectInput“Col”和“Row”中假设的每个关系创建一个“表格结果”? Dinamicaly,每次按下“确定”底部后。

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()    
})
})

【问题讨论】:

    标签: dataframe matrix shiny rbind cbind


    【解决方案1】:

    我稍微更改了您的代码,现在它可以工作了。我在进行更改的地方添加了 cmets。

    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)
    

    【讨论】:

    • 非常感谢,但结果与我所做的相同,而是由 DT:: 让我再试一次:对于每个关系,col>row>value,我按底部“确定”,所以我在桌子上放一排。如果我做五个关系,我的表格结果必须有 5 行。我清楚了吗?谢谢
    • @Fernando 你能通过修改你的问题并更具体地解释你想要做什么吗?
    • ex:我选择 Col = "Col01" and Row="Row01" and Value="4" 最后,我点击底部的“OK”。结果,给我看:c01 r01 4 next,我选择 Col = "Col01" and Row="Row02" and Value="2" and "OK" botom。结果,应该告诉我: c01 r01 4 r02 2 但在每次确定底部单击时,它都会替换 data.frame 内容,我无法编写代码来添加行。如何在 data.frame 中添加新行?现在呢 ? tks.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    • 2015-10-08
    • 1970-01-01
    相关资源
    最近更新 更多