如何向 R Shiny 添加行 table

How to add rows to R Shiny table

我正在尝试使用 R Shiny 构建一个表单,一旦单击表单末尾的操作按钮,它将用于填充 table。我无法弄清楚的是如何获取表单中的数据并将其添加到 table 中的新行。现在,它只是用表单中的任何内容不断更新第一行。我在这里复制了一个简单版本的代码:


    #ui.r
    
    library(shiny)
    
    shinyUI(fluidPage(
      # Application title
      titlePanel("Test App"),
      
      sidebarPanel(
        numericInput("x", "Enter Value of X", 1),
        numericInput("y", "Enter Value of Y", 1),
        actionButton("add_data", "Add Data", width="100%")
      ),
      mainPanel(
        tableOutput("xy_Table")
      )
    )
    )

    #server.R

    library(shiny)
    library(tidyverse)
    
    shinyServer(function(input, output) {
    
      x <- vector("numeric")
      y <- vector("numeric")
      xyTable <- tibble(x, y)
      e <- reactive(input$x)
      f <- reactive(input$y)
      
      eventReactive(input$add_data, {
        xyTable %>% add_row(x=e(), y=f())
      })
      
      output$xy_Table <- renderTable({
        xyTable
      })
    })

非常感谢您的帮助。


#ui.r

library(shiny)

ui <- shinyUI(fluidPage(
  # Application title
  titlePanel("Test App"),
  
  sidebarPanel(
    numericInput("x", "Enter Value of X", 1),
    numericInput("y", "Enter Value of Y", 1),
    actionButton("add_data", "Add Data", width="100%")
  ),
  mainPanel(
    tableOutput("xy_Table")
  )
)
)

#server.R

library(shiny)
library(tidyverse)

server <- shinyServer(function(input, output) {
  
  x <- vector("numeric")
  y <- vector("numeric")
  xyTable <- reactiveValues()
  xyTable$df <- tibble(x, y)
  e <- reactive(input$x)
  f <- reactive(input$y)
  
  observeEvent(input$add_data, {
    xyTable$df <- xyTable$df %>% add_row(x=e(), y=f())
  })
  
  output$xy_Table <- renderTable({
    xyTable$df
  })
})

shinyApp(ui,server)

您需要使用响应式 xyTable 才能更新输出。还, 将行附加到观察者而不是反应表达式中,并确保保存更新的反应值:

library(shiny)
library(tidyverse)

ui <- fluidPage(
  sidebarPanel(
    numericInput("x", "Enter Value of X", 1),
    numericInput("y", "Enter Value of Y", 1),
    actionButton("add_data", "Add Data", width = "100%")
  ),
  mainPanel(
    tableOutput("xy_Table")
  )
)

server <- function(input, output, session) {
  xyTable <- reactiveVal(
    tibble(x = numeric(), y = numeric())
  )

  observeEvent(input$add_data, {
    xyTable() %>%
      add_row(
        x = input$x,
        y = input$y,
      ) %>%
      xyTable()
  })

  output$xy_Table <- renderTable(xyTable())
}

shinyApp(ui, server)

试试这个:

library(shiny)
library(tidyverse)

#ui.r
ui <- fluidPage(
  # Application title
  titlePanel("Test App"),
  
  sidebarPanel(
    numericInput("x", "Enter Value of X", 1),
    numericInput("y", "Enter Value of Y", 1),
    actionButton("add_data", "Add Data", width = "100%")
  ),
  mainPanel(
    tableOutput("xy_Table")
  )
)


#server.R
server <- function(input, output) {
  xyTable <- reactiveValues(
    table1 = tibble(x = numeric(), y = numeric())
  )
  
  # what happens when `add_data` is clicked?
  observeEvent(input$add_data, {
    xyTable$table1 <- xyTable$table1 |> 
      add_row(x = input$x, y = input$y)
  })
  
  output$xy_Table <- renderTable({
    xyTable$table1
  })
}

shinyApp(ui, server)