Jenny
Jenny

Reputation: 451

Rendering the variables from the uploaded file

I am trying to develop a application, where the user can upload the file and prepare the file.

Therefore, I designed the dashboard with 2 sub menu items called Load and Prepare as you could see below.

In the first tab, I am trying to upload the file by the user.

In the second tab, Prepare I wanted to display the column names from the file selected by the user. for examples, if my data frame has column names (ID,Date,Accepeted,Rejected), then I want them to be listed in the prep tab.

Once listed, User should be able to select the datatype he wants.

Below is the snapshot of the dashboard I have created.

Idea is to display all the column names from the file user has uploaded  in "select variable" and modify the variable as numeric or charchter or in date format

Here is the UI code :

ui<-dashboardPage(
  dashboardHeader(title = "Model"),
  dashboardSidebar(
    sidebarMenu(id="tabs",
                menuItem("Data", tabName = "data", icon = icon("table"),startExpanded = TRUE,
                         menuSubItem("Load", tabName = "data1"),
                         menuSubItem("Prep", tabName = "prep")
                ),
                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")
                )

                )),
    #--------Sub menu Item 2-----------
    tabItem(tabName = "prep",
            h1("Preprocessing"),
            fluidPage(
              fluidRow(
                uiOutput("loaded_tb"),
                selectInput('data_option','Select Option',
                            label="Select the Variable",
                            list(
                              "Variable Attributes"="var_attr",
                              "Data Summary ='data_summary"
                            ))
              ),
              radioButtons("class_selection", label="Variables Modeification",
                           choices = list(Numeric="numeric",Factor="factor",
                                          Character ="character",
                                          Date="date"),
                           selected = "numeric"),
              selectInput('date_format', "Select the Date Format",
                          list(
                            YMD ="ymd",
                            YDM ="ydm",
                            MYD ="myd",
                            DMY ="dmy",
                            DYM ="dym"
                          )),
              tags$h5("Date Preview"),
              verbatimTextOutput('date_preview'),
              actionButton("var_modify", "Modify")
              ),
                 mainPanel(
                   uiOutput("Pre")

              )
            ))
  )
) 

Here is the server code:

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

  })

  #----- Data Preparation------

  output$Pre <- renderUI({checkboxGroupInput(inputId = "select_vars",
                                             choices = names(data))
  })
  data_sel <- reactive({
    req(input$select_vars)
    data_sel<- data()%>% select(input$select_var)
  })
})

shinyApp(ui,server)

Upvotes: 1

Views: 852

Answers (1)

kluu
kluu

Reputation: 2995

It's not quite an answer, since we don't understand what you really want to do. But here's a couple of tips that could help you move on hopefully.

data()

Do not use data as a variable name. Checking if data() is null or using names(data()) can result in bad surprises.

Store a data frame as a reactiveValues

As suggested by @Florian, in case you want to directly edit your data, you could use reactiveValues. A nice answer has been posted here, and below is the relevant piece of code for your case.

values <- reactiveValues(df_data = NULL)

observeEvent(input$file1, {
  values$df_data <- read.csv(input$file1$datapath, sep = input$sep)
}) 

Now instead of using data() to retrieve the data frame, you will have to use values$df_data instead.

Render the variables from the uploaded file

You made good use of renderUI, since you want your input selectors to depend on your data variables, except that your uiOutput are never linked to your uploaded file variables. Here's a way to edit your uiOutput for the variable selectors.

output$Pre <- renderUI({
  checkboxGroupInput(inputId = "select_vars",
                     label = "foo",
                     selected = names(values$df_data),
                     choices = names(values$df_data))
})

output$ui_data_option <- renderUI({
  selectInput('data_option',
              choices = names(values$df_data),
              label = "Select the Variable")
})

Hope this helps you a little.

Upvotes: 1

Related Questions