0

I have a shiny app which takes a csv file as input and after clicking 'submit' should display a jsoneditOutput. Besides this I have used a reset button which when clicked should reset the file input. But when I click submit I get: Error in read.table: 'file' must be a character string or connection.

library(shiny)
library(shinyjs)
library(tidyverse)
library(listviewer)
library(jsonlite)
library(SACCR)
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
  useShinyjs(),
  fileInput('inFile', 'Choose 1st file'),
  actionButton('submit', 'Submit'),
  tags$hr(),
  actionButton('reset', 'Reset')
    ),
  mainPanel(
    jsoneditOutput("choose")
  )
)
)

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

  rv <- 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)



  output$choose <- renderJsonedit({input$submit
    jsonedit(jsonlite::fromJSON(SACCR::SACCRCalculator(isolate(rv$data), JSON=TRUE)))
  })
}

shinyApp(ui, server)
firmo23
  • 7,490
  • 2
  • 38
  • 114

1 Answers1

1

So the issue is with this line:
jsonedit(jsonlite::fromJSON(SACCR::SACCRCalculator(isolate(rv$data), JSON=TRUE)))

The SACCRCalculator function needs a .csv file, not an R dataframe. Try replacing rv$data with input$inFile$datapath.

Also, the SACCRCalculator function requires three files in total; the trades, CSA, and collaterals. So that line will need to be expanded to include all three files. It should end up looking something like: SACCRCalculator(input$trades$datapath, input$csa$datapath, input$collaterals$datapath, JSON=TRUE)

Ash
  • 1,463
  • 1
  • 4
  • 7
  • I followed your advice but submit button seems not to work for me. https://stackoverflow.com/questions/61355743/isolate-is-not-working-when-used-inside-an-if-statement-in-a-shiny-app – firmo23 Apr 22 '20 at 01:15