首页 文章

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

提问于
浏览
3

我正在制作一个使用ggplot2的R闪亮应用程序 . 此应用程序接收用户上传的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
      )
   )
))

server.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 回答

  • 2

    当情节非常拥挤时,您可以做一些事情:

    • 汇总您的数据,例如平均每年 .

    • 对数据进行子集,将数据限制为您感兴趣的变量/时间 Span . 或者对您的数据进行二次采样,随机抽取,例如1% .

    • 重新思考你的图表 . 尝试提出一个涵盖您的假设的替代可视化,但不会使您的图形混乱 . 对于复杂的数据集(尽管棒球数据集的8 MB并不大),智能可视化是最佳选择 .

相关问题