闪亮的 if else 语句

Shiny if else statement

我在 Shiny 中使用条件语句时遇到问题。我要用户 select 数量的变量。如果选择 1 个变量,则绘制 1 个变量的图表(ex 密度图),如果选择 2 个变量,则绘制 2 个变量的图表(ex 散点图)。我已经尝试了几种方法,但输出并不像我预期的那样。我如何在 Shiny 服务器中使用 if else 语句?谢谢

UI

   df <- mtcars
    ui <- fluidPage(
        h1("My first app",
           style = 'color: green;
           font-style: italic'),
        hr(),
        fluidRow(
            sidebarPanel(
                radioButtons(inputId = "number",
                             label = "Select number of variable",
                             choices = c("1 variable" = 1,
                                         "2 variable" = 2)),
                selectInput(inputId = "x",
                            label = "Variable 1",
                            choices = names(df)),
                conditionalPanel(
                    condition = "input.number == 2",
                    selectInput(inputId = "y",
                                label = "Variable 2",
                                choices = names(df))
                    ) 
                ),
            column(8, plotOutput("plot"))
        ),
        hr(),
        plotOutput("plot") )

服务器

server <- function(input, output, session){
    observeEvent(input$x, 
                 {updateSelectInput(session,
                                    inputId = "y",
                                    label = "Variable 2",
                                    choices = names(df)[names(df) != input$x])
                 })
    
    data <- reactive({
        if(input$number == 1){
            data <- df %>% 
                select(input$x)
        } else {
            data <- df %>% 
                select(input$x, input$y)
        }
    })
    
    output$plot <- renderPlot({
        if(input$number == 1){
            ggplot(data = data(),
                   x = get(input$x))+
                geom_density()
        } else {
            ggplot(data = data,
                   x = get(input$x),
                   y = get(input$y)) +
                geom_point()
        }
    })
}

shinyApp(ui = ui, server = server)

你可以试试下面的代码-

  • plotOutput("plot") 被提及两次,将其删除以仅包含一次。
  • 我们在 reactive 中创建数据集时不需要检查条件,在绘图代码本身中处理它。
  • 使用 .data 引用 ggplot 代码中的列名。
library(shiny)
library(ggplot2)

df <- mtcars
ui <- fluidPage(
  h1("My first app",
     style = 'color: green;
           font-style: italic'),
  hr(),
  fluidRow(
    sidebarPanel(
      radioButtons(inputId = "number",
                   label = "Select number of variable",
                   choices = c("1 variable" = 1,
                               "2 variable" = 2)),
      selectInput(inputId = "x",
                  label = "Variable 1",
                  choices = names(df)),
      conditionalPanel(
        condition = "input.number == 2",
        selectInput(inputId = "y",
                    label = "Variable 2",
                    choices = names(df))
      ) 
    ),
    column(8, plotOutput("plot"))
  )
)


server <- function(input, output, session){
  
  data <- reactive({
    df
  })

 observeEvent(input$x, 
               {updateSelectInput(session,
                                  inputId = "y",
                                  label = "Variable 2",
                                  choices = names(df)[names(df) != input$x])
               })
  
  output$plot <- renderPlot({
    
    if(input$number == 1){
      plot <- ggplot(data = data(), aes(x = .data[[input$x]])) + geom_density()
    } else {
      plot <- ggplot(data = data(),
             aes(x = .data[[input$x]], y = .data[[input$y]])) +
        geom_point()
    }
    plot
  })
}

shinyApp(ui = ui, server = server)

你可以使用 aes_string.
另外很重要的一点是 in UI:

df <- mtcars
library(ggplot2)
library(dplyr)

ui <- fluidPage(
  h1("My first app",
     style = 'color: green;
           font-style: italic'),
  hr(),
  fluidRow(
    sidebarPanel(
      radioButtons(inputId = "number",
                   label = "Select number of variable",
                   choices = c("1 variable" = 1,
                               "2 variable" = 2)),
      selectInput(inputId = "x",
                  label = "Variable 1",
                  choices = names(df)),
      conditionalPanel(
        condition = "input.number == 2",
        selectInput(inputId = "y",
                    label = "Variable 2",
                    choices = names(df))
      ) 
    ),
    column(8, plotOutput("plot"))
  ),
  hr() 
  # Never use output twice : the UI won't work!
  #plotOutput("plot") 
  )

server <- function(input, output, session){
  observeEvent(input$x, 
               {updateSelectInput(session,
                                  inputId = "y",
                                  label = "Variable 2",
                                  choices = names(df)[names(df) != input$x])
               })
  
  data <- reactive({
    if(input$number == 1){
      data <- df %>% 
        select(input$x)
    } else {
      data <- df %>% 
        select(input$x, input$y)
    }
  })
  
  output$plot <- renderPlot({
    cat(input$x)
    if(input$number == 1){
      ggplot(data = data())+
        geom_density(aes_string(x=input$x))
    } else {
      ggplot(data = data()) +
        geom_point(aes_string(x=input$x,y=input$y))
    }
  })
}

shinyApp(ui = ui, server = server)