首页 > 解决方案 > 使用 actionButton Shiny R 删除 outputTable

问题描述

我正在尝试首先显示 TableOuput,根据用户输入,有:“media”和“desv_pad”。当我单击“rodar”按钮时,会显示表格。之后,当按下动作按钮“reset”时,我需要删除输出表“saida”,然后我的界面将干净以接收新输入并再次运行,但我的代码无法正常工作。

    library(shiny)


ui <- fluidPage(
  
  sidebarLayout(
    sidebarPanel("Inputs",
                 numericInput(inputId = "media", 
                              label = "Mean:",
                              value = 0,
                              min = 0),
                 numericInput(inputId = "desv_pad", 
                              label = "Standard Deviation:",
                              value = 1,
                              min = 0),
                 numericInput(inputId = "delta", 
                              label = "Mean Shift:",
                              value = 0.5,
                              min = 0,
                              max = 2,
                              step = 0.25),
                 numericInput(inputId = "n", 
                              label = "Size of Samples:",
                              value =  5,
                              min = 0,
                              max = 10,
                              step = 1),
                 numericInput(inputId = "alfa", 
                              label = "% alpha",
                              value = 0.27,
                              min = 0,
                              step = 0.1),
                 numericInput(inputId = "beta", 
                              label = "% beta:",
                              value = 97,
                              min = 0,
                              step = 0.1),
                 numericInput(inputId = "xb_teo", 
                              label = "%X max:",
                              value = 10,
                              min = 0),
                 actionButton("rodar", "Run")
    ),
    mainPanel(
      
      
      tags$h4( tableOutput('saida')),
      tags$br(),
      tags$br(),
      tags$br(),
      tags$br(),
      tags$br(),
      
      
      tags$br(),
      actionButton("reset", "Reset")
      
      
    )
  )
)  


server <- function(input, output) 
{
  v <- reactiveValues(data = NULL)
 
   observeEvent(input$rodar,{   
               output$saida <- renderTable({ #resultado,
                   
                   
                   passo_n <- 0 
                   #Recebendo os inputs:     
                   n <-input$n
                   Xb_teo <- input$xb_teo# input Xbarra percentual teorico definido pelo usuario
                   med<- input$media #input da media
                   desv_pad <- input$desv_pad #input do desvio padrao
                   alfa <- input$alfa #% determinado pelo usuario
                   beta <- input$beta #% determinado pelo usuario
                   delta <- input$delta
                   
                   v$data <- c(n, Xb_teo,med, desv_pad, alfa, beta, delta)

                 })
               })
  
                observeEvent(input$reset, {
                  v$data <- NULL

                })
                
                output$saida <- renderTable({
                  if(is.null(v$data)) return()
                  v$data
                  
                })

}
shinyApp(ui = ui, server = server)

标签: rshiny

解决方案


您的代码中有很多未定义的变量。我现在已经用常量替换了它们。

放在output$saida外面observeEvent。试试这个应用程序:

library(shiny)

ui <- fluidPage(
  
  sidebarLayout(
    sidebarPanel("Inputs",
                 numericInput(inputId = "media", 
                              label = "Mean:",
                              value = 0,
                              min = 0),
                 numericInput(inputId = "desv_pad", 
                              label = "Standard Deviation:",
                              value = 1,
                              min = 0),
                 numericInput(inputId = "delta", 
                              label = "Mean Shift:",
                              value = 0.5,
                              min = 0,
                              max = 2,
                              step = 0.25),
                 numericInput(inputId = "n", 
                              label = "Size of Samples:",
                              value =  5,
                              min = 0,
                              max = 10,
                              step = 1),
                 numericInput(inputId = "alfa", 
                              label = "% alpha",
                              value = 0.27,
                              min = 0,
                              step = 0.1),
                 numericInput(inputId = "beta", 
                              label = "% beta:",
                              value = 97,
                              min = 0,
                              step = 0.1),
                 numericInput(inputId = "xb_teo", 
                              label = "%X max:",
                              value = 10,
                              min = 0),
                 actionButton("rodar", "Run")
    ),
    mainPanel(
      
      
      tags$h4( tableOutput('saida')),
      tags$br(),
      tags$br(),
      tags$br(),
      tags$br(),
      tags$br(),
      
      
      tags$br(),
      actionButton("reset", "Reset")
      
      
    )
  )
)  


server <- function(input, output) 
{
  v <- reactiveValues(data = NULL)
  
  observeEvent(input$rodar,{   
      passo_n <- 0 
      #Recebendo os inputs:     
      n <-input$n
      Xb_teo <- input$xb_teo# input Xbarra percentual teorico definido pelo usuario
      med<- input$media #input da media
      desv_pad <- input$desv_pad #input do desvio padrao
      alfa <- input$alfa #% determinado pelo usuario
      beta <- input$beta #% determinado pelo usuario
      delta <- input$delta
      
      v$data <- c(n, Xb_teo,med, desv_pad, alfa, beta, delta)
      
  })
  
  observeEvent(input$reset, {
    v$data <- NULL
    
  })
  
  output$saida <- renderTable({
    v$data
  })
  
}
shinyApp(ui = ui, server = server)

推荐阅读