Reputation: 355
I am a R newbie. I am currently using Rstudio and trying to develop a program that graphs whatever the user gives it (assuming it is a csv file). My problem is, I do not know how to reference the columns in the data that the user is giving. Here is a portion of my code:
library(shiny)
library(datasets)
library(ggplot2)
X <- read.csv(file.choose())
print(qplot(data=X, **x=?????, y=?????**, main="la"))
For the qplot function (or ggplot2), I want to give an x and y value (columns in the csv). Normally you would just use the fileName$ColumnName
, but in this case, I don't know what's in the data the user is uploading (so I don't know the column names).
I've tried doing this, without success:
library(shiny)
library(datasets)
library(ggplot2)
X <- read.csv(file.choose())
headers <- names(X)
print(qplot(data=X, **x=X$headers[1], y=X$headers[2]**, main="la"))
Any ideas guys?
EDIT: I'd also like to be able to display the column name on the graph. Is there a way to do this?
Upvotes: 0
Views: 145
Reputation: 121598
The key to solving this problem is to use aes_string
instead of aes
(I am not sure you know aes
since you are using qplot
which use it implicitly). aes_string
works directly with strings, so this should work:
ggplot(data=X) +
geom_point(aes_string(x=headers[1], y=headers[2]))
Upvotes: 3