0

I am trying to develop an application, that could help the users upload a file and display the summary statistics.

I am using the below UI and Server code to achieve this, once I have completed my file upload, i am unable to see the data frame and its summary statistics.

I am missing something with my code, but unable to guess.

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 Code.

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

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

Currently i dont have the code for displaying the statistics from data frame. any lead on how to start would be helpful

Mikz
  • 571
  • 2
  • 11
  • 29
  • Try to get started here: https://rstudio.github.io/shinydashboard/get_started.html. If you still have questions, try to be more specific what you want to achieve and provide code that you tried but didn't work. – hplieninger Apr 09 '18 at 13:16
  • @hplieninger I started from there , and I guess my question is direct. I have designed my dashboard, and have updated my code for server as well. The problem is I am not able to display the data that I am uploading. I tried to figure out, but was unsuccessful,therefore I posted in stackoverflow. – Mikz Apr 09 '18 at 14:36
  • @hplieninger I am unsure if i could rendera table in fluidpage without a box defined. I am confused on the approach. Would be great, id you could help – Mikz Apr 09 '18 at 14:41

1 Answers1

1

Here is an example: Inside dashboardBody(), you need a second (and third) tabItem() to "fill" the menuItem(tabName = "vis") and menuItem(tabName = "result"). And in the server, you need code to generate plots or tables or whatever you want to display. Furthermore, note how I assigned the output of read.csv() to a function (called DATA() here) so that it can be used at different places inside the server.

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:

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 = "")
    })
}

run:

library(shiny)
library(shinydashboard)
shinyApp(ui, server)
hplieninger
  • 3,214
  • 27
  • 32
  • @hpliwninger, thank you for the sample code, but i still dont understand, why I am not able to see the data that I am uploading in my userinterface – Mikz Apr 10 '18 at 14:30
  • 1
    Where do you expect to "see" the data and in which form? When I use the above code and I upload a CSV-file, the data set is displayed in the tab called `"Load"`; this is done by `renderTable()` in the server. – hplieninger Apr 10 '18 at 14:59
  • @yeah true, I want it exactly as you did . I would try with a smaller csv file and tell you – Mikz Apr 11 '18 at 07:30
  • https://stackoverflow.com/questions/49873530/render-variables-from-file-input, could you help me with this item ? – Mikz Apr 17 '18 at 11:48