1

我正在尝试编写一个 Shiny 应用程序,它读取由三个组件系统组成的文件并使用 ggtern 包绘制三元相图。

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

错误:CoordTern 需要以下缺失的美学(tlr->xy):z

该应用程序由 fileInput 小部件和 renderUI 函数的三个 selectInput 小部件组成,以便用户可以从文件中为三元图的每个角选择变量。

我正在使用最新的库版本:ggplot2 3.3.0 和 ggtern 3.3.0

这是作为数据框的“.csv”文件的内容:

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

reprex 包(v0.2.1)于 2020-04-13 创建

下面是闪亮代码的代表:

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

4

1 回答 1

1

我从另一个堆栈溢出问题 ( https://stackoverflow.com/a/55102305/11234102 ) 中找到了我的问题的答案。尽管提供了答案以解决不同的问题,但它仍然有效。ggtern 函数需要放在 print() 命令中。

于 2020-04-18T18:05:54.553 回答