简体   繁体   English

如何通过使用“fileInput”函数绘制ggplot来提供上传的数据?

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

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).我正在尝试开发一个shiny app在该shiny app中,我为用户提供了上传数据文件(CSV 或 txt)的选项。 Then I want to render a ggplot or plotly from this data.然后我想从这个数据渲染一个ggplotplotly 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.因此,我需要渲染各种绘图,它们将由用户选择该数据集中的列作为 x 轴数据和 y 轴数据来呈现。 I have provided options to the user to select the data column for x and y axes.我为用户提供了选择 x 和 y 轴数据列的选项。 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?我应该使用aes还是aes_string来呈现上传数据中的列?

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?考虑到您可能让用户为列选择选择一个selectInput下拉列表,对吗? That is going to send a string (chr) to your ggplot functions.这将向您的ggplot函数发送一个字符串 (chr)。

Normally, you use aes() to choose aesthetics, but that won't work with strings.通常,您使用aes()来选择美学,但这不适用于字符串。 For that, you need aes_string() .为此,您需要aes_string() It's used exactly the same way as aes() , but all calls are character strings.它的使用方式与aes()完全相同,但所有调用都是字符串。 See documentation for information . 有关信息,请参阅文档

So if your ggplot call was:因此,如果您的ggplot调用是:

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:数据集myData中的列是“something”、“somethingElse”和“prettyColors”,然后使用aes_string()创建相同的图:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM