简体   繁体   English

在闪亮的应用程序的selectinput小部件中包含选项NULL

[英]include option NULL in selectinput widget in shiny app

I'm running into some issues in my work. 我的工作遇到了一些问题。 To iullustrate the problem, I'm using the diamonds data as an example. 为了说明这个问题,我以Diamonds数据为例。

I made a function sca.plot. 我做了一个函数sca.plot。 If the argument color.by=NULL, then make a scatter plot and color all points red. 如果参数color.by = NULL,则进行散点图绘制并将所有点涂成红色。 If color.by is some factor variable, make scatter plot and color points by the variable. 如果color.by是某个因子变量,请通过该变量绘制散点图和色点。

My question is when I make interactive scatter plot in shiny. 我的问题是当我使交互式散点图变得闪亮时。 How can I include the NULL option in selectinput widget, so that I can choose whether or not those points are colored by some variable? 如何在selectinput小部件中包含NULL选项,以便可以选择这些点是否由某些变量着色?

If I include NULL in choices in selectinput, I got errors. 如果在selectinput的选择中包括NULL,则会出现错误。 Could not figure it out.... 无法解决...。

Thanks a lot in advance. 非常感谢。

Below is my code: 下面是我的代码:

library(ggplot2)

sca.plot <- function (color.by=NULL) {
    if (is.null(color.by)) {
        diamonds %>% ggplot(aes(x, y))+
            geom_point (color='red')
    } else {
        color.by = sym(color.by)
        diamonds %>% ggplot(aes_(~x, ~y, color=color.by))+
            geom_point ()
    }
}

sca.plot(color.by='clarity')

ui <- fluidPage(
    sidebarLayout(
        sidebarPanel(
            selectInput('colorby', label = h5(strong('Color By')),
                        choices = list('clarity', 'cut'),
                        selected = NULL)
        ),
        mainPanel(
            plotOutput('plot') 
            )
        )
    )


server <- function(input, output) {

    output$plot <- renderPlot ({
        sca.plot(color.by=input$colorby)
    })
}

runApp(shinyApp(ui, server))

Here is the solution for you: 这是为您提供的解决方案:

library(ggplot2)
library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput('colorby', label = h5(strong('Color By')),
                  choices = list('NULL','clarity', 'cut'),
                  selected = NULL)
    ),
    mainPanel(
      plotOutput('plot') 
    )
  )
)


server <- function(input, output) {

  output$plot <- renderPlot ({
    if(!input$colorby == 'NULL'){
    ggplot(diamonds, aes_string(x="x",y="y", color=input$colorby)) +
        geom_point()
      }else{
      ggplot(diamonds, aes(x=x,y=y)) +
        geom_point(color='red')
    }
  })
}
shinyApp(ui = ui, server = server)

You can use NULL as an argument in selectInput but as a string --> 'NULL' 您可以使用NULL作为参数selectInput但作为一个字符串- > 'NULL'

You actually do not need such a function as you wrote at the beginning of shiny app, you can easily use if...else... statement directly in renderPlot() to get the desired color of the geom_point() . 实际上,您不需要在闪亮应用程序开始时编写的函数,可以轻松地直接在renderPlot()使用if...else...语句来获取所需的geom_point()颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM