当用户选择要在R中上传的文件时,如何指定列?

时间:2013-07-24 00:01:51

标签: r ggplot2 shiny rstudio import-from-csv

我正在编写 R 文件,该文件会提示用户上传文件并在用户上传的文件中绘制数据。我不知道如何在我的代码中引用列(我试图使用ggplot2)。

用户将上传的数据将是一个看似类似但可能有所不同的CSV文件:

        January February March April May
Burgers    4       5       3     5    2

我被困在ggplot2部分,我需要引用列名。

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"))
    )
  )
))

1 个答案:

答案 0 :(得分:4)

这是一个有效的例子。我接受了您的代码并对其进行了修改,以便可以从UI.R作为输入传递列号。 (我在ggplot2中使用钻石数据集作为我的数据帧。)

请注意,我在Server.R中创建了几个reactive函数。

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"))
    )
  )
))

作为一个单独的建议,您可以先使用静态数据框使用闪亮的应用程序,然后尝试使用可变数据框的file.choose()选项。

希望这有助于你前进。

根据@joran的评论更新:

我原来的回答是在ggplot的aes中使用了列号,并添加了environment=environment()参数。我修改了server.R中的createPlot函数,改为使用aes_string