Select columns and conver the column datatype

shiny
rstudio
machinelearning

#1

I am trying to build my app for prediction model in shiny.

My idea is to first upload the file, by the user,

select the columns in the data frame and convert them to required datatype of numeric/factor/integer/date etc.

I have succeed in uploading the file , but I am struck on how I could use the selection method for columns and converting their data type.

I have my below code working. Any help would be great.

ui<- shinyUI(
 
  fluidPage(
  titlePanel("File Input - Upload multiple files"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file","Upload the file", multiple = TRUE, accept = c("text/csv", 
                                                                      "text/comma-seperated-values, text/plain",
                                                                      ".csv")), # fileinput() function is used to get the file upload contorl option
      helpText("Default max. file size is 5MB"),
      helpText("Select the read.table parameters below"),
      checkboxInput(inputId = 'header', label = 'Header', value = TRUE),
      checkboxInput(inputId = "stringAsFactors", "stringAsFactors", FALSE),
      radioButtons(inputId = 'sep', label = 'Separator', choices = c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ','),
      uiOutput("selectfile"),
      uiOutput("checkbox")
    ),
    mainPanel(
      uiOutput("tb")
     
    )
  
  )
))

server<- shinyServer(function(input,output) {
  
  ## input$file is a data frame and contains the details around the name, size and temp location of the files uploaded
  # this reactive output display the content of the input$file dataframe
  output$filedf <- renderTable({
    if(is.null(input$file)){return ()}
    input$file # the file input data frame object that contains the file attributes
  })
  
  # Extract the file path for file
  output$filedf2 <- renderTable({
    if(is.null(input$file)){return ()}
    input$file$datapath # the file input data frame object that contains the file attributes
  })
  
  ## Below code to display the structure of the input file object
  output$fileob <- renderPrint({
    if(is.null(input$file)){return ()}
    str(input$file)
  })
  
  ## Side bar select input widget coming through renderUI()
  # Following code displays the select input widget with the list of file loaded by the user
  output$selectfile <- renderUI({
    if(is.null(input$file)) {return()}
    list(hr(), 
         helpText("Select the files for which you need to see data and summary stats"),
         selectInput("Select", "Select", choices=input$file$name)
    )
    
  })
  
  #Dynamically generate UI input when data is uploaded
  
  output$checkbox <- renderUI({
    if(is.null(input$file)) {return()}
    else
    checkboxGroupInput(inputId = "select_var",
                       label="Select variables",
                       choices=names(input$file$name==input$Select))
  })
  
  'select columns to print'
  
  
  ## Summary Stats code ##
  # this reactive output contains the summary of the dataset and display the summary in table format
  output$summ <- renderPrint({
    if(is.null(input$file)){return()}
    summary(read.csv(file=input$file$datapath[input$file$name==input$Select], 
                       sep=input$sep, 
                       header = input$header, 
                       stringsAsFactors = input$stringAsFactors))})
  
  ## Dataset code ##
  # This reactive output contains the dataset and display the dataset in table format
  output$table <- renderTable({ 
    if(is.null(input$file)){return()}
    read.csv(file=input$file$datapath[input$file$name==input$Select], sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors)
    
  })
  
  ## MainPanel tabset renderUI code ##
  # the following renderUI is used to dynamically generate the tabsets when the file is loaded. 
  # Until the file is loaded, app will not show the tabset.
  output$tb <- renderUI({
    if(is.null(input$file)) {return()}
    else
      tabsetPanel(
        tabPanel("Input File Object DF ", tableOutput("filedf"), tableOutput("filedf2")),
        tabPanel("Input File Object Structure", verbatimTextOutput("fileob")),
        tabPanel("Dataset", tableOutput("table")),
        tabPanel("Summary Stats", verbatimTextOutput("summ")))
  })
})
shinyApp(ui, server)