0

I am working on a box plot maker for my group and want to include all of our data in the maker. So I made a data.frame in the code that holds the data, but I think I put it in the wrong place. Each time I run the code on my end it works perfectly but when I publish using Shiny and run it on their end, I get the Warning: Error in eval: object 'Percents' not found error (telling me it can't find the data?) If anyone can I help, that'd be great, here's the code:

library(shiny)
All_Data <- data.frame(
    Name = as.character(c("Jeff","Bob","Greg")),
    Year = c(2015,2015,2015),
    Bacteria = c("A","B","C"),
    Site = c("C1","C1","C1"),
    Percents = c(1,2,4),
stringsasfactors = TRUE
)
ndata <-data.frame(
    Name = as.character("Preset"),
    Year = c("0"),
    Bacteria = as.factor(c("0")),
    Site = as.factor(c("0")),
    Percents = as.numeric(c("0")),
    stringsAsFactors = TRUE
)
Names <- c("2015 Acidobacteria C2", "2015 Actinobacteria C2","2015 Alpha-Proteobacteria C2", "2015 Bacteriodetes C2","2015 Verrucomicrobia C2","2016 Acidobacteria C2", "2016 Actinobacteria C2","2016 Alpha-Proteobacteria C2", "2016 Bacteriodetes C2","2016 Verrucomicrobia C2","2018 Acidobacteria C2", "2018 Actinobacteria C2","2018 Alpha-Proteobacteria C2", "2018 Bacteriodetes C2","2018 Verrucomicrobia C2","2019 Acidobacteria C2", "2019 Actinobacteria C2","2019 Alpha-Proteobacteria C2", "2019 Bacteriodetes C2","2019 Verrucomicrobia C2")
Years <- c("2015","2016","2018","2019")
names(Years)<-c("2015 C2","2016 C2","2018 C2","2019 C2")
Sites <- c("","C2")
# Define UI for application
ui <- fluidPage(
    
    # Application title
    titlePanel("ISAMR DNA Group Boxplot Maker"),
    
    # Sidebar
    sidebarLayout(
        sidebarPanel(width = 4,
            h3("Which Data Sets Would You Like to Compare?"),
            selectInput("Set1", "", choices = c(Sites, Years, Names)),
            selectInput("Set2", "",choices = c(Sites, Years, Names)),
            selectInput("factor","Based on Which Factor?", choices = c("Year", "Site", "Bacteria", "Site by Year", "Bacteria by Year", "Bacteria by Site by Year")),
            textInput("title", h4("Title"), placeholder = "Enter title..."),
            numericInput("numcol","How many colors?",value=1,min=1,max=5),
            h4("Pick the colors you want, leave the rest blank."),
            selectInput("col1","",choices = c("",colors())),
            selectInput("col2","",choices = c("",colors())),
            selectInput("col3","",choices = c("",colors())),
            selectInput("col4","",choices = c("",colors())),
            selectInput("col5","",choices = c("",colors())),
            actionButton("Submit", "Submit")
        ),
        
        # Show a chosen plot
        mainPanel(
            #uiOutput("data"),
            DT::dataTableOutput("data"),
            plotOutput("plot")
        )
    )
)

# Define server logic
server <- function(input, output) {
    
    ndata<-eventReactive(input$Submit, {req(input$Submit)
        
        if (input$Set1 %in% Names & input$Set2 %in% Names){
        ndata <- filter(All_Data, All_Data$Name == input$Set1 | All_Data$Name == input$Set2)
        }
        else if (input$Set1 %in% Names & input$Set1 %in% Years){
            ndata <- filter(All_Data, All_Data$Name == input$Set1 | All_Data$Year == input$Set2)
        }
        else if (input$Set1 %in% Names & input$Set1 %in% Sites){
            ndata <- filter(All_Data, All_Data$Name == input$Set1 | All_Data$Site == input$Set2)
        }
        else if (input$Set1 %in% Years & input$Set1 %in% Names){
            ndata <- filter(All_Data, All_Data$Site == input$Set1 | All_Data$Name == input$Set2)
        }
        else if (input$Set1 %in% Years & input$Set2 %in% Years){
            ndata <- filter(All_Data, All_Data$Year == input$Set1 | All_Data$Year == input$Set2)
        }
        else if (input$Set1 %in% Years & input$Set2 %in% Sites){
            ndata <- filter(All_Data, All_Data$Year == input$Set1 | All_Data$Site == input$Set2)
        }
        else if (input$Set1 %in% Sites & input$Set1 %in% Names){
            ndata <- filter(All_Data, All_Data$Site == input$Set1 | All_Data$Name == input$Set2)
        }
        else if (input$Set1 %in% Sites & input$Set1 %in% Years){
            ndata <- filter(All_Data, All_Data$Site == input$Set1 | All_Data$Year == input$Set2)
        }
        else if (input$Set1 %in% Sites & input$Set1 %in% Sites){
            ndata <- filter(All_Data, All_Data$Site == input$Set1 | All_Data$Site == input$Set2)
        }   })

        output$data <- DT::renderDataTable({
            ndata()
        })
observeEvent(input$numcol,{req(input$numcol)
        if (input$numcol == 1){coll<-c(input$col1)}
        if (input$numcol == 2){coll<-c(input$col1,input$col2)}
        if (input$numcol == 3){coll<-c(input$col1,input$col2,input$col13)}
        if (input$numcol == 4){coll<-c(input$col1,input$col2,input$col3,input$col4)}
        if (input$numcol == 5){coll<-c(input$col1,input$col2,input$col3,input$col4,input$col5)}
})    
    
observeEvent(input$factor,{req(input$factor)
        if (input$factor == "Year"){
            output$plot <- renderPlot(
            boxplot(Percents ~ Year, data = ndata(), main = input$title, ylab = "Relative Abundance", col = coll))
            }
        
        else if (input$factor == "Site"){
            output$plot <- renderPlot(
                boxplot(Percents ~ Site, data = ndata(), main = input$title, ylab = "Relative Abundance", col = coll))   
        }
        
        else if (input$factor == "Bacteria"){
            output$plot <- renderPlot(
                boxplot(Percents ~ Bacteria, data = ndata(), main = input$title, ylab = "Relative Abundance", col = coll))   
        }
        
        else if (input$factor == "Site by Year"){
            output$plot <- renderPlot(
                boxplot(Percents ~ Year%in%Site, data = ndata(), main = input$title, ylab = "Relative Abundance", col = coll))   
        }
        
        else if (input$factor == "Bacteria by Year"){
            output$plot <- renderPlot(
                boxplot(Percents ~ Year%in%Bacteria, data = ndata(), main = input$title, ylab = "Relative Abundance", col = coll))   
        }

        else if (input$factor == "Bacteria by Site by Year"){
            output$plot <- renderPlot(
                boxplot(Percents ~ Year%in%Site%in%Bacteria, data = ndata(), main = input$title, ylab = "Relative Abundance", col = coll))   
        }
})
}

# Run the application 
shinyApp(ui = ui, server = server)

(I didn't include the real data)

2 Answers2

0

in your All_Data creation, stringsasfactors = True needs to be stringsasfactors = TRUE or atleast T. Also would advice using the placeholder arguement in textInput like textInput("title", h4("Title"), placeholder = "Enter title...")

I am wondering if the Percents error is due to my above advice as all the values weren't correctly assigned

Daniel_j_iii
  • 3,041
  • 2
  • 11
  • 27
  • It seems like that would be the error, but it didn't change the problem. – Sam Ferraro Aug 06 '20 at 17:41
  • Apologize, I didn't see the warning first time. Are you sure you you need the observeEvent? what if you used a `eventReactive` function? I think the issue is dataset is not getting passed on after the observeEvent, or atleast not being passed on how Shiny needs to be able to input it into your boxplots. [see here](https://stackoverflow.com/questions/33519816/shiny-what-is-the-difference-between-observeevent-and-eventreactive) – Daniel_j_iii Aug 06 '20 at 18:08
  • I'll try that, but why would this make the app work perfectly before I publish it, but not afterward? – Sam Ferraro Aug 06 '20 at 18:34
  • When You work in your Rsession , you create variables, reassign values and everything works, but then you close out and didn't save individual lines of code ran in the console or didn't save them in the script while creating you shiny app. That happens to me – Daniel_j_iii Aug 06 '20 at 18:48
  • When I replaced `observeEvent` with `eventReactive`, the program lost all output. When I looked at the requirements for the 2 functions, they seemed the same, is there something I have to change? : `eventReactive(input$Submit, {...})` – Sam Ferraro Aug 06 '20 at 18:53
0

You have a few issues here. You need to define ndata reactive dataframe layer by layer making sure that each layer gives you what you expect. Instead of input$Set1 %in% Years, it should be Years %in% input$Set1. I will leave it to you to fix it as it has numerous conditions, and some are repeating. I will just use airquality data. Lastly, please define output$plot only once. You can plot conditionally inside. I am just showing you that the program works. Please see below. You should be able to adapt it.

ui <- fluidPage(
  
  # Application title
  titlePanel("ISAMR DNA Group Boxplot Maker"),
  
  # Sidebar
  sidebarLayout(
    sidebarPanel(width = 4,
                 h3("Which Data Sets Would You Like to Compare?"),
                 selectInput("Set1", "", choices = c(Sites, Years, Names)),
                 selectInput("Set2", "",choices = c(Sites, Years, Names)),
                 selectInput("factor","Based on Which Factor?", choices = c("Year", "Site", "Bacteria", "Site by Year", "Bacteria by Year", "Bacteria by Site by Year")),
                 textInput("title", h4("Title"), value = "Enter title..."),
                 numericInput("numcol","How many colors?",value=1,min=1,max=5),
                 h4("Pick the colors you want, leave the rest blank."),
                 selectInput("col1","",choices = c("",colors())),
                 selectInput("col2","",choices = c("",colors())),
                 selectInput("col3","",choices = c("",colors())),
                 selectInput("col4","",choices = c("",colors())),
                 selectInput("col5","",choices = c("",colors())),
                 actionButton("Submit", "Submit")
    ),
    
    # Show a chosen plot
    mainPanel(
      
      #uiOutput("data"),
      DT::dataTableOutput("data")
      ,plotOutput("plot")
    )
  )
)

# Define server logic
server <- function(input, output) {
  
  ndata <- eventReactive(input$Submit, {
    req(input$Submit,input$Set1,input$Set2)
    ndata <- airquality
    # if (Names %in% input$Set1 & Names %in% input$Set2 ){
    #   ndata <- filter(All_Data, All_Data$Name == input$Set1 | All_Data$Name == input$Set2)
    # }
    # else if (Names %in% input$Set1 & Years %in% input$Set1){
    #   ndata <- filter(All_Data, All_Data$Name == input$Set1 | All_Data$Year == input$Set2)
    # }
    # else if (Names %in% input$Set1 & Sites %in% input$Set1){
    #   ndata <- filter(All_Data, All_Data$Name == input$Set1 | All_Data$Site == input$Set2)
    # }
    
    # else if (input$Set1 %in% Years & input$Set1 %in% Names){
    #   ndata <- filter(All_Data, All_Data$Site == input$Set1 | All_Data$Name == input$Set2)
    # }
    # else if (input$Set1 %in% Years & input$Set2 %in% Years){
    #   ndata <- filter(All_Data, All_Data$Year == input$Set1 | All_Data$Year == input$Set2)
    # }
    # else if (input$Set1 %in% Years & input$Set2 %in% Sites){
    #   ndata <- filter(All_Data, All_Data$Year == input$Set1 | All_Data$Site == input$Set2)
    # }
    # else if (input$Set1 %in% Sites & input$Set1 %in% Names){
    #   ndata <- filter(All_Data, All_Data$Site == input$Set1 | All_Data$Name == input$Set2)
    # }
    # else if (input$Set1 %in% Sites & input$Set1 %in% Years){
    #   ndata <- filter(All_Data, All_Data$Site == input$Set1 | All_Data$Year == input$Set2)
    # }
    # else if (input$Set1 %in% Sites & input$Set1 %in% Sites){
    #   ndata <- filter(All_Data, All_Data$Site == input$Set1 | All_Data$Site == input$Set2)
    # }
    
    return(ndata)
  })
  
    output$data <- DT::renderDataTable({
      ndata()
    })
    
    
    observeEvent(input$numcol, {
      req(input$numcol)
      if (input$numcol == 1){coll<-c(input$col1)}
      if (input$numcol == 2){coll<-c(input$col1,input$col2)}
      if (input$numcol == 3){coll<-c(input$col1,input$col2,input$col13)}
      if (input$numcol == 4){coll<-c(input$col1,input$col2,input$col3,input$col4)}
      if (input$numcol == 5){coll<-c(input$col1,input$col2,input$col3,input$col4,input$col5)}
    })
    
    
    observeEvent(input$factor, {
      req(input$factor)
      output$plot <- renderPlot({
        boxplot(Temp~Month,
                data=ndata(),
                main="Different boxplots for each month",
                xlab="Month Number",
                ylab="Degree Fahrenheit",
                col="orange",
                border="brown"
        )
        # if (input$factor == "Year"){
        #   boxplot(Percents~Year, data = ndata(), main = input$title, ylab = "Relative Abundance", col = coll)
        # }
        #
        # else if (input$factor == "Site"){
        #   boxplot(Percents ~ Site, data = ndata(), main = input$title, ylab = "Relative Abundance", col = coll)   
        # }
        # 
        # else if (input$factor == "Bacteria"){
        #   boxplot(Percents ~ Bacteria, data = ndata(), main = input$title, ylab = "Relative Abundance", col = coll)  
        # }
      })
      
      
      # else if (input$factor == "Site by Year"){
      #   output$plot <- renderPlot(
      #     boxplot(Percents ~ Year%in%Site, data = ndata, main = input$title, ylab = "Relative Abundance", col = coll))   
      # }
      # 
      # else if (input$factor == "Bacteria by Year"){
      #   output$plot <- renderPlot(
      #     boxplot(Percents ~ Year%in%Bacteria, data = ndata, main = input$title, ylab = "Relative Abundance", col = coll))   
      # }
      # 
      # else if (input$factor == "Bacteria by Site by Year"){
      #   output$plot <- renderPlot(
      #     boxplot(Percents ~ Year%in%Site%in%Bacteria, data = ndata, main = input$title, ylab = "Relative Abundance", col = coll))   
      # }
    })
    
}

# Run the application 
shinyApp(ui = ui, server = server)

output

enter image description here

YBS
  • 19,324
  • 2
  • 9
  • 27
  • Your changes make sense to me, but now I am receiving the `Warning: Error in rep: attempt to replicate an object of type 'closure'` which I'm pretty sure is a problem with the new `ndata()` function. – Sam Ferraro Aug 06 '20 at 21:39
  • Please update your question with the updated code so I can review. – YBS Aug 06 '20 at 21:53
  • Thank you for your suggestion about `%in%`. However, it didn't fix the problem that causes the `Error in rep: attempt to replicate an object of type 'closure'` warning to arise. – Sam Ferraro Aug 08 '20 at 16:15
  • @SamFerraro, The above code is working for you as is? If so, you can replace airquality data with ndata. Please ensure that your filtering is working for one group first. That may be the source of the issue. – YBS Aug 08 '20 at 20:47