mindlessgreen
mindlessgreen

Reputation: 12142

R shiny ggplot2 error 'Results must be all atomic, or all data frames'

I get inconsistent errors with my shiny app and I can't seem to figure out what is wrong. This is the most common error.

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

The shiny app basically allows the user to chose 1 or more files, then reads those files (having differing number of columns), merges them using rbind.fill() (plyr) and then to melt() and then to ggplot2. ggplot2 plots one below the other using faceting.

The errors are same on my computer running Win 8, R 3.2.0, plyr_1.8.2 ggplot2_1.0.1, shiny_0.12.0 and on the Server running Ubuntu 14.04, R 3.2.0, shiny_0.12.0, plyr_1.8.2, ggplot2_1.0.1.

The code is below.

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

})

Upvotes: 2

Views: 2587

Answers (1)

cbologa
cbologa

Reputation: 46

It seems to be a problem with plyr, which is probably going to get fixed in the next R update. Until then you can fix it following these steps:

  1. Install platform specific development tools:
  2. Install devtools

    install.packages("devtools")

  3. Compile and install the fixed version of plyr

    devtools::install_github("hadley/plyr")

  4. RESTART R/Rstudio

Upvotes: 3

Related Questions