表输出从r到html

时间:2016-11-10 12:19:49

标签: html r shiny

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>

这两个是我的R和html文件。 我想将csv文件显示为用户上传的表格。 我得到了错误:&#39;文件&#39;必须是字符串或连接

1 个答案:

答案 0 :(得分:1)

您需要使用html模板吗? Shiny Gallery中有一个例子http://shiny.rstudio.com/gallery/file-upload.html

另外这里有一个简单的应用程序,可以帮助你入门。

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)