简体   繁体   English

R使用不同数据框的闪亮嵌套输入函数

[英]R Shiny nested input functions using different dataframes

I have 3 measures in an nXm matrix representing n=500 and m=31(representing the same measure over 1 month including: ("Executive functioning", "working memory", "depression") for three different "trials"("Trial1", "Trial2", "Trial3"). I have 6 different matrices representing the combinations of these datasources (ie "Trial1_WM"). I also have a corresponding variable with the corresponding dates. 我在nXm矩阵中有3个量度,分别代表n = 500和m = 31(代表1个月内的相同量度,包括:(“执行功能”,“工作记忆”,“抑郁”),用于三个不同的“试验”(“ Trial1 “,” Trial2“,” Trial3“)。我有6个不同的矩阵来表示这些数据源的组合(即“ Trial1_WM”)。我还具有带有相应日期的相应变量。

I am attempting to build an R Shiny app where I can select subsets of these datasets to graph them in a histogram (ie WM across all trials and across a date range, WM for Trial 1, etc.). 我正在尝试构建一个R Shiny应用程序,在这里我可以选择这些数据集的子集以直方图形式对其进行绘制(例如,所有试验和日期范围内的WM,试验1的WM等)。 I have built the widgets and have built the app to graph all the data. 我建立了小部件,并建立了应用程序以图形化所有数据。 But I can't figure out how to use multiple widgets to segement the data as I want. 但是我不知道如何根据需要使用多个小部件对数据进行细分。 Here is some working code with all the widgets I want to build that currently only works for agregrate data (ie all WM) with a slider to bin data: 这是我要构建的所有小部件的一些工作代码,这些小部件当前仅适用于带有滑动条来合并数据的汇总数据(即所有WM):

library(shiny) 库(闪亮)

read in data 读入数据

Date <- seq(as.Date("2018-01-01"), as.Date("2018-01-31"), by="days")
Date <- as.matrix(t(Date))

N<- 500
M<-31


T1_EF <- matrix( rnorm(N*M,mean=23,sd=3), N, M)
T1_WM <- matrix( rnorm(N*M,mean=30,sd=4), N, M) 
T1_DP <- matrix( rnorm(N*M,mean=30,sd=3.5), N, M)

T2_EF <- matrix( rnorm(N*M,mean=30,sd=3.5), N, M)
T2_WM <- matrix( rnorm(N*M,mean=40,sd=4), N, M) 
T2_DP <- matrix( rnorm(N*M,mean=34,sd=4), N, M)

T3_EF <- matrix( rnorm(N*M,mean=35,sd=3), N, M)
T3_WM <- matrix( rnorm(N*M,mean=35,sd=3), N, M) 
T3_DP <- matrix( rnorm(N*M,mean=40,sd=3), N, M)


Trial1_EF<- as.matrix(round(T1_EF, digits = 6))
Trial2_EF<- as.matrix(round(T2_EF, digits = 6))
Trial3_EF<- as.matrix(round(T3_EF, digits = 6))

Trial1_WM <-as.matrix(round(T1_WM,digits = 6))
Trial2_WM <-as.matrix(round(T2_WM,digits = 6))
Trial3_WM <-as.matrix(round(T3_WM, digits = 6))

Trial1_DP <-as.matrix(round(T1_DP, digits = 6))
Trial2_DP <-as.matrix(round(T2_DP, digits = 6))
Trial3_DP <-as.matrix(round(T3_DP = 6))


# Define UI  ----
ui <- fluidPage(
  titlePanel(code(strong("Tools"), style = "color:black")),
  sidebarLayout(
    sidebarPanel(
      strong("Tools:"),
      selectInput("Test", 
                  label = "Choose a measure to display",
                  choices = c("Executive Functioning", 
                              "Working Memory",
                              "Depression"
                  ),
                  selected = "Executive Functioning"),

      selectInput("Study", 
                  label = "Choose a Study to display",
                  choices = c("Trial1", 
                              "Trial2",
                              "Trial3",
                              "All"
                  ),
                  selected = "All"),
      selectInput("Uptake", 
                  label = "Uptake",
                  choices = c("Prior Week", 
                              "Prior Month",
                              "Study to Date"),
                  selected = "Study to Date"),

      dateRangeInput("dates", label= "Date range"),
      sliderInput(inputId="slider1", label = "Bins",
                  min = 1, max = 300, value = 200),
      downloadButton("downloadData", "Download")),
    mainPanel(
      code(strong("Study Readout")),
      plotOutput("distPlot")
    ))
)

# Define server logic ----
server <- function(input, output) {
  output$distPlot <- renderPlot({
    slider1 <- seq(floor(min(x)), ceiling(max(x)), length.out = input$slider1 + 1)
    x    <- switch(input$Test, 
                   "Executive Functioning" = cbind(Trial1_EF,Trial2_EF,Trial3_EF),
                   "Working Memory" = cbind(Trial1_WM,Trial2_EF,Trial3_WM), 
                   "Depression" = cbind(Trial1_DP,Trial2_EF,Trial3_DP)
    color <- switch(input$Test, 
                    "Executive Functioning" = "darkgreen",
                    "Working Memory" = "darkorange",
                    "Depression" = "darkviolet")

    legend <- switch(input$Test, 
                     "Executive Functioning" = "Executive Functioning",
                     "Working Memory" = "Working Memory",
                     "Depression" = "Depression")

    hist(x, breaks = slider1, col=color, main=legend)

  })
}


# Run ----
shinyApp(ui = ui, server = server)

One option is to use reactive() function inside your server. 一种选择是在服务器内部使用react()函数。 It enables you to make some manipulation on the matrix x , which will be the matrix used in the renderPlot() . 它使您能够对矩阵x进行一些操作,该矩阵将是renderPlot()中使用的矩阵。 For instance, a simple if statement will create a different matrix if Study = 'All' or if Study = 'Trial1'. 例如,如果Study ='All'或Study ='Trial1',则简单的if语句将创建一个不同的矩阵。

server <- function(input, output) {    

  filterData <- reactive({    
     if(input$Study == 'All')
        x <- switch(input$Test, 
               "Executive Functioning" = cbind(Trial1_EF, Trial2_EF, Trial3_EF),
               "Working Memory"        = cbind(Trial1_WM, Trial2_EF, Trial3_WM), 
               "Depression"            = cbind(Trial1_DP, Trial2_EF, Trial3_DP))

    if(input$Study == 'Trial1')
        x <- switch(input$Test, 
               "Executive Functioning" = cbind(Trial1_EF),
               "Working Memory"        = cbind(Trial1_WM), 
               "Depression"            = cbind(Trial1_DP))

    return(x)
  })    

  output$distPlot <- renderPlot({     

    x <- filterData()        
    slider1 <- seq(floor(min(x)), ceiling(max(x)), length.out = input$slider1 + 1)

    [...]

    hist(x, breaks = slider1, col = color, main = legend)
  })
}

You can create as many reactive() as you want. 您可以根据需要创建任意数量的react()

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

相关问题 R Shiny:嵌套观察功能 - R Shiny: nested observe functions R Shiny-使用selectInput刷新动态生成的输入函数 - R Shiny - Flush dynamically generated input functions using selectInput 如何在R中不使用嵌套循环的情况下引用2个不同的数据帧 - How to reference 2 different dataframes without using nested loops in R R Shiny:在无功输入上执行一系列功能 - R Shiny: Perform a series of functions on reactive input R Shiny 中的反应式桑基图,以多个数据帧作为输入 - Reactive Sankey Diagram in R Shiny with multiple dataframes as input 如何根据用户输入打印不同的数据框? Rmarkdown/闪亮 - How to print different dataframes based of user input? Rmarkdown/Shiny 使用 2 个不同大小的数据帧的子集 R - Subset using 2 dataframes of different sizes R 将元数据数据框中定义的R函数应用于不同数据框的特定列 - Applying R functions as defined in metadata dataframe to specific columns of different dataframes 使用R将不同数据框中的重复行分开 - Seperate the duplicated rows in different dataframes using R 如何在不使用类似update_Input的函数的情况下从外部文件交互式更新R闪亮输入窗口小部件中的默认值(选择值)? - How to interactively update default (selected = ) values in R shiny input widgets from external file without using update_Input like functions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM