【问题标题】:Problems with Shiny PCA and ggbiplot coloringShiny PCA 和 ggbiplot 着色的问题
【发布时间】: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


【解决方案1】:

这是我正在使用的代码,它可以工作:

library(shiny)
library(ggbiplot)

ui <- 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 <- function(input, output, session) {
  upData <- reactive({if(is.null(input$file1)) return(mtcars) 
    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))
  })
}

shinyApp(ui = ui, server = server)

【讨论】:

  • 我刚刚运行了您的代码,这是一个轻微的改进 - 我现在收到“找不到对象”错误(其中列出了输入变量,如 cyl 等)。但是没有双标图,也没有颜色变化。我看不出这怎么会是系统错误,因为我重新启动了 R 并检查了所有依赖项。
  • 也许将selectInput 更改为renderUI 并在服务器端生成它,而不是通过观察/更新来查看您的版本中是否存在问题/错误。
  • 将服务器端切换到 output$variable &lt;- renderUI({ selectInput("variable", "Variable", choices=names(upData()) }) 和 ui 端切换到 uiOutput("variable") 会产生与以前完全相同的问题。我完全感到困惑。
【解决方案2】:

嗯,我终于明白了。

geom_point(aes_string(color=upData()[,input$variable]))

出于我对 R 的有限理解之外的任何原因,我不得不定义初始反应值 upData()

希望能帮助其他人避免几天的痛苦。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 1970-01-01
    • 2014-07-06
    • 1970-01-01
    • 2014-07-30
    相关资源
    最近更新 更多