total_noob total_noob -4 years ago 71
R Question

How to edit specific portions of the File upload functionality in Shiny?

I am totally new to R programming (and StackOverflow too) and was working on a small R project using the Shiny package(It seemed a lot easier and in tune to my requirements). Now I need to upload a .csv file for which Shiny has already provided a template or say codes in the form of ui.R and server.R. But my problem is that the code snippet Shiny has provided contains a browse section as well as a checkbox section for Headers and radio buttons for selection of Separators(Comma,Semicolon,Tab) and Quotes (None,Double,Single).

[1].

Now I only need the browse section and not the header or Separator or Quotes so I modified the script but the radio buttons don't seem to go.

# The code section starts here.
# Original Script provided by Shiny.
ui.R

library(shiny)

ui <- fluidPage(
titlePanel("Uploading Files"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose CSV File',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
tags$hr(),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator',
c(Comma=',',
Semicolon=';',
Tab='\t'),
','),
radioButtons('quote', 'Quote',
c(None='',
'Double Quote'='"',
'Single Quote'="'"),
'"')
),
mainPanel(
tableOutput('contents')
)
)
)


server.R file

library(shiny)

server <- function(input, output) {
output$contents <- renderTable({

# input$file1 will be NULL initially. After the user selects
# and uploads a file, it will be a data frame with 'name',
# 'size', 'type', and 'datapath' columns. The 'datapath'
# column will contain the local filenames where the data can
# be found.

inFile <- input$file1

if (is.null(inFile))
return(NULL)

read.csv(inFile$datapath, header=input$header, sep=input$sep,
quote=input$quote)
})
}

shinyApp(ui=ui, server = server)


I was able to remove the header tick box section as shown in the second image
[2]

but not been able to remove the radio boxes. Please advise on how to do that.Thanking everybody in advance.

Answer Source

I don't know if I correctly understood your question, but you can just simply remove the two radioButtons() and the checkboxInput() from ui:

ui <- fluidPage(
  titlePanel("Uploading Files"),
  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Choose CSV File',
                accept=c('text/csv', 
                         'text/comma-separated-values,text/plain', 
                         '.csv')),
      tags$hr()
    ),
    mainPanel(
      tableOutput('contents')
    )
  )
)

And then remove or replace the three input variables (input$header, input$set and input$quote) from your read.csv() in server:

server <- function(input, output) {
  output$contents <- renderTable({

    inFile <- input$file1

    if (is.null(inFile))
      return(NULL)

    read.csv(inFile$datapath, header=T, sep=";")
  })
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download