2

The idea is to develop a shiny application, that helps the user to upload a file in the user interface (basically the test data) and then check for prediction and display graphs with a single button.

Currently, I have developed the UI, in such a way that it is possible to upload the test file using file input command.

I am now struck how I could include the server command, that triggers for changing the datatype and checking for missing value. I am unsure on how i could integrate these codes in my server.

any help would be great. or any link that could guide me is helpful. I have spent whole day on searching for relevant post, but i was not able to find and integrate it.

Below is my code for server and my general R code that i would like to integrate.

UI code:

shinyUI(fluidPage(
  titlePanel("File Input"),
  sidebarLayout(
    sidebarPanel(
      fileInput("file","Upload the file"), # fileinput() function is used to get the file upload contorl option
      helpText("Default max. file size is 5MB"),
      tags$hr(),
      h5(helpText("Select the read.table parameters below")),
      checkboxInput(inputId = 'header', label = 'Header', value = FALSE),
      checkboxInput(inputId = "stringAsFactors", "stringAsFactors", FALSE),
      br(),
      radioButtons(inputId = 'sep', label = 'Separator', choices = c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ',')
      ),
    mainPanel(
      uiOutput("tb")
      )

    )
  ))

shinyServer(function(input,output){


  # file$datapath -> gives the path of the file
  data <- reactive({
    file1 <- input$file
    if(is.null(file1)){return()} 
    read.table(file=file1$datapath, sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors)

  })

  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()))
      h5("Powered by", tags$img(src='RStudio-Ball.png', heigth=200, width=200))
    else
      tabsetPanel(tabPanel("About file", tableOutput("filedf")),tabPanel("Data", tableOutput("table")),tabPanel("Summary", tableOutput("sum")))
  })
})

here is the preprocessing steps that I would like to integrate with my server code.

Converting the datatype

claim[,c(2:12,16:22,25,30,31,33,32,34)] <- lapply(claim[,c(2:12,16:22,25,30,31,33,32,34)], as.numeric)
claim[,c(1, 13:15)] <- lapply(claim[,c(1, 13:15)], as.Date, format = "%d.%m.%Y")

Missing value imputation

mice_impute = mice(New,m=5, method='rf')
Imputed_data <- mice::complete(mice_impute, 3)

EDIT

My data set in the R environment contains 4000 observation with 34 variables.

For example, I have considered 5 variables.

variables are of datatype factor.

claim <- c(V1,V2,V3,V4,V5) the first step is I want to convert them to numeric. this is the general R code.

claim[,c(2:12,16:22,25,30,31,33,32,34)] <- lapply(claim[,c(2:12,16:22,25,30,31,33,32,34)], as.numeric)
    claim[,c(1, 13:15)] <- lapply(claim[,c(1, 13:15)], as.Date, format = "%d.%m.%Y")

I want something user friendly like below. By converting so, I should be able to create them as a new test data frame

once converted , I want to check for missing values, and generate the result, dynamically in a box and impute them using mice package.In my R environment, i already have the code for mice.

Basically my training set will be in R environment itself, the uploaded new test data should be processed in the shiny and predict the result according to the training set.

Jenny
  • 441
  • 2
  • 7
  • 19
  • 1
    Hi @Jenny, please see [here](https://stackoverflow.com/questions/48343080/how-to-convert-a-shiny-app-consisting-of-multiple-files-into-an-easily-shareable/48343110#48343110) for some tips on how to create a reproducible example. Also, I would suggest limiting your question so that it is easier to answer; for example start with just the missing values imputation and omit the rest. With the answers you get, see if you can perform the next step yourself, if not; time for the next question ;) – Florian Apr 06 '18 at 07:11
  • @Florian yeah sure, I would then edit it.. Hope you could then help me with the first part of how i can change the datatype and impute missing value ;) – Jenny Apr 06 '18 at 07:13
  • definitely, let me know when the update is ready. – Florian Apr 06 '18 at 07:44
  • @Florian please let me know, if the update is clear to you. – Jenny Apr 06 '18 at 07:56
  • not 100% yet, see the link I included. You are missing an UI and data. In this case you could use `dput()` to include some data, or work with the `mtcars` dataset. – Florian Apr 06 '18 at 08:05
  • @Florian I have update the UI code i am using, and we will assume with mtcars dataset. – Jenny Apr 06 '18 at 08:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/168387/discussion-between-jenny-and-florian). – Jenny Apr 06 '18 at 08:30

1 Answers1

4

Here is how I would approach this, but I was unable to test this, see my comment to your question.

Basically, we store the data in a reactiveVal, and use an observeEvent to listen to a click on an actionButton (input$convert_button, note that you need to add this to the UI), and then modify the data.

First, a working example with the mtcars dataset. Press upload to fake uploading your data, and press convert to fill the NA's.

my_mtcars <- mtcars
my_mtcars$cyl[1:10]=NA
my_mtcars$wt[1:10]=NA

ui <- fluidPage(
  actionButton('upload_button','Upload'),
  actionButton('convert_button','Convert'),
  tableOutput('table')
)

server <- shinyServer(function(input,output){

  data <- reactiveVal()

  # file$datapath -> gives the path of the file
  observeEvent(input$upload_button, ignoreNULL=T, ignoreInit=T, {
    data(my_mtcars)
  })

  observeEvent(input$convert_button, {
    claim <- data() # read the data from the reactiveVal
    claim[is.na(claim)] <- 0
    data(claim) # write back the modified data to the reactiveVal
  })

  output$table <- renderTable({
    if(is.null(data())){return ()}
    data()
  })

})

shinyApp(ui,server)

Here is an example on how to do this with your data, but as I said I was unable to test this:

shinyServer(function(input,output){

  data <- reactiveVal()

  # file$datapath -> gives the path of the file
  observeEvent(input$file, ignoreNULL=T, ignoreInit=T, {
    file1 <- input$file
    if(is.null(file1)){return()} 
    data(read.table(file=file1$datapath, sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors))
  })

  observeEvent(input$convert_button, {
    claim <- data() # read the data from the reactiveVal
    claim[,c(2:12,16:22,25,30,31,33,32,34)] <- lapply(claim[,c(2:12,16:22,25,30,31,33,32,34)], as.numeric)
    claim[,c(1, 13:15)] <- lapply(claim[,c(1, 13:15)], as.Date, format = "%d.%m.%Y")
    data(claim) # write back the modified data to the reactiveVal
  })

  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()))
      h5("Powered by", tags$img(src='RStudio-Ball.png', heigth=200, width=200))
    else
      tabsetPanel(tabPanel("About file", tableOutput("filedf")),tabPanel("Data", tableOutput("table")),tabPanel("Summary", tableOutput("sum")))
  })
})
Florian
  • 24,425
  • 4
  • 49
  • 80
  • basically from where are you reading your dataset ? is that from R environment ? – Jenny Apr 06 '18 at 14:17
  • https://stackoverflow.com/questions/49857752/rendering-the-variables-from-the-uploaded-file could you help me with this item. I am not getting any clue to solve it – Jenny Apr 17 '18 at 12:08
  • Hi @Jenny, please consider [accepting the answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) if it solver your issue. Thanks! – Florian Apr 17 '18 at 13:36
  • Yeah it did with mtcar dataset. Sorry, thats why I dint accept it. Though it solved mtcars, I am accepting it for sample – Jenny Apr 17 '18 at 13:44