首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >发亮的ggplot2错误“结果必须是所有原子的,或者所有的数据帧”

发亮的ggplot2错误“结果必须是所有原子的,或者所有的数据帧”
EN

Stack Overflow用户
提问于 2015-06-06 22:09:13
回答 1查看 2.5K关注 0票数 2

我的闪闪发亮的应用程序出现了不一致的错误,我似乎找不出出了什么问题。这是最常见的错误。

代码语言:javascript
复制
Error: Results must be all atomic, or all data frames.

这个闪亮的应用程序基本上允许用户选择一个或多个文件,然后读取这些文件(列数不同),使用rbind.fill() (plyr)合并这些文件,然后将它们合并到melt(),然后到ggplot2。ggplot2使用面法绘制一个下面的图。

运行Win 8、R3.2.0、plyr_1.8.2 ggplot2_1.0.1、shiny_0.12.0和运行Ubuntu14.04、R3.2.0、shiny_0.12.0、plyr_1.8.2、ggplot2_1.0.1的服务器上的错误是相同的。

代码在下面。

代码语言:javascript
复制
#ui.R
shinyUI(fluidPage(

  titlePanel("Error test App"),

  sidebarLayout(
    sidebarPanel(
      selectInput("data", label = "Select files",
                  choices = c("file1.txt","file2.txt", "file3.txt","file4.txt","file5.txt"),
                  selected=NULL,
                  multiple=T)
    ),

    # Show a plot of the generated distribution
    mainPanel(
      imageOutput("plotoutput",width="100%",height="100%")
    )
  )
))

#server.R
library(ggplot2)
library(plyr)

options(shiny.trace=TRUE)

#plotfunction
#reads selected files and transforms them into a dataframe compatible to be read by ggplot and the plot is returned
plotfunction <- function(files = NULL, na.rm = TRUE)
{
  #loop to process selected files
  plist <- vector("list",length=length(files))
  for (i in 1:length(files))
  {
    df1 <- read.delim(file = files[i],header=F,stringsAsFactors=F)

    k <- ncol(df1)
    df1$Ind <- factor(1:nrow(df1))
    df1$Num <- factor(rep(i, nrow(df1)))
    plist[[i]] <- df1
  }

  #MOST LIKELY ERROR BLOCK ====================================================
  #combine list to one dataframe 
  df2 <- plyr::rbind.fill(plist)

  #melt
  df3 <- reshape2::melt(df2, id.var = c("Ind", "Num"))

  #ggplot
  p <- ggplot2::ggplot(data = df3, aes(x = Ind, y = value, fill = variable))+
    geom_bar(width = 1, space = 0, stat = "identity", position = "stack", na.rm = na.rm)+
    scale_x_discrete(expand = c(0, 0))+
    scale_y_continuous(expand = c(0, 0))+
    facet_grid(Num~.)+
    labs(x = NULL, y = NULL)+
    theme(legend.position = "none", panel.grid = element_blank(), panel.background = element_blank(), 
          axis.ticks = element_blank(), axis.text = element_blank(), axis.line = element_blank(), 
          axis.title = element_blank(),
          plot.margin = grid::unit(c(0.1, 0, 0, 0), "lines"),
          strip.text=element_blank())

  #MOST LIKELY ERROR BLOCK ====================================================

  return(p)
}

#shinyserver
shinyServer(function(input, output) {

  #renderimage
  output$plotoutput <- renderImage({
    fnvalidate <- function(input) {if(is.null(input)) print("Select one or more files.")}
    validate(fnvalidate(input=input$data))

    sp1 <- plotfunction(files=input$data)

    png("plot.png", height=2, width=6, res=200, units="cm")
    print(sp1)
    dev.off()

    return(list(src = "plot.png",
                contentType = "image/png",
                width = round((6*200)/2.54, 0),
                height = round((1*length(input$data)*200)/2.54, 0),
                alt = "plot"))

    },deleteFile=T)

})
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-11 20:55:40

这似乎是plyr的一个问题,它可能会在下一次R更新中得到修复。在此之前,您可以按照以下步骤修复它:

  1. 安装平台专用开发工具:

  1. 安装devtools install.packages("devtools")
  2. 编译并安装固定版本的plyr devtools::install_github("hadley/plyr")
  3. 重启R/Rstudio
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30688041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档