2

I need to have a pop-up window for when the uploaded file is not .csv file. The window information can be like "The .csv as file extension is allowed to be uploaded!". Unfortunately, I couldn't succeed in creating a pop-up window in Shiny so the code provided looks quite basic:

  library(shiny)

   shinyUI(
     fluidPage(
      fileInput("file1",
          "Choose CSV files from directory",
          multiple = TRUE,
          accept=c('text/csv', 
                   'text/comma-separated-values,text/plain', 
                   '.csv')),
      dataTableOutput("importcsv")))

server.R

  library(shiny)

   shinyServer(function(input, output) {

    output$importcsv <- renderDataTable({

     filecsv <- input$file1
     if (is.null(filecsv)) {
     return(NULL)
     } else {
      filecsv %>%
        rowwise() %>%
       do({
        read.csv(.$datapath)
       })
      }
     }) 

     })
pnuts
  • 58,317
  • 11
  • 87
  • 139
can.u
  • 515
  • 1
  • 4
  • 12

1 Answers1

1

Reactive text is pretty simple (although you need to test the filetype). Try

# ui.R    
library(shiny)

shinyUI(
  fluidPage(
    fileInput("file1",
              "Choose CSV files from directory",
              multiple = TRUE,
              accept=c('text/csv', 
                       'text/comma-separated-values',
                       'text/plain', 
                       '.csv')),
    htmlOutput('filetype'),
    dataTableOutput("importcsv")))

and

# server.R
library(shiny)
library(dplyr)

shinyServer(function(input, output) {

  output$importcsv <- renderDataTable({
    filecsv <- input$file1
    if (is.null(filecsv)) {
      return(NULL)
    } else {
      output$filetype <- renderText({
        ifelse(input$file1$type %in% c('text/csv', 
                               'text/comma-separated-values',
                               'text/plain', 
                               '.csv'), 
               '', 
               HTML('<script type="text/javascript">alert("CSV, please!");</script>'))
           })
      filecsv %>%
        rowwise() %>%
        do({
          read.csv(.$datapath)
        })
    }
  })  
})

That lets you show a string in your UI dependent on the filetype. Then you need to format your string. Shiny supports some HTML text tags (p, h1 to h6, etc.), but to get a popup, you need to call JavaScript.

The above code wraps the JavaScript alert in an HTML <script> tag, all in an R string, all wrapped in a Shiny HTML() function to keep it from being treated as escaped display text. It's a double turducken, but it works, and is surprisingly intelligible. It doesn't stop the the datatable from rendering, but you could easily rearrange it to do so (a good idea, as fileInput smashes everything into a dataframe, regardless of filetype).

A look at R's error handling is probably in order here, too. See ?try and ?tryCatch. And careful of security; that's not my area, but I suspect this would be iffy deployed for real.

alistaire
  • 42,459
  • 4
  • 77
  • 117
  • See [similar](http://stackoverflow.com/questions/29022770/r-shiny-warning-message-when-wrong-file-is-uploaded?rq=1); validating is probably what you want to do anyway, but I read your question pretty literally. – alistaire Jan 08 '16 at 15:57
  • when i compare the "similar" example with mine, the pop-up window is needed to show the warnings. And it works nice. thank for your helping! :) – can.u Jan 08 '16 at 22:30
  • I happened across [this question](http://stackoverflow.com/questions/14452465/how-to-create-textarea-as-input-in-a-shiny-webapp-in-r) today, which led me to think that if you like, you could rewrite the line with the JavaScript as `tags$script(type = "text/javascript", 'alert("CSV, please!");')` which is probably slightly more robust. – alistaire Jan 10 '16 at 06:41
  • I would like to ask one more thing that the pop window does not appear when i try to uplaod pdf file? how can i develop? – can.u Jan 11 '16 at 20:15
  • It's because you're passing the file to `renderDataTable` regardless of whether it's a .csv. When passed a .pdf, it raises an AJAX error (you'd have to dig into the source code to figure out why) which stops the execution of `renderDataTable`, which includes the alert code. Really, there's no reason to render anything that's not a .csv, though, so you can solve the issue by putting the `filetype %>% ...` chain in a conditional: `if(input$file1$type %in% c('text/csv', ... )) { filetype %>% ... }` so it only renders if the file is a .csv. – alistaire Jan 11 '16 at 21:04