简体   繁体   中英

observeEvent is triggered unnecessary when using one evenExpr for mulitple handlerExpr in Shiny

Im creating shiny app. for calculating risk score where the user will upload input file and select the input such as ethnic groups, type of calculating score and diseases. After all of the input are selected and file is uploaded, my App. will be run when user click at action button and the output such as graph and dataframe will be shown

Im using observeEvent to control my App for triggering unnecessarily( mulitple handleExpr with one eventExpr ), and this is my shorten version of code. Im sorry for my code that is not reproducible.

 observeEvent(input$action,{

    isolate(system2("bash_script/plink.sh",args = c(input$file$datapath,input$type,input$sum_stat,input$Disease,input$Ethnic,input$Ref)))

    output$table_score <- renderDataTable({
      percentile <- read.csv("../output/score_percentile.csv",header = T, sep = "\t")
    }, selection = "single")

    output$table_variant <- renderDataTable({
      varaints_in_sample <- fread("../output/summary.csv", header = T, drop = 1)
    })

    #Plot Graph
    output$plot <- renderPlot({
      s <- input$table_score_cell_clicked
      plot("../output/score_percentile_plot.csv",s,"analysis")
    })

  })

my problem is that when Im running app for the first time, everything is controllable. However , if I want to select new input. for example im changing input disease from heart disease to another disease. my App. will be triggered unnecessarily although I did NOT click at action button.

So, Is there any way to use observeEvent with one evenExpr for mulitple handleExpr

Thanks everyone for your help!

I think, this is simplified example of your problem. The solution is to put all your input$... inside isolate() .

library(shiny)

ui <- fluidPage(

   sidebarLayout(
      sidebarPanel(
         sliderInput("bins",
                     "Number of bins:",
                     min = 1,
                     max = 50,
                     value = 30),
         actionButton('action', 'Click')
      ),

      mainPanel(
         plotOutput("distPlot")
      )
   )
)

server <- function(input, output) {

    output$distPlot <- renderPlot({
        req(input$action)
        # generate bins based on input$bins from ui.R
        x    <- faithful[, 2] 
        bins <- seq(min(x), max(x), length.out = isolate(input$bins) + 1)

        # draw the histogram with the specified number of bins
        hist(x, breaks = bins, col = 'darkgray', border = 'white')
    })

}

shinyApp(ui = ui, server = server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM