如何使用ggvis和gl

时间:2015-05-24 17:21:24

标签: r shiny dplyr ggvis

我正在使用ggvis编写一个Shiny应用程序。但是,当我决定使用ggvis(和dplyr)而不是ggplot2时,我在处理 NULL 值时遇到了一些问题。该应用程序的想法是加载文件,显示文件的列,以便用户可以选择在X和Y上绘制的内容,而不是使用ggvis显示它。我的服务器.R:

library(shiny)
library(plyr)
library(ggplot2)
library(googleVis)
library(reshape2)
library(ggvis)

    options(shiny.maxRequestSize=30*1024^2)

shinyServer(function(input, output, session) {


  Data <- reactive({
    inFile <- input$file1

    if (is.null(inFile)) return(NULL)
    if (input$datatype == "tel") {
      mydata <- read.csv(inFile$datapath, skip=1, header=TRUE, sep=',')
      source("clean_data.r", local=TRUE)  
    }
    info <- list(mydata=mydata, all.names)
    return(info)
  })

  output$chooseX <- renderUI({
    df <- Data()$mydata
    if (is.null(df)) return(NULL)
    items=names(df)
    #names(items)=items
    selectInput("chooseX","Select X axis:",items, items[[117]])
  })

  output$chooseY <- renderUI({
    df <- Data()$mydata
    if (is.null(df)) return(NULL)
    items=names(df)
    selectInput("chooseY","Select Y axis:",items, items[[49]])
  })

  # allows pageability and number of rows setting
  myOptions <- reactive({  
    list(
      page=ifelse(input$pageable==TRUE,'enable','disable'),
      pageSize=input$pagesize
    ) 
  } )

  output$raw <- renderGvis({
    if (is.null(input$file1)) { return() }
    gvisTable(Data()$mydata[,1:50],options=myOptions()) 

  })

  reactive({
    if (is.null(input$file1)) { return(NULL) }
    df <- Data()$mydata
    if(!is.null(input$chooseX)) { x <- paste(as.character(input$chooseX)) }
    if(!is.null(input$chooseX)) { y <- paste(as.character(input$chooseY)) }
    tel <- df %>% ggvis(~x, ~y) %>% layer_lines() %>%
      set_options(width=400, height=600, keep_aspect=TRUE) %>%
      bind_shiny("tel")
  })

  output$caption1 <- renderText( {
    if (is.null(input$file1)) { return() }
    "Cleaned data"
  })

})

在clean_data.R中,我只对数据执行一些操作。输出是

structure(list(tel1= c(4.44445, 4.23688, 4.45421, 4.65202, 4.44689, 
6.23443, 7.43101, 7.42124, 4.54701, 4.23688, 4.41758, 4.84493, 
4.55189, 4.45421, 4.337, 4.65202, 4.54945, 4.45177, 4.43956, 
4.34432), tel2= c(3.33089, 3.33089, 3.33334, 3.33334, 3.33089, 
4.12943, 5.45054, 6.64957, 5.23565, 4.27839, 3.72894, 3.52625, 
3.43102, 3.43102, 3.32845, 3.33334, 3.32845, 3.33334, 3.33334, 
3.32845), tel3= c(1.11356, 0.90599, 1.12087, 1.31868, 1.116, 
2.105, 1.98047, 0.77167, -0.688639999999999, -0.0415099999999997, 
0.68864, 1.31868, 1.12087, 1.02319, 1.00855, 1.31868, 1.221, 
1.11843, 1.10622, 1.01587)), .Names = c("tel1", "tel2", "tel2"
), row.names = c(NA, 20L), class = "data.frame")

在ui.R中我只有一个简单的ggvisOutput("tel")

library(shiny)
library(ggvis)

shinyUI(pageWithSidebar(
  headerPanel("Prototype v0.2"),

  sidebarPanel(
    fileInput('file1', 'Choose CSV File'),

    radioButtons('datatype', 'Data Type',c('tel'='tel','Corrective Actions'="corrections"), 'tel'),
    tags$head(tags$style(type="text/css",
                         "label.radio { display: inline-block; margin:0 10 0 0;  }",
                         ".radio input[type=\"radio\"] { float: none; }"))
  ),
  mainPanel(
    tabsetPanel(
      tabPanel("Data Information",
               h4(textOutput("caption1")),
               checkboxInput(inputId = "pageable", label = "Pageable"),
               conditionalPanel("input.pageable==true",
                                numericInput(inputId = "pagesize",
                                             label = "Pupils per page",value=20,min=1,max=25)),

               htmlOutput("raw"),
               value = 1),
      tabPanel("Tel Data",
               ggvisOutput("tel"))


    )
  )) )

在使用ggvis之前,一切都运行良好但似乎ggvis在处理NULL值时遇到了一些问题。我检查了https://groups.google.com/forum/#!msg/ggvis/BBTABX01RoQ/1UQMqgysVd0Jhttps://github.com/rstudio/ggvis/issues/333,但我仍然无法找到解决方法。错误消息是:

Error in UseMethod("rename_") : 
  no applicable method for 'rename_' applied to an object of class "factor"

我打开了options(shiny.trace=TRUE),然后我收到了:

    SEND {"errors":[],"values":{"chooseX":null,"chooseY":null,"chooseY2":null,"caption1":"","y2axis":null,"raw":""},"inputMessages":[]}

SEND {"errors":{"chooseX":{"message":"no applicable method for 'rename_' applied to an object of class \"factor\"","call":"UseMethod(\"rename_\")","type":null},"chooseY":{"message":"no applicable method for 'rename_' applied to an object of class \"factor\"","call":"UseMethod(\"rename_\")","type":null},"chooseY2":{"message":"no applicable method for 'rename_' applied to an object of class \"factor\"","call":"UseMethod(\"rename_\")","type":null},"y2axis":{"message":"no applicable method for 'rename_' applied to an object of class \"factor\"","call":"UseMethod(\"rename_\")","type":null},"raw":{"message":"no applicable method for 'rename_' applied to an object of class \"factor\"","call":"UseMethod(\"rename_\")","type":null}},"values":{"caption1":"Cleaned data"},"inputMessages":[]}

任何解决方法的想法?非常感谢你!

0 个答案:

没有答案