【问题标题】:Not quite getting the expected results when using filter使用过滤器时没有得到预期的结果
【发布时间】:2019-10-11 20:32:30
【问题描述】:

对于 R 或任何编程来说还是很新的东西,向部门展示一些健康结果数据,我想我会尝试写一些东西来让我选择要显示的数据子集。完成了一些自主阅读并尝试了以下内容(请参见下文)。
我遇到的问题是,当我选择所有 4 种“手术方法”时,得到的结果不是我所期望的。例如,2019 年 7 月的平均 LOS 应该低于我在此处看到的。

正如我所说,对此非常陌生,请假设我在回答这个问题时一无所知。

# User Interface

ui <-basicPage(
  sliderInput("year", "Select Year:", animate = T,
              min = 2016, max = 2019, value = 2016, sep = ""),

  checkboxGroupInput("approach", "Surgical Approach", c("Laparoscopic", "Lap-assisted", "Converted to open", "Open"),
                     selected = c("Laparoscopic", "Lap-assisted", "Converted to open", "Open")),

plotOutput(outputId = "LOS_plot"),

  plotOutput(outputId = "All3_plot")
)

# Server

server <- function(input, output){

  output$LOS_plot <- renderPlot({

    ERAS %>% filter(Sx_Approach == input$approach) %>% 
      arrange(year_m) %>% 
      mutate(DoSdate = ymd(year_m), yearDoS = year(DoSdate), monthDoS = month(DoSdate)) %>% 
      filter(yearDoS == input$year) %>%
      group_by(year_m) %>% mutate(mean_LOS = mean(Postop_LOS)) %>%
      ggplot(aes(x = monthDoS, y = mean_LOS)) + geom_line(colour = "black", size = 1.5) +
      scale_x_continuous(limits = c(1, 12), breaks = c(1:12)) +
      scale_y_continuous(limits = c(0, 30), minor_breaks = 1) +
      theme(aspect.ratio = 0.4) + xlab("Month") + ylab("Days") + ggtitle("Mean Length of Stay over Time")

  })
  output$All3_plot <- renderPlot({

    ERAS %>% filter(Sx_Approach == input$approach) %>%
      arrange(year_m) %>% 
      mutate(DoSdate = ymd(year_m), yearDoS = year(DoSdate), monthDoS = month(DoSdate)) %>% 
      filter(yearDoS == input$year) %>%
      group_by(year_m) %>% mutate(mean_All3 = mean(All_3 == T, na.rm = T)) %>%
      ggplot(aes(x = monthDoS, y = mean_All3)) + geom_line(colour = "#5391c6", size = 1.5) + scale_x_continuous(limits = c(1, 12), breaks = c(1:12)) +
      scale_y_continuous(labels = scales::percent, limits = c(0, 1)) +
      theme(aspect.ratio = 0.4) + xlab("Month") + ylab("Percentage") + ggtitle("Patients Achieving All Three ERAS Goals")

  })
}

【问题讨论】:

    标签: r shiny shinyapps


    【解决方案1】:

    与其使用“过滤器”,不如尝试使用“子集”来选择带有滑块输入和复选框组输入的数据框部分。在不知道您使用的数据结构的情况下,它看起来像这样:

    ERAS <-  subset(Sx_Approach, (year==input$year & approach==input$approach))
    

    【讨论】:

    • 谢谢。自发布问题以来,我一直在玩代码,发现使用filter(Sx_Approach %in% input$approach) 似乎也有效,而不是filter(Sx_Approach == input$approach)。不过我会试试你的答案。非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 2021-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多