【发布时间】:2016-05-08 18:02:39
【问题描述】:
我在 StackOverflow 上看到了很多关于 Shiny 中 aes 映射问题的问题,其中大部分问题都是通过在人们的代码中使用 aes_string() 来解决的。不过,这些几乎完全与 x/y 值有关。
我的问题是在使用 ggbiplot 进行 PCA 时出现的,并且与映射着色变量有关。我的应用程序允许客户上传他们自己的文件,但我使用mtcars 运行它只是为了确保它是一个可重现的错误(它是)。这是上传代码:
ui.R
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Where the Flip are my colors?"),
sidebarPanel(
fileInput('file1', 'Choose CSV File',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
tags$hr(),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator',c(Comma=',',Semicolon=';',Tab='\t'),','),
selectInput("variable",label = h5("Coloring Variable"),"")
),
mainPanel(
tableOutput("inputfile"),
plotOutput("PCA")
)
))
server.R
library(shiny)
shinyServer(function(input, output, session) {
upData <- reactive({if(is.null(input$file1))return(NULL)
inFile <- input$file1
dat <- read.csv(inFile$datapath)
return(dat)
})
output$inputfile <- renderTable({
upData()
})
observe({
updateSelectInput(
session,
"variable",
choices=names(upData()))
})
output$PCA <- renderPlot({
upData.pca <- prcomp(upData(), scale = TRUE)
ggbiplot(upData.pca, obs.scale = 1, var.scale = 1)
# + geom_point(aes_string(color=input$variable))
})
})
如果我按原样运行此代码,它会提供我想要的上传数据帧和 PCA 双图。但是,当我取消注释 geom_point 行时,我收到“一元运算符的参数无效”错误。我已经用 Shiny 完成了这个,并且代码在 mtcars 上运行良好。当我运行我的实际代码时,我得到一个“找不到对象'输入'”错误,所以我的玩具示例和我的实际问题并不完全相同,但我希望问题是相关的。
我查看了 Shiny 中 aes 上的文档,并在 Google 上搜索了一个类似的问题,但一直未能找到解决方案。我会LOVE为此提供一些帮助。
使用 Gopala 的代码,我看到的错误是:
Warning: Error in [[: subscript out of bounds
Stack trace (innermost first):
84: FUN
83: lapply
82: aes_string
81: layer
80: geom_point
79: renderPlot [#17]
71: output$PCA
4: <Anonymous>
3: do.call
2: print.shiny.appobj
1: <Promise>
Don't know how to automatically pick scale for object of type tbl_df/tbl/data.frame. Defaulting to continuous.
Warning: Error in : Aesthetics must be either length 1 or the same as the data (32): colour, x, y
Stack trace (innermost first):
71: output$PCA
4: <Anonymous>
3: do.call
2: print.shiny.appobj
1: <Promise>
Warning: Error in eval: object 'drat' not found
Stack trace (innermost first):
71: output$PCA
4: <Anonymous>
3: do.call
2: print.shiny.appobj
1: <Promise>
【问题讨论】:
-
当我从响应式返回
mtcars代替 NULL 时,我可以在不上传文件的情况下进行测试等,它对我来说很好。 `packageVersion('shiny')在我的例子中是[1] ‘0.12.2’ -
这很奇怪 - 我只是做了同样的事情,它再次返回“一元运算符的无效参数”错误。我的版本是 0.13.2,所以现在我很困惑。您可以根据输入变量对点进行反应性着色吗?
-
是的,选择 mpg 或 cyl 会更改颜色和图例。我收到一些下标超出范围的解析错误,但这并没有阻止应用程序中的任何内容正常工作。尚未调试该错误。
标签: r ggplot2 shiny pca ggbiplot