【问题标题】:How to Specify Columns when a user chooses a file to upload in R?当用户选择要在 R 中上传的文件时如何指定列?
【发布时间】:2013-07-23 06:43:35
【问题描述】:

我正在编写一个 R 文件,它会提示用户上传文件并在用户上传的文件中绘制数据。但是,我不知道如何在我的代码中引用这些列(我正在尝试使用 ggplot2)。

用户将上传的数据将是一个 CSV 文件,看起来有点像,但可能会有所不同:

        January February March April May
Burgers    4       5       3     5    2

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

服务器.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"))
    )
  )
))

【问题讨论】:

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


    【解决方案1】:

    这是一个工作示例。我采用了您的代码并对其进行了修改,以便可以从 UI.R 传递列号作为输入。 (我使用 ggplot2 中的 diamonds 数据集作为我的数据框。)

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

    服务器.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

    【讨论】:

    • 我投了反对票,因为这是将参数传递给 ggplot 以选择变量的不好方法。最好使用列号来选择列名并将其传递给aes_string
    • @joran 阅读您的评论后,我已将回复更新为使用 aes_string。我在这方面看到了两种思想流派(stackoverflow.com/questions/15458526/… vs stackoverflow.com/questions/15323269/…)。我选择了 env 选项,因为有一个案例是在列名中包含减号 a-b 这样的字符可能会出错 aes_string. 根据您的建议更新了我的答案。
    猜你喜欢
    • 2013-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 2016-06-06
    • 2011-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多