I am trying to build a shiny app.
For this app, I have first created a fileinput() where user can upload the csv file they want and look at the dataset with statistics.
I would like to change the datatype of the columns from factors to numeric inthe displayed dataset and then perform analytics online.
I searched through web to get a starting point for developing this idea, but could not find any.
any lead on how i could select my required variable and change its datatype , would be helpful
here is the code i have tried so far.
UI
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")
),
mainPanel(
uiOutput("tb")
)
)
))
SERVER code:
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)
)
})
## 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)
ui <- shinyUI(fluidPage(
titlePanel("Claim Model"),
sidebarLayout(
sidebarPanel(
numericInput("1"),
),
mainPanel(
textOutput('dynamicText')
)
)
)
)