使用文件输入上载文件时出错

时间:2018-04-09 07:50:58

标签: r shiny shiny-server shinydashboard

我正在尝试开发一个应用程序,它可以帮助用户上传文件并显示摘要统计信息。

我使用下面的UI和服务器代码来实现这一点,一旦我完成文件上传,我就无法看到数据框及其摘要统计信息。

我错过了我的代码,但无法猜测。

ui<-dashboardPage(
  dashboardHeader(title = "Claim 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(
                  tableOutput("contents")
                )
                )
                )
      )
    )
 ) 

服务器代码。

server <- shinyServer(function(input,output){
  output$contents <- renderTable({
    req(input$file1)

   df <- read.csv(input$file1$datapath,
                  header=input$header,
                  sep=input$sep)
  })
})

目前我没有用于显示数据框统计信息的代码。如何开始的任何领导都会有所帮助

1 个答案:

答案 0 :(得分:1)

以下是一个示例:在dashboardBody()内,您需要第二个(和第三个)tabItem()来“填充”menuItem(tabName = "vis")menuItem(tabName = "result")。在服务器中,您需要代码来生成绘图或表格或任何您想要显示的内容。此外,请注意我如何将read.csv()的输出分配给函数(此处称为DATA()),以便可以在服务器内的不同位置使用它。

UI:

ui <- dashboardPage(
    dashboardHeader(title = "Claim 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(
                            tableOutput("contents")
                        )
                    )
            ),
            tabItem(tabName = "vis", h2("Two Plots"),

                    fluidRow(
                        box(
                            title = "Plot 1", solidHeader = TRUE, collapsible = TRUE,
                            plotOutput("hist1") 
                        ),
                        box(
                            title = "Plot 2", solidHeader = TRUE, collapsible = TRUE,
                            plotOutput("hist2") 
                        )
                    )
            )
        )
    )
)

服务器:

server <- function(input, output) {
    DATA <- reactive({
        req(input$file1)

        df <- read.csv(input$file1$datapath,
                       header = input$header,
                       sep = input$sep)
        return(df)
    })

    output$contents <- renderTable(DATA())

    output$hist1 <- renderPlot({
        tmp1 <- sample(which(apply(DATA(), 2, is.numeric)), 1)
        hist(DATA()[, tmp1], breaks = 10, xlab = names(DATA())[tmp1], main = "")
    })

    output$hist2 <- renderPlot({
        tmp1 <- sample(which(apply(DATA(), 2, is.numeric)), 1)
        hist(DATA()[, tmp1], breaks = 20, xlab = names(DATA())[tmp1], main = "")
    })
}

运行:

library(shiny)
library(shinydashboard)
shinyApp(ui, server)