How to access different columns of a .csv data file just uploaded with shiny and work with them?

1,353 views
Skip to first unread message

Koushik Khan

unread,
Jun 27, 2015, 12:56:12 AM6/27/15
to shiny-...@googlegroups.com
I want to make an app which shows the effect of power transformation on a non-normal data. I want to keep an file upload option through which user can upload a .csv file (it is working fine). Next I want to allow user to select a variable of his/her choice from the data file just uploaded through a selectInput(). But I don't know how to implement this. Next task is to draw a histogram and a qqplot of the transformed data (i.e. vector) (obtained by applying transformation on the selected variable). Obviously there is already an option to select a suitable power to which the selected variable can be raised.

The question is how to work with a particular column of a data set which is previously uploaded by fileInput().

Robin Browne

unread,
Jun 27, 2015, 8:03:48 AM6/27/15
to shiny-...@googlegroups.com
something like this?

 df <- read.csv(inFile$datapath)

And if your csv contains a column named "Value" to access it you do something like
 
 df$Value

e.g. if you want a sum of all numbers in Value column and store it in x

x <- sum(df$Value)

Bastien Tran

unread,
Jun 27, 2015, 12:56:52 PM6/27/15
to shiny-...@googlegroups.com
If for some reason you do need the column indices, or a list of the variables to update your selectInput options accordingly you could do something like the following:

as.list(setNames(names(mydataframe),1:length(names(mydataframe))))

should return a named list of the form "column number"="column name" (as text strings!)

With that list you should be able to update you selectInput's (let's call it input$select) list so that when a new file is loaded, the input$select options are updated according to the now available variables. From there you can select your data through names as Robin suggested or through column indices (you may need to use as.numeric() in order to get an actual number) .

Kumar Siddasetty

unread,
Mar 26, 2017, 12:25:03 PM3/26/17
to Shiny - Web Framework for R
Hi I am new to shiny, i have the same question described below, Can any one of the post complete code "how to access columns of data loaded thru fileinput option.

Thanks a lot
Kumar

Bárbara Borges

unread,
Mar 27, 2017, 6:35:00 AM3/27/17
to Shiny - Web Framework for R
Here's a full example:

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose CSV File"),
      selectInput("col", "Select a column", character(0))
    ),
    mainPanel(
      tableOutput("table"),
      textOutput("selected")
    )
  )
)

server <- function(input, output, session) {
  data <- reactive({
    inFile <- input$file1
    if (is.null(inFile)) return(NULL)
    read.csv(inFile$datapath)
  })
  
  observeEvent(data(), {
    updateSelectInput(session, "col", choices = names(data()))
  })
  
  output$table <- renderTable({
    req(data())
    head(data())
  })
  
  output$selected <- renderText({
    req(data())
    paste0("You've selected the column named ", input$col, 
      ". It's mean value is: ", mean(data()[[input$col]]))
  })
}

shinyApp(ui, server)
Reply all
Reply to author
Forward
0 new messages