I am trying to write a Shiny app that reads a file with the composition of three component systems and plots the ternary phase diagram using the ggtern package.
I can do this in basic R but when trying to implement it as part of Shiny I get and error which I am not sure how to fix:
Error:CoordTern requires the following missing aesthetics (tlr->xy) : z
The app consists of fileInput widget and three selectInput widgets part of a renderUI functions so that the user can select the variable for each corner of the ternary plot from the file.
I am using the latest library versions: ggplot2 3.3.0 and ggtern 3.3.0
This is the content of the ‘.csv’ file as a data frame:
df <- data.frame("Comp1" = c(0.3, 0.5, 0.6, 0.75, 0.8),
"Comp2" = c(0.3, 0.25, 0.15, 0.15, 0.1),
"Comp3" = c(0.4, 0.25, 0.25, 0.1, 0.1),
"Value" = c(300, 500, 1200, 2500, 4500))
Created on 2020-04-13 by the reprex package (v0.2.1)
Below is the reprex of the shiny code:
#
# Ternary Phase Plot
#
library(shiny)
library(ggplot2)
library(ggtern)
#> --
#> Remember to cite, run citation(package = 'ggtern') for further info.
#> --
#>
#> Attaching package: 'ggtern'
#> The following objects are masked from 'package:ggplot2':
#>
#> aes, annotate, ggplot, ggplot_build, ggplot_gtable,
#> ggplotGrob, ggsave, layer_data, theme_bw, theme_classic,
#> theme_dark, theme_gray, theme_light, theme_linedraw,
#> theme_minimal, theme_void
library(DT)
#>
#> Attaching package: 'DT'
#> The following objects are masked from 'package:shiny':
#>
#> dataTableOutput, renderDataTable
ui <- fluidPage(
titlePanel("Ternary Phase Plot"),
sidebarLayout(
sidebarPanel(
fileInput("file","Upload '.csv' file"),
br(),
uiOutput("vx"), # vx is coming from renderUI in server
br(),
uiOutput("vy"), # vy is coming from renderUI in server
br(),
uiOutput("vz") # vy is coming from renderUI in server
),
mainPanel(
plotOutput("plot", height = "800px"),
DT::dataTableOutput("table")
)
)
)
server <- function(input, output, session) {
# get the names of the variables from the data file and used them in the selectInput part of the renderUI function
vars <- reactive({
req(input$file)
names(read.csv(input$file$datapath,
header = TRUE,
sep = ","))
})
# read the data file
df <- reactive({
req(input$file)
read.csv(input$file$datapath,
header = TRUE,
sep = ",")
})
output$vx <- renderUI({
selectInput("varx", "Select the 1st (L) component", choices = vars())
})
output$vy <- renderUI({
selectInput("vary", "Select the 2nd (T) component", choices = vars())
})
output$vz <- renderUI({
selectInput("varz", "Select the 3rd (R) component", choices = vars())
})
# render plot
output$plot <- renderPlot({
ggtern(df(), aes_string(x=input$varx, y=input$vary, z=input$varz)) +
geom_point(aes(fill=Value), color="black",shape=21, size=6) +
labs(fill="Value")
})
# print dataframe in table to confirm it is read properly
output$table <- DT::renderDataTable(
df()
)
}
# Run the application
shinyApp(ui = ui, server = server)
I found the answer to my problem from another stack overflow question (https://stackoverflow.com/a/55102305/11234102). It worked although the answer was provided to resolve a different problem. The ggtern function needs to be put in a print() command.