0
votes

I have an issue regarding Shiny on R. I have a function which returns a list with 2 objects as output, both as a matrix. The first one is always created and is always available for download. The second one, is combined with a condition displayed as a checkbox.

global

physical_check <- function(file_path, x,y,z, classification)
input: String, int, int, int, boolean
output: list(matrix1 = result, matrix2 = rating)

UI:

ui <- fluidPage(   
   # Application title
   titlePanel("Review"),

   # Sidebar with a slider input for number of bins 
   sidebarLayout(
     sidebarPanel(
       fileInput('file1', 'Choose .txt File',
                 accept=c('text/csv','text/comma-separated,text/plain')),
       hr(),

       checkboxInput("classification", "Use Text-Classification", value = FALSE),
       hr(),

       downloadButton("download_physic", "Physical Review"),
       br(),
       conditionalPanel(condition = "input.classification == true", 
                        downloadButton("download_classify", "Text Classification"))

     ),
     mainPanel(
       h3("Review"),
       tableOutput("rating"),
       tableOutput("result_shiny")
     )
   )

)

Server:

server <- function(input, output) {
  inFile <- reactive({
    input$file1
  })
  result <- reactive({NULL})

    classify <-  reactive({
    input$classification
  })

  observe({
    if (!is.null(inFile())) {
      result <- reactive({
        withProgress({
          setProgress(message = "Processing review...")
          physical_check(as.String(inFile()$datapath),15,8,3,classify())
        })
      })
    }  

  output$result_shiny <- renderTable({
    result()$result
  })
    output$rating <- renderTable({
    result()$rating
  })

  output$download_physic <- downloadHandler(
    filename = function() {
      sub(pattern = "\\.txt",replacement = "_result.csv",inFile()$name)
    },
    content = function(file) {
      write.table(
        result()$result,
        file,
        sep = ";",
        col.names = NA,
        qmethod = "double"
      )
    }
  )

  output$download_classify <- downloadHandler(
    filename = function() {
      paste("rating", ".csv")
    },
    content = function(file) {
      write.table(
        result()$rating,
        file,
        sep = ";",
        col.names = NA,
        qmethod = double
      )
    }
  )
  }) 
}
# Run the application 
shinyApp(ui = ui, server = server)

So as you can see in the code I mentioned a conditional download button for the text classification, if the checkbox is triggered. According to this TRUE/FALSE value the function physical_checkis called with true or false and returns a matrix and NULL or 2 matrizes and only in the second option, the download button is displayed. I'm a little bit confused, because with tableOutputI receive the correct tables in the main panel, also downloading the physical reviewwith the first downloading part works fine. But the second one fails in google chrome with a download error:

download_classify
Failed - Server problem

Although it's constructed the same way.

1

1 Answers

0
votes

You forgot to put quotes"" in the qmethod argument. Your download handler for classify should be this:

output$download_classify <- downloadHandler(
      filename = function() {
        paste0("rating", ".csv")
      },
      content = function(file) {
        write.table(
          result()$rating,
          file,
          sep = ";",
          col.names = NA,
          qmethod = "double"
        )
      }
    )