0

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)
firmo23
  • 7,490
  • 2
  • 38
  • 114
  • 1
    It might work if you move the logic into an `observeEvent(input$submit, {...})` block, store the result in a variable, and use that variable in the `renderJsonedit(...)` call. That way, you don't have to use the `isolate(...)`. – Bas Apr 22 '20 at 06:39
  • I moved the if logic inside an observeEvent(input$submit, {...}) and passed it to variable named log. But when I pass log inside renderJsonedit(...) it seems to run when I click submit but does not display the plot. – firmo23 Apr 22 '20 at 12:23
  • 1
    Could it be that you have to `isolate` the `rv$data` calls as well in your original example? – Bas Apr 22 '20 at 14:09
  • it worked with eventReactive() as you can see in my edit but now when I click reset the plot deos not disappear even if there is no file. – firmo23 Apr 22 '20 at 14:58
  • 1
    I think you should add the `input$reset` to `eventReactive `, as such: `eventReactive({input$submit \n input$reset}, {...})`, in which the `\n` is a newline – Bas Apr 22 '20 at 15:25
  • closer it deletes it.. but it needs to have clicked both submit and reset at least one now to display the plot – firmo23 Apr 22 '20 at 15:34

0 Answers0