我想使用 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 的一部分来实现时,我得到了错误,我不确定如何修复。