如何使闪亮的情节React复选框数据选择

yshpjwxd  于 2023-09-27  发布在  React
关注(0)|答案(1)|浏览(147)

这是我第一个闪亮的代码。我已经把它简化成一个更像reprex的例子,并带有测试数据。这并不简单,因为它可以,但我试图保持一些结构的程序。我正在尝试使绘图响应文件选择复选框。例如,如果未选中Trial_4,则其数据将从所有三个图中消失,重新选择它将重新出现。

  1. library(shiny) # Server/App
  2. library(shinyWidgets) # Custom controls
  3. library(tidyverse) # For ggplot and dataframe manipulations
  4. # Function to generate checkbox group UI
  5. generateCheckboxGroupUI <- function(id, choices, names, selected, label) {
  6. checkbox_group <-
  7. checkboxGroupButtons(
  8. inputId = id,
  9. label = label,
  10. choiceValues = choices,
  11. choiceNames = names,
  12. selected = selected,
  13. status = "primary",
  14. direction = "vertical",
  15. checkIcon = list(
  16. yes = icon("ok",
  17. lib = "glyphicon"),
  18. no = icon("remove",
  19. lib = "glyphicon")),
  20. size = 'sm'
  21. )
  22. }
  23. # Plot function
  24. # Data frames must contain standard variables trial, time, and 3 columns of
  25. # data, pass column to plot in index = c(1,2,3)
  26. CreatePlot <- function(df, index) {
  27. ylab <- names(df)[index + 2]
  28. df <- df %>% select(c(1, 2, data = index + 2))
  29. plot <- ggplot(df, aes(x = time, y = data, col = trial)) +
  30. geom_line(linewidth = 1) +
  31. labs(x = "Time (s)", y = ylab) +
  32. theme_minimal()
  33. }
  34. # ---- User Interface ----
  35. ui <- fluidPage(
  36. sidebarLayout(
  37. # Nothing in sidebar for this example
  38. sidebarPanel(),
  39. # Main panel displays controls and plots
  40. mainPanel(
  41. # 1. Title
  42. fluidRow(
  43. column(12, align = 'center', h3("Reactive Plots"))
  44. ),
  45. # 2. File controls
  46. fluidRow(
  47. # File labels
  48. column(4),
  49. column(3, style = "display: flex;text-align: left; align-items: flex-start;",
  50. wellPanel(uiOutput("file_names")), style = "text-align: left;"),
  51. #column(1),
  52. # File selection check boxes
  53. column(1, style = "display: flex; justify-content: center; align-items: flex-start;",
  54. wellPanel(uiOutput("UseFile"))),
  55. column(4)
  56. ),
  57. ), # mainPanel
  58. ), # sidebarLayout
  59. # New section below sidebar layout to use full width for plots
  60. # 3. Left side plot windows
  61. fluidRow(
  62. column(4, plotOutput("left_plot")),
  63. column(4, plotOutput("middle_plot")),
  64. column(4, plotOutput("right_plot"))
  65. )
  66. ) # fluidPage
  67. #### ---- Server ---- ####
  68. server <- function(input, output) {
  69. #############
  70. # Test Data: 4 files with time, X, Y, Z Data of equal lengths
  71. # Data inside server to replicate actual program
  72. # In actual program file chooser loads data files
  73. file_name_labels <- c("File_1", "File_2", "File_3", "File_4")
  74. num_files <- 4
  75. t <- seq(0,10,0.1)
  76. shift <- 0.25
  77. F1 <- tibble(
  78. trial = as.factor(1),
  79. time = t,
  80. X = sin(t),
  81. Y = cos(t),
  82. Z = sin(t) + cos(t)
  83. )
  84. F2 <- tibble(
  85. trial = as.factor(2),
  86. time = t,
  87. X = sin(t + shift),
  88. Y = cos(t + shift),
  89. Z = sin(t + shift) + cos(t + shift)
  90. )
  91. F3 <- tibble(
  92. trial = as.factor(3),
  93. time = t,
  94. X = sin(t - shift),
  95. Y = cos(t - shift),
  96. Z = sin(t - shift) + cos(t - shift)
  97. )
  98. F4 <- tibble(
  99. trial = as.factor(4),
  100. time = t,
  101. X = sin(-t),
  102. Y = cos(-t),
  103. Z = sin(-t) + cos(-t)
  104. )
  105. # Now bind together
  106. plot_data <- bind_rows(F1, F2, F3, F4)
  107. ########
  108. # Define reactive values for checkbox states
  109. # Being reactive when these values change, checkboxes are updated
  110. checkbox_states <- reactiveValues(
  111. UseFile = NULL,
  112. )
  113. # File names
  114. # Create file name labels in UI
  115. output$file_names <- renderUI({
  116. file_names <- lapply(file_name_labels, function(name) {
  117. # Adjust h-level here to get size right
  118. h4(name)
  119. })
  120. tagList(
  121. # Add margin at the top to align with checkboxes and radio buttons
  122. tags$div(style = "margin-top: 12px;"),
  123. fluidRow(file_names)
  124. )
  125. })
  126. # Initialize checkbox states, use all initially
  127. checkbox_states$UseFile <- rep(TRUE, num_files)
  128. # choices are given dummy values: c('A', 'B', 'C', ...)
  129. checkbox_choices <- LETTERS[1:num_files]
  130. # names are set to blank in a vector of same size as choices
  131. checkbox_names <- rep("", num_files)
  132. # Plot File options
  133. output$UseFile <- renderUI({
  134. checkbox_group <- generateCheckboxGroupUI(
  135. id = "UseFile",
  136. choices = checkbox_choices,
  137. names = checkbox_names,
  138. selected = LETTERS[which(checkbox_states$UseFile)],
  139. label = "Files")
  140. checkbox_group
  141. })
  142. # Create the three plots
  143. output$left_plot <- renderPlot({
  144. plot <- CreatePlot(df = plot_data, index = 1)
  145. plot
  146. })
  147. output$middle_plot <- renderPlot({
  148. plot <- CreatePlot(df = plot_data, index = 2)
  149. plot
  150. })
  151. output$right_plot <- renderPlot({
  152. plot <- CreatePlot(df = plot_data, index = 3)
  153. plot
  154. })
  155. } # Server
  156. shinyApp(ui = ui, server = server)
i86rm4rw

i86rm4rw1#

也许你在找这个

  1. library(shiny) # Server/App
  2. library(shinyWidgets) # Custom controls
  3. library(tidyverse) # For ggplot and dataframe manipulations
  4. # Function to generate checkbox group UI
  5. generateCheckboxGroupUI <- function(id, choices, names, selected, label) {
  6. checkbox_group <-
  7. checkboxGroupButtons(
  8. inputId = id,
  9. label = label,
  10. choiceValues = choices,
  11. choiceNames = names,
  12. selected = selected,
  13. status = "primary",
  14. direction = "vertical",
  15. checkIcon = list(
  16. yes = icon("ok",
  17. lib = "glyphicon"),
  18. no = icon("remove",
  19. lib = "glyphicon")),
  20. size = 'sm'
  21. )
  22. }
  23. # Plot function
  24. # Data frames must contain standard variables trial, time, and 3 columns of
  25. # data, pass column to plot in index = c(1,2,3)
  26. CreatePlot <- function(df, index) {
  27. ylab <- names(df)[index + 2]
  28. df <- df %>% select(c(1, 2, data = index + 2))
  29. plot <- ggplot(df, aes(x = time, y = data, col = trial)) +
  30. geom_line(linewidth = 1) +
  31. labs(x = "Time (s)", y = ylab) +
  32. theme_minimal()
  33. }
  34. # ---- User Interface ----
  35. ui <- fluidPage(
  36. sidebarLayout(
  37. # Nothing in sidebar for this example
  38. sidebarPanel(),
  39. # Main panel displays controls and plots
  40. mainPanel(
  41. # 1. Title
  42. fluidRow(
  43. column(12, align = 'center', h3("Reactive Plots"))
  44. ),
  45. # 2. File controls
  46. fluidRow(
  47. # File labels
  48. column(4),
  49. column(3, style = "display: flex;text-align: left; align-items: flex-start;",
  50. wellPanel(uiOutput("file_names")), style = "text-align: left;"),
  51. #column(1),
  52. # File selection check boxes
  53. column(1, style = "display: flex; justify-content: center; align-items: flex-start;",
  54. wellPanel(uiOutput("UseFile"))),
  55. column(4)
  56. ),
  57. ), # mainPanel
  58. ), # sidebarLayout
  59. # New section below sidebar layout to use full width for plots
  60. # 3. Left side plot windows
  61. fluidRow(
  62. column(4, plotOutput("left_plot")),
  63. column(4, plotOutput("middle_plot")),
  64. column(4, plotOutput("right_plot"))
  65. )
  66. ) # fluidPage
  67. #### ---- Server ---- ####
  68. server <- function(input, output) {
  69. #############
  70. # Test Data: 4 files with time, X, Y, Z Data of equal lengths
  71. # Data inside server to replicate actual program
  72. # In actual program file chooser loads data files
  73. file_name_labels <- c("File_1", "File_2", "File_3", "File_4")
  74. num_files <- 4
  75. t <- seq(0,10,0.1)
  76. shift <- 0.25
  77. F1 <- tibble(
  78. trial = as.factor(1),
  79. time = t,
  80. X = sin(t),
  81. Y = cos(t),
  82. Z = sin(t) + cos(t)
  83. )
  84. F2 <- tibble(
  85. trial = as.factor(2),
  86. time = t,
  87. X = sin(t + shift),
  88. Y = cos(t + shift),
  89. Z = sin(t + shift) + cos(t + shift)
  90. )
  91. F3 <- tibble(
  92. trial = as.factor(3),
  93. time = t,
  94. X = sin(t - shift),
  95. Y = cos(t - shift),
  96. Z = sin(t - shift) + cos(t - shift)
  97. )
  98. F4 <- tibble(
  99. trial = as.factor(4),
  100. time = t,
  101. X = sin(-t),
  102. Y = cos(-t),
  103. Z = sin(-t) + cos(-t)
  104. )
  105. # Now bind together
  106. plot_data <- bind_rows(F1, F2, F3, F4)
  107. ########
  108. # Define reactive values for checkbox states
  109. # Being reactive when these values change, checkboxes are updated
  110. checkbox_states <- reactiveValues(
  111. UseFile = NULL,
  112. )
  113. # File names
  114. # Create file name labels in UI
  115. output$file_names <- renderUI({
  116. file_names <- lapply(file_name_labels, function(name) {
  117. # Adjust h-level here to get size right
  118. h4(name)
  119. })
  120. tagList(
  121. # Add margin at the top to align with checkboxes and radio buttons
  122. tags$div(style = "margin-top: 12px;"),
  123. fluidRow(file_names)
  124. )
  125. })
  126. # Initialize checkbox states, use all initially
  127. checkbox_states$UseFile <- rep(TRUE, num_files)
  128. # choices are given dummy values: c('A', 'B', 'C', ...)
  129. checkbox_choices <- c(1:4) # LETTERS[1:num_files]
  130. # names are set to blank in a vector of same size as choices
  131. checkbox_names <- rep("", num_files)
  132. # Plot File options
  133. output$UseFile <- renderUI({
  134. checkbox_group <- generateCheckboxGroupUI(
  135. id = "UseFile",
  136. choices = checkbox_choices,
  137. names = checkbox_names,
  138. selected = checkbox_choices[which(checkbox_states$UseFile)],
  139. label = "Files")
  140. checkbox_group
  141. })
  142. plot_df <- reactive({
  143. if (is.null(input$UseFile)) {
  144. df <- NULL
  145. }else {
  146. df <- plot_data %>% filter(trial %in% input$UseFile)
  147. }
  148. df
  149. })
  150. # Create the three plots
  151. output$left_plot <- renderPlot({
  152. if (is.null(input$UseFile)) return(NULL)
  153. plot <- CreatePlot(df = plot_df(), index = 1)
  154. plot
  155. })
  156. output$middle_plot <- renderPlot({
  157. if (is.null(input$UseFile)) return(NULL)
  158. plot <- CreatePlot(df = plot_df(), index = 2)
  159. plot
  160. })
  161. output$right_plot <- renderPlot({
  162. if (is.null(input$UseFile)) return(NULL)
  163. plot <- CreatePlot(df = plot_df(), index = 3)
  164. plot
  165. })
  166. } # Server
  167. shinyApp(ui = ui, server = server)
展开查看全部

相关问题