r - 如何扩展我的 R Shiny 应用程序以获得更大的数据输入?

标签 r ggplot2 shiny

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

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

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

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

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

ui.R

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

最佳答案

当绘图中的数据非常拥挤时,您可以做一些事情:

  • 汇总您的数据,例如平均每年。
  • 对数据进行子集化,将数据限制为您感兴趣的变量/时间跨度。或者对您的数据进行二次抽样,例如随机抽取 1%。
  • 重新考虑您的图表。试着想出一个替代的可视化来涵盖你的假设,但不会弄乱你的图表。对于复杂的数据集(尽管 8 MB 的棒球数据集并不大),智能可视化是必经之路。

关于r - 如何扩展我的 R Shiny 应用程序以获得更大的数据输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18119510/

相关文章:

r - 解析字符中的数值,因为它们与 `scipen` 的值无关

r - 排除 ggplot2 上图例条目中的箭头

javascript - R Shiny 传单 javascript 插件 - 热图

在鼠标单击事件中删除 Shiny::textInput 字段中的文本

ggplot2 geom_order中的反向堆叠顺序

javascript - IGraph 中的 VisNetwork - 无法实现顶点的簇颜色

r - 如何在R中找到上一个星期日

r - 根据键在数据框中汇总值

javascript - 在 Shiny DataTable 中展开和折叠子行

当 reshape 无法猜测时变变量的名称时, reshape r 中的数据