Isolate() is not working when used inside an if statement in a shiny app

…衆ロ難τιáo~ 提交于 2020-04-30 06:26:19

问题


I have the shiny app below in which I have 3 file inputs and 2 actionbuttons.

The 1st submit button 'submit' is used to trigger the tree plot and the 2nd 'reset' to reset every file input.

When the 1st actionbutton is empty I get a warning message.

The tree is displayed only when the all of the files are loaded or 1st and 2nd are loaded or only 1st is loaded. Based on this I have crated an if statement which you can find in line 120. The issue is that while the app works fine it does not follow the submit button and the plots are displayed automatically. If I run it just for one case lets say :

jsonedit(jsonlite::fromJSON(SACCR::SACCRCalculator(isolate(input$inFile$datapath),isolate(input$inFile2$datapath),isolate(input$inFile3$datapath), JSON=TRUE)))

it works fine. So I assume that the problem is the if statements:

library(shiny)
library(shinyjs)
library(tidyverse)
library(listviewer)
library(jsonlite)
library(SACCR)
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
  useShinyjs(),
  fileInput('inFile', 'Choose 1st file'),
  fileInput('inFile2', 'Choose 2nd file'),
  fileInput('inFile3', 'Choose 3rd file'),
  actionButton('submit', 'Submit'),
  tags$hr(),
  actionButton('reset', 'Reset')
    ),
  mainPanel(
    #This hides the temporary warning messages while the plots are being created
    tags$style(type="text/css",
               ".shiny-output-error { visibility: hidden; }",
               ".shiny-output-error:before { visibility: hidden; }"
    ),
    uiOutput("choose"),
    jsoneditOutput( "choose2" )
  )
)
)

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

  rv <- reactiveValues(
    data = NULL,
    clear = FALSE
  )
  rv2 <- reactiveValues(
    data = NULL,
    clear = FALSE
  )
  rv3 <- reactiveValues(
    data = NULL,
    clear = FALSE
  )
  ########1st
  observe({
    req(input$inFile)
    req(!rv$clear)

      rv$data <- read.csv(input$inFile$datapath,header = T)



  })

  observeEvent(input$inFile, {
    rv$clear <- FALSE
  }, priority = 1000)

  observeEvent(input$reset, {
    rv$data <- NULL
    rv$clear <- TRUE
    reset('inFile')
  }, priority = 1000)


  #############2nd
  observe({
    req(input$inFile2)
    req(!rv2$clear)

    rv2$data <- read.csv(input$inFile2$datapath,header = T)



  })

  observeEvent(input$inFile2, {
    rv2$clear <- FALSE
  }, priority = 1000)

  observeEvent(input$reset, {
    rv2$data <- NULL
    rv2$clear <- TRUE
    reset('inFile2')
  }, priority = 1000)


  ##############3rd
  observe({
    req(input$inFile3)
    req(!rv3$clear)

    rv3$data <- read.csv(input$inFile3$datapath,header = T)



  })

  observeEvent(input$inFile3, {
    rv3$clear <- FALSE
  }, priority = 1000)

  observeEvent(input$reset, {
    rv3$data <- NULL
    rv3$clear <- TRUE
    reset('inFile3')
  }, priority = 1000)

  output$choose <- renderUI ({
    if(is.null(rv$data))
    {
      "You must upload 1st csv at least"
    }
    else
    {
      return(NULL)

    }
  })
  log<-eventReactive({input$submit 
input$reset}, {
if(is.null(isolate(rv$data))){
  return(NULL)
}
if(!is.null(isolate(rv$data))){

  if(!is.null(isolate(rv2$data))&is.null(isolate(rv3$data))){
    jsonedit(jsonlite::fromJSON(SACCR::SACCRCalculator(input$inFile$datapath,input$inFile2$datapath, JSON=TRUE)))
  }
  else if(is.null(isolate(rv2$data))&!is.null(isolate(rv3$data))){
    return(NULL)
  }
  else if(!is.null(isolate(rv2$data))&!is.null(isolate(rv3$data))){
    jsonedit(jsonlite::fromJSON(SACCR::SACCRCalculator(input$inFile$datapath,input$inFile2$datapath,input$inFile3$datapath, JSON=TRUE)))

  }
  else if(is.null(isolate(rv2$data))&is.null(isolate(rv3$data))){

    jsonedit(jsonlite::fromJSON(SACCR::SACCRCalculator(input$inFile$datapath, JSON=TRUE)))

  }
}

})

  output$choose2<-renderJsonedit({

    log()
  })
}

shinyApp(ui, server)

来源:https://stackoverflow.com/questions/61355743/isolate-is-not-working-when-used-inside-an-if-statement-in-a-shiny-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!