How to Specify Columns when a user chooses a file to upload in R?

ggplot2, import-from-csv, r, rstudio, shiny

Solution

Here's a working example. I took your code and modified it so that the Column Numbers can be passed from `UI.R` as inputs. (I use the diamonds dataset in ggplot2 for my dataframe.)

Note that I have created a couple of `reactive` functions in Server.R.

Server.R

library(shiny)
library(datasets)
library(ggplot2)

#x <- read.csv(file.choose())
x <- diamonds

# Define server logic required to summarize and view the selected dataset
shinyServer(function(input, output) {

  createPlot <- function(df, colx, coly) {
    x <- names(df)[colx] 
    y <- names(df)[coly] 
    ggplot(data=df, aes_string(x = x, y = y) ) + geom_line()
  }

  Y <- reactive({
    x
  })

  # Generate a summary of the dataset
  output$summary <- renderPrint({
    dataset <- x
    summary(dataset)
  })

  # Show the first "n" observations
  output$view <- renderTable({
    head(x, n = input$obs)
  })

  # create line plot (I took this from https://gist.github.com/pssguy/4171750)
  output$plot <- reactivePlot(function() {
    df <- Y()
    print(createPlot(df, colx=input$xa, coly=input$ya))
  })
})

UI.R

library(shiny)

# Define UI for dataset viewer application
shinyUI(pageWithSidebar(

  # Application title
  headerPanel("Sample Proj"),

  # Sidebar with controls to select a dataset and specify the number
  # of observations to view
  sidebarPanel(
    numericInput("obs", "Number of observations to view:", 10)
    ,numericInput("xa", "Column to plot as X-axis:", 5)
    ,numericInput("ya", "Column to plot as Y-axis:", 6)

  ),

  # Show a summary of the dataset and an HTML table with the requested
  # number of observations
  mainPanel(
    tabsetPanel(
      tabPanel("Table", tableOutput("view")),
      tabPanel("LineGraph", plotOutput("plot"))
    )
  )
))

As a separate suggestion, you could first get your shiny app working with a static dataframe, then try the `file.choose()` option with variable data frames.

Hope this helps you move forward.

Updated based on @joran's comment:

My original response was using the column number inside ggplot's `aes` with an `environment=environment()` argument added. I have modified the createPlot function in server.R to use `aes_string` instead.

Problem

I am writing an R file which prompts a user to upload a file and plots the data in the file the user uploads. I do not know how to reference the columns however (I am trying to use ggplot2) in my code. The data the user will upload will be a CSV file that would look something like, but can vary: ``` January February March April May Burgers 4 5 3 5 2 ``` I am stuck at the ggplot2 part where I need to reference column names. server.R ``` library(shiny) library(datasets) library(ggplot2) X <- read.csv(file.choose()) # Define server logic required to summarize and view the selected dataset shinyServer(function(input, output) { # Generate a summary of the dataset output$summary <- renderPrint({ dataset <- X summary(dataset) }) # Show the first "n" observations output$view <- renderTable({ head(X, n = input$obs) }) # create line plot (I took this from https://gist.github.com/pssguy/4171750) output$plot <- reactivePlot(function() { print(ggplot(X, aes(x=date,y=count,group=name,colour=name))+ geom_line()+ylab("")+xlab("") +theme_bw() + theme(legend.position="top",legend.title=element_blank(),legend.text = element_text(colour="blue", size = 14, face = "bold"))) }) }) ``` UI.r ``` library(shiny) # Define UI for dataset viewer application shinyUI(pageWithSidebar( # Application title headerPanel("Sample Proj"), # Sidebar with controls to select a dataset and specify the number # of observations to view sidebarPanel( numericInput("obs", "Number of observations to view:", 10) ), # Show a summary of the dataset and an HTML table with the requested # number of observations mainPanel( tabsetPanel( tabPanel("Table", tableOutput("view")), tabPanel("LineGraph", plotOutput("plot")) ) ) )) ```

Original source

Related problems