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.
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.
Answer
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")))
})
})
No comments:
Post a Comment