Кнопка с блестящим действием не работает во второй раз - PullRequest
1 голос
/ 20 апреля 2020

Я создал следующее приложение в R блестящий

 library(shiny)
 library(data.table)

После того, как мы импортируем библиотеки, мы теперь создаем пользовательский интерфейс следующим образом

 ui <- fluidPage(

    pageWithSidebar(
    headerPanel("TestApp"),
    sidebarPanel(
    numericInput(inputId = "n", label = "N:", min = 0, max = 100, value = 50),
    br(),
    actionButton(inputId = "goButton", label = "Go!"),

     actionButton(inputId = "Reset",label =  "Reset")),
    mainPanel(dataTableOutput(outputId = "all") ) ))

Это создаст пользовательский интерфейс с числовой ввод и две кнопки, одна кнопка go, а другая кнопка сброса

Далее мы создаем сервер и запускаем приложение

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

     table_output<-eventReactive(input$goButton,{ ##### WE CREATE THE TABLE OBJECT HERE
     table_output<-data.frame("SLNO"= seq(1:input$n))
     table_output$Col2<-table_output$SLNO^2
     return(table_output)})


     output$all<-renderDataTable({
     table_output()})



        observeEvent(input$Reset, { #### WE EMPTY THE PLOT AREA HERE
         output$all <- renderDataTable({ })})}


       shinyApp(ui = ui, server = server)

Таблица генерируется, когда я нажимаю Go кнопка Однако после нажатия кнопки rest таблица отказывается от рендеринга, несмотря на изменение входных данных. Я прошу некоторую помощь здесь.

Ответы [ 2 ]

2 голосов
/ 20 апреля 2020

Проверьте, работает ли это для вас:

library(shiny)
library(data.table)

ui <- fluidPage(

  pageWithSidebar(
    headerPanel("TestApp"),
    sidebarPanel(
      numericInput(inputId = "n", label = "N:", min = 0, max = 100, value = 50),
      br(),
      actionButton(inputId = "goButton", label = "Go!"),

      actionButton(inputId = "Reset",label =  "Reset")),
    mainPanel(dataTableOutput(outputId = "all") ) ))

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

  table_output <- eventReactive({input$goButton}, data.frame("SLNO" = seq(1:input$n), "Col2" = seq(1:input$n)^2))

  observeEvent(input$goButton, {
    output$all <- renderDataTable({
      table_output()})
  })

  observeEvent(input$Reset, {
    output$all <- renderDataTable({ })})}

shinyApp(ui = ui, server = server)
1 голос
/ 20 апреля 2020

Я предпочитаю использовать observeEvent и reactiveValues в подобных ситуациях.

library(shiny)
library(data.table)

ui <- fluidPage(

  pageWithSidebar(
    headerPanel("TestApp"),
    sidebarPanel(
      numericInput(inputId = "n", label = "N:", min = 0, max = 100, value = 50),
      br(),
      actionButton(inputId = "goButton", label = "Go!"),

      actionButton(inputId = "Reset",label =  "Reset")),
    mainPanel(dataTableOutput(outputId = "all") ) ))


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

  table_output <- reactiveValues(df = data.frame(SLNO = as.numeric(), Col2 = as.numeric()))


  observeEvent(input$goButton, {
    temp <- table_output$df
    temp <- data.frame("SLNO"= seq(1:input$n))
    temp$Col2 <- temp$SLNO^2
    table_output$df <- temp
  })

  observeEvent(input$goButton, {

    if(nrow(table_output$df) > 1) {

      output$all<-renderDataTable({
      table_output$df})

    }
  }
)


  observeEvent(input$Reset, { #### WE EMPTY THE PLOT AREA HERE
    table_output <- reactiveValues(df = data.frame(SLNO = as.numeric(), Col2 = as.numeric()))
    output$all<-renderDataTable({})

    })
}

shinyApp(ui = ui, server = server)

enter image description here

...