选择一个或多个供体ID,在R中的shiny中绘制图形

ukqbszuj  于 2024-01-03  发布在  其他
关注(0)|答案(1)|浏览(94)

My Shiny应用程序允许用户通过使用选择一个或多个捐赠者ID:

selectizeInput("donor", label = "Enter Donor ID:", 
                                               choices = as.list(c(unique(df$ID))), 
                                               multiple = TRUE,
                                               options = list(),
                                               selected = NULL)

字符串
选择的ID将存储在input$donor变量中。如果没有选择donor ID(input$donor为null),则将颜色美学Map到x变量(input$x_var2),并使用color_code函数确定颜色值。如果用户没有在input$donor中输入任何内容,则默认首先显示此箱线图(如下所示):

df <- data.frame(
  ID = c("ID0001", "ID0002", "ID0003", "ID0004", "ID0005"),
  Gender = c("Female", "Male", "Female", "Female", "Female"),
  AgeGroup = c("30yr_39yr", "40yr_49yr", "40yr_49yr", "30yr_39yr", "30yr_39yr"),
  variable1 = c(10, 12, 8, 15, 11),
  variable2 = c(5, 3, 7, 9, 6),
  variable3 = c(20, 18, 22, 17, 19)
)

color_code <- function(x) {
  switch(x,
         Gender = c("#ff0000", "#4472c4"),
         AgeGroup = c("20yr_29yr" = "#c5e0b4", 
                      "30yr_39yr" = "#b4c7e7", 
                      "40yr_49yr" = "#f8cbad", 
                      "50yr_59yr" = "#c55a11", 
                      "60yr_69yr" = "#bf9000", 
                      "70yr_80yr" = "#7030a0"
         ),
         NULL
  )
}

ggplot(df, aes(x = !!sym(input$x_var2), y = !!sym(input$y_var2))) +
        theme_bw() +
        # coord_cartesian(xlim = ranges$x, ylim = ranges$y, expand = TRUE) +
        geom_boxplot(color = "black", fill = "white", width = 0.5, size = 1.0, outlier.shape = NA, coef = 1.5) +
        geom_jitter(aes(color = !!sym(input$x_var2)), width = 0.1, alpha = 0.5) +
        scale_color_manual(values = color_code(input$x_var2))


但是,当我添加更改箱线图以将颜色美学Map到选定点的条件时,它崩溃了。我想做的是,如果input$donor不为null,则箱线图将根据input$donor中的选定点更改颜色美学(即只有选定的供体显示红点,这就是为什么我创建一个新列来存储所选的ID。

ui.R
selectizeInput("donor", label = "Enter Donor ID:", 
                                               choices = as.list(c(unique(df$ID))), 
                                               multiple = TRUE,
                                               options = list(),
                                               selected = NULL)
server.R

    output$plot2 <- renderPlot({
    
df$selection <- ifelse(df$ID %in% input$donor, "selected", "unselected") 

p <- ggplot(df, aes(x = !!sym(input$x_var2), y = !!sym(input$y_var2))) +
        theme_bw() +
        # coord_cartesian(xlim = ranges$x, ylim = ranges$y, expand = TRUE) +
        geom_boxplot(color = "black", fill = "white", width = 0.5, size = 1.0, outlier.shape = NA, coef = 1.5) +
        # colored by selected IDs in inputbox
        if (!is.null(input$donor)) {
          geom_jitter(df, aes(color = selection), width = 0.1, alpha = 0.5) +
            scale_color_manual(values = c("unselected" = "black", "selected" = "red")) 
         # colored by gender
        } else {
          geom_jitter(df, aes(color = !!sym(input$x_var2)), width = 0.1, alpha = 0.5) +
            scale_color_manual(values = color_code(input$x_var2)) 
        }
       
      p

})
    
    output$brush_info2 <- renderTable({
      brushedPoints(df, input$plot2_brush)
    })
kninwzqo

kninwzqo1#

不是一个真实的闪亮的问题。正如我在回答你之前的一个问题时所建议的,为了更容易测试和调试,把你的绘图代码放在一个单独的函数中。
首先,使用data = df代替geom_point(df, ...),或者简单地删除它,因为df是全局数据。
其次,在if中,你不能再使用+将层添加到一起,而是将它们放在list中。

library(ggplot2)

input <- list(
  x_var2 = "Gender",
  y_var2 = "variable1",
  donor = NULL
)

df$selection <- ifelse(df$ID %in% input$donor, "selected", "unselected")

ggplot(df, aes(x = !!sym(input$x_var2), y = !!sym(input$y_var2))) +
  theme_bw() +
  geom_boxplot(color = "black", fill = "white", width = 0.5, size = 1.0, outlier.shape = NA, coef = 1.5) +
  if (!is.null(input$donor)) {
    list(
      geom_jitter(aes(color = selection), width = 0.1, alpha = 0.5),
      scale_color_manual(values = c("unselected" = "black", "selected" = "red"))  
    )
  } else {
    list(
      geom_jitter(aes(color = !!sym(input$x_var2)), width = 0.1, alpha = 0.5),
      scale_color_manual(values = color_code(input$x_var2))  
    )
  }

字符串


的数据

相关问题