Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
873 views
in Technique[技术] by (71.8m points)

r Shiny action button and data table output

I would like an output data table after selection of some simple criteria, once an action button is clicked.

The code works without the action button as an input. As soon as I include the observeEvent function, the result is no longer generated. See example code below.

Note: Removing the line observeEvent(input$gobutton,{ and the corresponding }) will produce the correct output.

library(shiny)

ui <- fluidPage(
  fluidRow(column(6,div(checkboxGroupInput("test1", "Testing buttons", 
                                           choices=c("A","B","C"),
                                           selected=c("A","B","C"))))),

  hr(),

  actionButton("gobutton","Start"),

  dataTableOutput("summary_table")

)

server <- function(input,output){

  output$summary_table <- renderDataTable({

    observeEvent(input$gobutton,{

    df=data.frame(col1=input$test1,col2=seq(1,length(input$test1),1))

    df
    })
  })
}

shinyApp(ui=ui, server=server)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Wrap it in a eventReactive instead:

library(shiny)

ui <- fluidPage(
  fluidRow(column(6,div(checkboxGroupInput("test1", "Testing buttons", 
                                           choices=c("A","B","C"),
                                           selected=c("A","B","C"))))),

  hr(),
  actionButton("gobutton","Start"),
  dataTableOutput("summary_table")

)

server <- function(input,output){

  data <- eventReactive(input$gobutton,{
    if(is.null(input$test1)){
      return()
    }

    df <- data.frame(col1=input$test1,col2=seq(1,length(input$test1),1))
    df
  })

  output$summary_table <- renderDataTable({
    data()
  })
}

shinyApp(ui=ui, server=server)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...