How to feed uploaded data by using 'fileInput' function to plot a ggplot?

Viewed 79

I am trying to develop a shiny app in which I am giving the user an option to upload a data file (CSV or txt). Then I want to render a ggplot or plotly from this data. There are various columns in that dataset. So, I need to render various Plots, and they are to be rendered by the user selecting the columns in that dataset to be x-axis data and y-axis data. I have provided options to the user to select the data column for x and y axes. But, when I feed the data columns selected by the user for plotting, the output does not show any graph.

code for my plot:

df <- mtcars
ggplot(df, aes(x, y)) +
geom_point(aes(color='blue'))

should I use aes or aes_string to render the columns from the uploaded data?

1 Answers

I had this exact issue for a while trying to show a plot in shiny. Consider that you are likely having the user choose a selectInput drop down for column selection, right? That is going to send a string (chr) to your ggplot functions.

Normally, you use aes() to choose aesthetics, but that won't work with strings. For that, you need aes_string(). It's used exactly the same way as aes(), but all calls are character strings. See documentation for information.

So if your ggplot call was:

ggplot(myData, aes(x=something, y=somethingElse)) +
    geom_point(aes(color=prettyColors))

Where columns in your dataset myData were "something", "somethingElse" and "prettyColors", then using aes_string(), you create the same plot using:

ggplot(myData, aes_string(x="something", y="somethingElse")) +
    geom_point(aes_string(color="prettyColors"))
Related