缺少 shiny 和 ggtern 的美学

missing aesthetics with shiny and ggtern

我想使用 Shiny 创建一个三元图仪表板。但是我陷入了这个错误。此代码采用 CSV 文件并更新三个下拉输入。然后创建的三元图具有这三个下拉值。

问题是在将“geom_point()”添加到 obj 时出现此错误 -> CoordTern 需要以下缺失的美学 (tlr->xy):z

想添加时出现相同但不同的问题 stat_density_tern 我收到此错误 -> stat_density_tern 需要以下缺失的美学:z

library(shiny)
library(colourpicker)
library(ggplot2)
library(ggtern)

ui <- fluidPage(

    titlePanel("TernaryPlot"),
    fileInput("csv_file", "source", multiple = FALSE, accept = "csv",
              width = NULL, buttonLabel = "Browse...",
              placeholder = "No file selected"),
    
    selectInput("x_val", "Choose x values:", choices=c()),
    selectInput("y_val", "Choose y values:", choices=c()),
    selectInput("z_val", "Choose z values:", choices=c()),
    
    checkboxInput("show_point", "points", value = FALSE, width = NULL),
    checkboxInput("show_dens", "density", value = FALSE, width = NULL),
    plotOutput("ggtern")
    )
    
server <- function(session,input, output) {
    
    selectedData <- reactive({
        inFile <- input$csv_file
        if (is.null(inFile))
            return(NULL)
        df <- read.csv(inFile$datapath,na.strings = c("", "NA", "#N/A"))
        updateSelectInput(session,"x_val","x series",colnames(df))
        updateSelectInput(session,"y_val","y series",colnames(df))
        updateSelectInput(session,"z_val","z series",colnames(df))
        return(df)
        })
    
    T1 <- reactive({
        if (is.null(selectedData()))
            return(NULL)
        temp <- selectedData()[input$x_val]
        return (unlist(temp))
    })
    
    T2 <- reactive({
        if (is.null(selectedData()))
            return(NULL)
        temp <- selectedData()[input$y_val]
        return (unlist(temp))
    })
    
    T3 <- reactive({
        if (is.null(selectedData()))
            return(NULL)
        temp <- selectedData()[input$z_val]
        return (unlist(temp))
    })
    

    output$ggtern <- renderPlot({
        
        if (is.null(selectedData()))
            return(NULL)
        if (is.null(T1()))
            return(NULL)
        
        x_s <- T1()
        y_s <- T2()
        z_s <- T3()
        vec <- aes_string(x_s,y_s,z_s)
        
        obj <- ggtern(data=selectedData(), vec)
        if (input$show_point)
            obj <- obj + geom_point()
        if (input$show_dens)
            obj <- obj + stat_density_tern(aes(fill=..level.., alpha=..level..), geom='polygon')
        print(obj)
    })
}

shinyApp(ui = ui, server = server)

我可以在基本 R 中执行此操作,但是当我尝试将其作为 Shiny 的一部分实现时,我遇到了错误,我不确定如何修复。

为了使您的应用正常运行,您可以通过删除重复表达式 Tx 并通过 aes_string 将选定的列直接传递给 ggtern 来简化代码。此外,为了使 ggterngeom_pointgeom_density 一起工作,我们必须命名美学。试试这个:

server <- function(session,input, output) {
  
  selectedData <- reactive({
    inFile <- input$csv_file
    if (is.null(inFile))
      return(NULL)
    df <- read.csv(inFile$datapath,na.strings = c("", "NA", "#N/A"))
    updateSelectInput(session,"x_val","x series",colnames(df))
    updateSelectInput(session,"y_val","y series",colnames(df))
    updateSelectInput(session,"z_val","z series",colnames(df))
    return(df)
  })
  
  output$ggtern <- renderPlot({
    
    if (is.null(selectedData()))
      return(NULL)
    
    req(input$x_val)
    req(input$y_val)
    req(input$z_val)
    
    obj <- ggtern(data=selectedData(), aes_string(x = input$x_val, y = input$y_val, z = input$z_val))
    
    if (input$show_point)
      obj <- obj + geom_point()
    if (input$show_dens)
      obj <- obj + stat_density_tern(aes(fill=..level.., alpha=..level..), geom='polygon')
    print(obj)
  })
}