交换点颜色闪亮

时间:2016-12-07 18:49:59

标签: r ggplot2 shiny

我有一个闪亮的应用程序,显示ggplot的情节。我想制作一个下拉菜单,让用户可以在两种颜色方案之间切换。用户界面:

pageWithSidebar(
    headerPanel('PCA'),
    sidebarPanel(
       selectInput('colorBy', 'Color By', c('red', 'blue'))
    ),
    mainPanel(
      plotOutput('plot1')
    )
 )  

服务器代码(包括一些伪代码):

function(input, output, session) {

  # create new columns with alternate color schemes
  iris$red = rep('red', (dim(iris)[1]))
  iris$blue = rep('blue', (dim(iris)[1]))

  #FIXME  
  # set the color scheme 
  colorScheme <- reactive({
    if (input$colorBy == 'red'){
      iris$red
    }

    else{
      iris$blue
    }
  })

 # make the plot
 output$plot1 <- renderPlot({
   ggplot(iris, aes(Sepal.Length, Sepal.Width)) +     geom_point(colour=colorScheme)
  })

}

1 个答案:

答案 0 :(得分:1)

colorScheme是一个函数,因此您应该在末尾添加()

 output$plot1 <- renderPlot({
            ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point(colour=colorScheme())
    })