使用Fileinput上传文件

时间:2018-04-09 09:55:12

标签: r shiny shinydashboard

我正在尝试开发一个带仪表板设计的应用程序来显示表格摘要并显示数据框。

我在设计它时非常不成功。我为此加入了自己的仪表板设计。

我在服务器功能中缺少一个元素,无法查看上传文件的数据框。

任何领导都会很有帮助。

以下是我的UI和服务器功能代码

UI

  ui <- dashboardPage(
  dashboardHeader(title = "Model"),
  dashboardSidebar(sidebarMenu(
    id = "tabs",
    menuItem(
      "Data",
      tabName = "data",
      icon = icon("table"),
      startExpanded = TRUE,
      menuSubItem("Load", tabName = "data1")

    ),
    dashboardBody(
      tags$style(
        type = "text/css",
        ".shiny-output-error { visibility: hidden; }",
        ".shiny-output-error:before { visibility: hidden; }"
      ),
      tabItems(tabItem(
        tabName = "data1",
        fluidPage(
          fluidRow(
            fileInput(
              "file",
              "Choose CSV File",
              accept = c("text/csv",
                         "text/comma-seperated-values, text/plain",
                         ".csv")
            ),
            tags$hr(),
            checkboxInput("header", "Header", TRUE),
            radioButtons(
              "sep",
              "Separator",
              choices = c(
                Comma = ",",
                semicolon = ";",
                Tab = "\t"
              ),
              selected = ";"
            )
          ),
          mainPanel(uiOutput("tb"))
        )
      ))
    )
  )

服务器代码

server <- shinyServer(function(input,output){
 data <- reactive({
   file1 <- input$file
   if(is.null(file1)){return()}
   read.csv(file = file$datapath, sep=input$sep)
 })
 output$filedf <- renderTable({
   if(is.null(data())){return()}
   input$file
 })
 output$sum <- renderTable({
   if(is.null(data())){return()}
   summary(data())
 })
 output$table <- renderTable({
   if(is.null(data())){return()}
   data()
 })
 output$tb <- renderUI({
   if(is.null(data())){return()}
   tabPanel("About file", tableOutput("filedf")),tabPanel("Data", tableOutput("table")),tabPanel("Summary", tableOutput("sum"))

 })
  })

1 个答案:

答案 0 :(得分:1)

原来你的代码中有一个小错字。您指的是input$filefile$datapath,其中应该是input$file1file1$datapath。所以工作版本如下所示。希望这有帮助!

library(shiny)
library(shinydashboard)

ui<-dashboardPage(
  dashboardHeader(title = "Model"),
  dashboardSidebar(
    sidebarMenu(id="tabs",
                menuItem("Data", tabName = "data", icon = icon("table"),startExpanded = TRUE,
                         menuSubItem("Load", tabName = "data1")

                ),
                menuItem("Visualisation",icon=icon("bar-chart-o"), tabName = "vis"),
                menuItem("Result", icon=icon("cog"), tabName = "result")
    )
  ),
  dashboardBody(
    tags$style(type="text/css",
               ".shiny-output-error { visibility: hidden; }",
               ".shiny-output-error:before { visibility: hidden; }"
    ),
    tabItems(
      tabItem(tabName = "data1",
              fluidPage(
                fluidRow(
                  fileInput("file1","Choose CSV File",
                            accept = c("text/csv",
                                       "text/comma-seperated-values, text/plain",
                                       ".csv")
                  ),
                  tags$hr(),
                  checkboxInput("header", "Header", TRUE),
                  radioButtons("sep","Separator",
                               choices=c(Comma=",",
                                         semicolon=";",
                                         Tab="\t"),
                               selected = ";")
                ),
                mainPanel(
                  uiOutput("tb")
                )
              )
      )
    )
  )
) 


server <- shinyServer(function(input,output){
  data <- reactive({
    file1 <- input$file1
    if(is.null(file1)){return()}
    read.csv(file = file1$datapath, sep=input$sep)
  })
  output$filedf <- renderTable({
    if(is.null(data())){return()}
    input$file1
  })
  output$sum <- renderTable({
    if(is.null(data())){return()}
    summary(data())
  })
  output$table <- renderTable({
    if(is.null(data())){return()}
    data()
  })
  output$tb <- renderUI({
    if(is.null(data())){return()}
    tabsetPanel(tabPanel("About file", tableOutput("filedf")),tabPanel("Data", tableOutput("table")),tabPanel("Summary", tableOutput("sum")))

  })
})



shinyApp(ui,server)