3

我正在制作一个使用 ggplot2 的 R 闪亮应用程序。这个应用程序接收用户上传的 csv 文件并使用 ggplot2 来绘制它们。

我的应用程序适用于小型 csv 输入(我说的是 20 行/列)。我正在尝试使我的应用程序对 2MB+ 范围内的文件的数据可视化有用。

然而,在我目前的状态下,我的图表对于大数据分析毫无用处。我将发布我的一些代码并链接到相关的 csv 文件,以便您重现问题。

这是一个示例数据集:http ://seanlahman.com/baseball-archive/statistics/ ,从版本 5.9.1 中选择任何内容 - 逗号分隔版本

尝试在 Appearances.csv 中为 X 绘制“YearID”,为 Y 绘制“playerID”,你会明白我的意思。

用户界面

library(shiny)

dataset <- list('Upload a file'=c(1))

shinyUI(pageWithSidebar(

  headerPanel(''),

  sidebarPanel(
     wellPanel(
         radioButtons('format', 'Format', c('CSV', 'TSV', 'XLSX')),
         uiOutput("radio"),
         fileInput('file', 'Data file')           
      ),

      wellPanel(
          selectInput('xLine', 'X', names(dataset)),
          selectInput('yLine', 'Y', names(dataset),  multiple=T)
      )
  ),
  mainPanel( 
      tabsetPanel(

          tabPanel("Line Graph", plotOutput('plotLine', height="auto"), value="line"),   
          id="tsp"            #id of tab
      )
   )
))

服务器.R

library(reshape2)
library(googleVis)
library(ggplot2)
library(plyr)
library(scales)
require(xlsx)
require(xlsxjars)
require(rJava)


options(shiny.maxRequestSize=-1)


shinyServer(function(input, output, session) {

data <- reactive({

    if (is.null(input$file))
      return(NULL)
    else if (identical(input$format, 'CSV'))
      return(read.csv(input$file$datapath))
    else if (identical(input$format, 'XLSX'))
      return(read.xlsx2(input$file$datapath, input$sheet))
    else
      return(read.delim(input$file$datapath))
  })

  output$radio <- reactiveUI(function() {
    if (input$format == 'XLSX') {
        numericInput(inputId = 'sheet',
                     label = "Pick Excel Sheet Index",1)
    }
  })

  observe({
    df <- data()
    str(names(df))
    if (!is.null(df)) {


      updateSelectInput(session, 'xLine', choices = names(df))
      updateSelectInput(session, 'yLine', choices = names(df))


    }
  })

output$plotLine <- renderPlot(height=650, units="px", {

    tempX <- input$xLine
    tempY <- input$yLine

    if (is.null(data()))
      return(NULL)
    if (is.null(tempY))
      return(NULL)

    widedata <- subset(data(), select = c(tempX, tempY))
    melted <- melt(widedata, id = tempX)
    p <- ggplot(melted, aes_string(x=names(melted)[1], y="value", group="variable", color="variable")) + geom_line() + geom_point()
    p <- p + opts(axis.text.x=theme_text(angle=45, hjust=1, vjust=1))
    p <- p + labs(title=paste("",tempX," VS ",tempY,""))

    print(p)
  })
})
4

1 回答 1

2

当绘图中的数据非常拥挤时,您可以执行以下操作:

  • 汇总您的数据,例如每年的平均值。
  • 子集您的数据,将您的数据限制在您感兴趣的变量/时间范围内。或者对您的数据进行二次抽样,随机抽取 1%。
  • 重新考虑你的图表。尝试提出一个替代可视化,它涵盖了您的假设,但不会弄乱您的图表。对于复杂的数据集(尽管棒球数据集的 8 MB 并不大),智能可视化是可行的方法。
于 2013-08-08T06:59:06.903 回答