Jsoneditoutput is not displayed when given as reactive value in a shiny app

萝らか妹 提交于 2020-05-14 08:48:27

问题


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)

回答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)



来源:https://stackoverflow.com/questions/61349868/jsoneditoutput-is-not-displayed-when-given-as-reactive-value-in-a-shiny-app

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