【问题标题】:Select a variable based on two inputs in Rshiny根据 R Shiny 中的两个输入选择一个变量
【发布时间】:2022-01-18 02:19:45
【问题描述】:

我有一个数据集,其中包含患者 ID、不同测试 (MMT) 和治疗的变量。

ID
MMT_II_week15_change
MMT_II_Week20_change
MMT_Tot_week15_change
MMT_Tot_Week20_change
Treatment

如您所见,我们针对两个不同的时间点(week15week20)进行了两种不同的测试(MMT_II_changeMMT_Tot_change)。

我想要的是用户能够首先选择测试,然后选择时间点。 实际上,他只会选择其中一个变量,但分两个不同的步骤。

类似:

**Select test:**
MMT_II
MMT_III

**Select timepoint:**
Week15
Week20

在此之后,选择的变量将是:
例如:MMT_II_Week20_change

我虽然为此使用正则表达式,但它似乎很复杂并且找不到这样做的方法。

任何帮助都非常感谢,因为我已经坚持了一段时间。

【问题讨论】:

    标签: r shiny selection


    【解决方案1】:

    这样的东西有用吗?

    VAR = paste0(test,"_",timepoint,"_change")
    
    ...
    
    # then later to use the variable...  
    .data[[VAR]]
    

    【讨论】:

      【解决方案2】:

      您可以在代码的 server 部分中包含对单个反应函数中发生的更改的检查。

      uptodateChoice <- reactive({
      paste0(input$firstcontrol, "_", input$secondcontrol, "_change") 
      })
      

      一旦两个控件状态中的任何一个发生变化,就会调用此函数。

      如果需要,您还可以在函数内添加任何 validate(need(...)) 检查,如果不满足某些条件,则只需 return()

      你可以调用uptodateChoice()来访问字符串值。

      【讨论】:

        【解决方案3】:

        我正在考虑将数据转为更长的格式,对其进行过滤,然后再次转为更宽的格式。这样我们就可以直接使用filter函数进行过滤了。

        library(tidyverse)
        library(shiny)
        
        # create some data
        df <- tibble(
          ID = 1:5, MMT_II_week15_change = sample(seq(0.01, 0.2, 0.01), 5), MMT_II_week20_change = sample(seq(0.01, 0.2, 0.01), 5),
          MMT_Tot_week15_change = sample(seq(0.01, 0.2, 0.01), 5), MMT_Tot_week20_change = sample(seq(0.01, 0.2, 0.01), 5)
        )
        # pivot wider capturing MMT_* for the first column and the number of week in the second.
        df_pivot <- pivot_longer(df, -ID, names_to = c("test", "week"), values_to = "change", names_pattern = "(MMT_.*)_week(\\d+)_change$")
        
        ## APP
        library(shiny)
        
        ui <- fluidPage(
          selectInput("test", "Select  Test", choices = unique(df_pivot$test)),
          selectInput("timepoint", "Select Timepoint", choices = NULL),
          tableOutput("table")
        )
        
        server <- function(input, output, session) {
          table <- reactiveVal(NULL)
        
          observeEvent(input$test, {
            choices <- filter(.data = df_pivot, test == input$test) %>%
              {
                unique(.$week)
              }
            updateSelectInput(inputId = "timepoint", choices = choices)
          })
        
          # this could also be a reactive.
          observe({
            table(filter(df_pivot, test == input$test, week == input$timepoint) %>%
              pivot_wider(names_from = "test", values_from = "change"))
          })
        
          output$table <- renderTable({
            table()
          })
        }
        
        shinyApp(ui, server)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-09-13
          • 1970-01-01
          • 2021-04-04
          • 2021-02-24
          • 1970-01-01
          • 2019-08-21
          • 2021-01-07
          相关资源
          最近更新 更多