0

我在 StackOverflow 上看到了很多关于aesShiny 映射问题的问题,其中大部分问题都是通过aes_string()在人们的代码中使用来解决的。不过,这些几乎完全与 x/y 值有关。

我的问题是在使用 ggbiplot 进行 PCA 时出现的,并且与映射着色变量有关。我的应用程序允许客户上传他们自己的文件,但我运行它mtcars只是为了确保它是一个可重现的错误(它是)。这是上传代码:

用户界面

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

服务器.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. 当我运行我的实际代码时,我得到一个“找不到对象'输入'”错误,所以我的玩具示例和我的实际问题并不完全相同,但我希望问题是相关的。

我查看了aesShiny 中的文档,并在 Google 上搜索了一个类似的问题,但一直未能找到解决方案。我很想这方面提供一些帮助。


使用 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>

4

2 回答 2

0

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

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)
于 2016-05-08T18:37:41.383 回答
0

嗯,我终于明白了。

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

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

希望这可以帮助其他人避免几天的痛苦。

于 2016-05-11T11:47:23.620 回答