rggplot2shinysoilggtern

Soil texture triangle won't draw


My soil texture triangle drawn from USDA data works outside of shiny but not inside, I am simply trying to get it to draw "base" here. It's not even reactive yet. Do you have any tips why I am getting an error:

Warning: Error in FUN: invalid 'type' (character) of argument.

The error seems to be happening when I move the code from outside shiny to inside shiny. I already made sure my packages weren't interacting.

library(ggplot2)
library(dplyr)
library(shinythemes)
library(tidyverse)
library(shiny)
library(aqp)
library(soilDB)
library(RColorBrewer)
library(latticeExtra)
library(reshape2)
library(sjPlot)
library(bslib)
library(ggtern)
library(grid)


# Define the UI for the app
ui <- fluidPage(
  theme = shinythemes::shinytheme("flatly"),
  titlePanel("Soil"),
  
  # Create the tabset panel
  tabsetPanel(
    
    tabPanel("Soil", 
             h3("Soil  Figures"),
             column(6, 
                    card(plotOutput("plot3"),
                         textOutput("plot3_caption")
                         
                    )
             )
    )
  )
  
  
)

# Define the server logic
server <- function(input, output, session) {
  
  output$plot3 <- renderPlot({
    
    # Put tile labels at the midpoint of each tile using dplyr instead of plyr
    USDA.LAB <- USDA %>%
      dplyr::group_by(Label) %>%
      dplyr::summarise(
        Clay = mean(Clay),
        Sand = mean(Sand),
        Silt = mean(Silt)
      )
    
    # Tweak
    USDA.LAB$Angle = 0
    USDA.LAB$Angle[which(USDA.LAB$Label == 'Loamy Sand')] = -35
    
    # Custom 12 colors palette
    custom_colors <- c(
      "#E5C4A1", 
      "#D47D35", # (Warm Orange)
      "#C0C0C0", #(Light Gray)
      "#9F7F4E", #(Earthy Brown)
      "#6A7F67", #(Muted Green)
      "#E2D9A7", #(Pastel Yellow)
      "#9D62A0", #(Lavender Purple)
      "#B67E5D", #(Dusty Coral)
      "#A9D6A1",#(Mint Green)
      "#D1A6D8", #(Pastel Pink)
      "#D4B97B", #(Golden Yellow)
      "#3E6E58" #(Dark Green)
    )
    
    # Construct the plot
    base <-  ggplot(data = USDA, aes(y=Clay, x=Sand, z=Silt)) +
      coord_tern(L="x",T="y",R="z") +
      geom_polygon(alpha = 0.75, size = 0.5, color = 'black',
                   aes(color=Label, fill=Label)) +
      scale_fill_manual(values = custom_colors) +   # Custom colors for fill
      scale_color_manual(values = custom_colors) +  # Custom colors for borders
      geom_text(data = USDA.LAB,
                aes(label = Label, angle = Angle),
                color = 'black',
                size = 3.5) +
      theme_rgbw() +
      theme_showsecondary() +
      theme_showarrows() +
      custom_percent("Percent") +
      theme(legend.justification = c(0, 1),
            legend.position      = c(0, 1),
            axis.tern.padding    = unit(0.15, 'npc')) +
      labs(title = 'USDA Textural Classification Chart',
           fill  = 'Textural Class',
           color = 'Textural Class')
    
    
    base 
    
    
  })
  
  output$plot3_caption <- renderText({
    "The plot above shows..."
  })
  
  
  
  
  
  
}

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

Solution

  • I can't fully explain why, but thanks to this issue reffering to this one , you need to print() explicitely your ggplot element by changing base with print(base) . It's seem to be an easy workaround. Those issues are old (2016-2018), I don't understand why it happen nowadays.

    Edit: This question seems to be a duplicate of this one, with a similar answer.