0

app_test.R

 library(shiny)
 ui <- fluidPage(
     includeHTML("www/index.html")
  )
  server <- function(input, output, session) {
      data <- reactive({
          f1<-paste(read.csv(input$file))
          return(f1)
      })
      output$text <- renderTable({
          x=data()
      })
    }
    shinyApp(ui, server)

index.html

<!DOCTYPE HTML>
    <html>
       <head>
           <title>Samp R with html</title>
       </head>
   <body>
       <form>
           choose file:<input type="file" name="file" id="file" />
       </form>
       <p id="text" class="shiny-html-output" ></p>
   </body>
   </html>

These two are my R and html files. I want to display csv file as table which is uploaded by user. Iam getting the ERROR:'file' must be a character string or connection

1 Answer 1

1

Do you need to use an html template? There is an example in the Shiny Gallery http://shiny.rstudio.com/gallery/file-upload.html.

additionally here's a simple app that should get you started.

library(shiny)

ui <- bootstrapPage(
  tags$div(style="margin:5% 5%",
           fluidRow(
             column(4,
                    inputPanel(
                      fileInput('csv_files', 'Upload a csv file:',
                                accept = 
                                  c('text/csv', 
                                    'text/comma-separated-values,text/plain', 
                                    '.csv')),
                      checkboxInput('header', 'File has column names in first row', TRUE),
                      checkboxInput('rownames', 'Include Row Names?', FALSE),
                      selectInput('sep', 'Separator', 
                                  choices = list(
                                    comma = ',', 
                                    semicolon = ';', 
                                    tab = "\\t")
                      ),
                      selectInput('quote', 'Quote-Type', 
                                  choices = list(
                                    none = '', 
                                    double = '"',
                                    single = "'"),
                                  selected = '"')
                    )
             ),
             column(8,
                    tableOutput('uploaded_data')
             )
           )
  ))

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

    output$uploaded_data <- renderTable({
      raw_file <- input$csv_files
     if(!is.null(raw_file)){

       tbl <- read.csv(raw_file$datapath,
                       header = input$header,
                       sep = input$sep, 
                       quote = input$quote,
                       stringsAsFactors = FALSE)
       if(!input$rownames & colnames(tbl)[[1]] == "X"){
         tbl[['X']] <- NULL
       }
       return(tbl)
     }
   })
}

shinyApp(ui = ui, server = server)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer Carl Boneri, It's very useful for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.