R 闪亮的 ggplot2 错误'Results must be all atomic, or all data frames'



我得到不一致的错误与我闪亮的应用程序,我似乎无法找出什么是错的。这是最常见的错误。

Error: Results must be all atomic, or all data frames.

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

在我的电脑上运行win8, R 3.2.0, plyr_1.8.2, ggplot2_1.0.1, shiny_0.12.0和服务器上运行Ubuntu 14.04, R 3.2.0, shiny_0.12.0, plyr_1.8.2, ggplot2_1.0.1.

代码如下:

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

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

  1. 安装平台特定的开发工具:
    • Windows:从http://cran.r-project.org/bin/windows/Rtools/
    • 下载并安装Rtools33.exe
    • Ubuntu或Debian Linux: sudo apt-get install r-base-devel
    • rpm Linux: install r-devel
    • Mac:从https://developer.apple.com/downloads
  2. 安装Xcode命令行工具 安装devtools
  3. install.packages (devtools)

  4. 编译并安装固定版本的plyr

    devtools: install_github("哈德利/plyr")

  5. 重启R/Rstudio

最新更新