单击actionButton时如何更新数据表?

时间:2019-02-26 16:25:19

标签: r datatable shiny

因此,我在Shiny中有一个数据表,我只想在用户更改输入参数并单击按钮时进行更新。下面是它的最小可复制示例:

Error:(13, 1) non-nominal type 'Any' cannot be extended

不幸的是,在这种情况下,数据表会在用户单击按钮时首先加载,但是此后每次library(dplyr) library(DT) library(shiny) ui <- fluidPage( fluidRow( column(3, numericInput("num1", "Limiter1", value = 0)), column(3, numericInput("num2", "Limiter2", value = 0)) ), fluidRow( column(3,actionButton("button1", "Apply filters1")), column(3,actionButton("button2", "Apply filters2")) ), fluidRow( column(6,dataTableOutput("testtable1")), column(6,dataTableOutput("testtable2")) ) ) server <- function(input, output, session) { filteredData1 <- reactive({ req(input$num1) iris %>% filter(Petal.Length >= input$num1) }) observeEvent(input$button1, { updateNumericInput(session, "num2", value = input$num1) output$testtable1 <- renderDataTable(datatable(filteredData1())) }) filteredData2 <- reactive({ req(input$num2) iris %>% filter(Petal.Length >= input$num2) }) observeEvent(input$button2, { output$testtable2 <- renderDataTable(datatable(filteredData2())) }) } shinyApp(ui, server) 更改时都会自动更新,而不管是否单击input$num1。仅当单击button1时,是否可以使用新参数更新表?

2 个答案:

答案 0 :(得分:0)

如果应用程序很简单,则只需将actionButton更改为submitButton

library(dplyr)
library(DT)
library(shiny)

ui <- fluidPage(
  fluidRow(
    numericInput("num1", "Limiter", value = 0)
    ),
  fluidRow(
    submitButton("button1", "Apply filters")
    ),
  fluidRow(
    dataTableOutput("testtable")
    )
)

server <- function(input, output, session) {
  filteredData <- reactive({
    req(input$num1)
    iris %>%
      filter(Petal.Length >= input$num1)
  })

  output$testtable <- renderDataTable(datatable(filteredData()))
}

shinyApp(ui, server)

答案 1 :(得分:0)

具有反应性值:

library(dplyr)
library(DT)
library(shiny)

ui <- fluidPage(
  fluidRow(
    numericInput("num1", "Limiter", value = 0)
  ),
  fluidRow(
    actionButton("button1", "Apply filters")
  ),
  fluidRow(
    dataTableOutput("testtable")
  )
)

server <- function(input, output, session) {

  filteredData <- reactiveVal(iris)
  observeEvent(input$button1, {
    filteredData(iris %>% filter(Petal.Length >= input$num1))
  })

  output$testtable <- renderDataTable(datatable(filteredData()))
}

shinyApp(ui, server)