我有一个基本的RShiny应用程序,它有一个被动复选框,根据复选框中 Select 的数据(DF列)绘制时间序列数据.我当前的代码生成了一个带有如下复选框输入的UI:

    # Load R packages
library(shiny)
library(shinyBS)

##example df in similar format to the data I'm working with
Both <- data.frame(
  Year  = c("1990", "1991", "1992", "1993"),
  SST_anomaly_GOM = c("-1.1", "0.23", "0.87", "-0.09"),
  SST_anomaly_GB = c("-1.1", "0.23", "0.87", "-0.09"),
  SST_anomaly_MAB = c("-1.1", "0.23", "0.87", "-0.09"),
  BT_anomaly_GOM = c("-2.5", "0.55", "1.20", "-0.19"),
  BT_anomaly_GB = c("-1.1", "0.05", "1.24", "-0.29"),
  BT_anomaly_MAB = c("-1.1", "-1.08", "0.67", "-2.40")
)

# Define UI
ui <- fluidPage(
  # useShinyBS
    "Visualizing Indicators", #app title
    tabPanel("",      # tab title
             sidebarPanel(width=6,
                          
                          checkboxGroupInput("variable", label = "Checkbox", choiceNames  = gsub("_", " ", colnames(Both[2:7])), 
                                                choiceValues = colnames(Both[2:7]), 
                                                ),
             ), # sidebarPanel
    ), #tabPanel
) # fluidPage

#Define Server:
server<- function (input,output){
   output$rendered <-   renderUI({
    })
}
# Create Shiny object
shinyApp(ui = ui, server = server)

这将生成如下所示的界面:

enter image description here

这很好,但有一点重复,随着我最终想要将更多的时间序列变量包括到这个列表中,这对用户来说可能会变得很麻烦,并且会占用UI上的大量空间来以这种方式列出所有内容.

我的问题是,我如何调整我的代码,使其生成一个界面,其中列出唯一的变量,然后 for each 感兴趣的子区域勾选框?(GOM、BG、MAB等) 我脑海中的一个例子是一个界面,它看起来更像这样:

![enter image description here

这个是可能的吗?是否可以使用我当前具有的格式的df(例如我的示例df称为"Both").

谢谢!

推荐答案

为了为您的解决方案创建答案,我使用DT包实现了一个复选框组输入.解决方案分为两个部分:1.Helper函数.2.App.

Example Image

enter image description here

Helper Functions

第一个助手函数创建一个包含checkbox个输入的数据表,每个输入都有一个唯一的id,它是行名和列名的组合.

第二个帮助器函数判断构造的表中每个复选框的"选中"状态,返回复选框表中每个单元格的值为TRUE/FALSE的矩阵.

App

应用程序的代码非常简单明了.

首先,我们使用第一个helper函数创建一个示例表.

然后,我们使用DT呈现表格,确保在表格上禁用escape(以便可以呈现复选框)、sortingpagingselection.最重要的是,我们发送preDrawCallbackdrawCallback JS函数以确保复选框注册到shiny.

最后,每当用户与表交互时,我们都会调用第二个helper函数来判断复选框状态.你可以用这些信息做任何你想做的事情.

Code

# Checkbox Table Demo

library(shiny)
library(DT)


#### Helper Functions ####
#' Construct a checkbox table for an app.
construct_checkbox_table <- function(rows,
                                     cols,
                                     rownames,
                                     colnames) {
  checkbox_table <- matrix(
    character(),
    nrow = rows,
    ncol = cols,
    dimnames = list(rownames, colnames)
  )
  
  for (i in seq_len(rows)) {
    for (j in seq_len(cols)) {
      checkbox_table[i, j] <-
        sprintf(
          '<input id="%s,%s" type="checkbox" class="shiny-bound-input" />',
          rownames[[i]],
          colnames[[j]]
        )
    }
  }
  
  checkbox_table
}

#' Get the status of checkboxes in a checkbox table.
evaluate_checkbox_table_status <- function(input, input_table) {
  table_status <-
    matrix(
      logical(),
      nrow = nrow(input_table),
      ncol = ncol(input_table),
      dimnames = list(rownames(input_table), colnames(input_table))
    )
  
  table_rownames <- rownames(input_table)
  table_colnames <- colnames(input_table)
  
  for (i in seq_len(nrow(input_table))) {
    for (j in seq_len(ncol(input_table))) {
      table_status[i, j] <-
        input[[sprintf("%s,%s", table_rownames[[i]], table_colnames[[j]])]]
    }
  }
  
  table_status
}
#### End Helper Functions ####


#### App ####
# Create an example checkbox input table to use for the app
example_checkbox_table <-
  construct_checkbox_table(
    2,
    4,
    rownames = c("Annual Bottom Temp Absolute", "Bottom Temp Anomoly"),
    colnames = c("GOM", "GB", "MAB", "SS")
  )

ui <- fluidPage(DT::DTOutput("selection_table"),
                verbatimTextOutput("table_selections"),)

server <- function(input, output, session) {
  output$selection_table <- DT::renderDT({
    DT::datatable(
      example_checkbox_table,
      escape = FALSE,
      selection = "none",
      options = list(
        dom = "t",
        ordering = FALSE,
        paging = FALSE,
        preDrawCallback = JS(
          'function() { Shiny.unbindAll(this.api().table().node()); }'
        ),
        drawCallback = JS(
          'function() { Shiny.bindAll(this.api().table().node()); } '
        )
      )
    )
  }, server = FALSE)
  
  observeEvent(input$selection_table_cell_clicked, {
    output$table_selections <- renderPrint({
      evaluate_checkbox_table_status(input, example_checkbox_table)
    })
  })
}
#### End App ####

shinyApp(ui, server)

R相关问答推荐

如何计算具有NA的行的更改百分比

Select R中列未排序的收件箱中的最后一个按顺序编号的列

使用gsim删除特殊词

如何使用Cicerone指南了解R Shiny中传单 map 的元素?

强制相关图以显示相关矩阵图中的尾随零

R的GG平行坐标图中的排序变量

基于shiny 应用程序中的日期范围子集xts索引

如何将在HW上运行的R中的消息(错误、警告等)作为批处理任务输出

在不安装软件包的情况下测试更新

r替换lme S4对象的字符串的一部分

R—将各种CSV数字列转换为日期

使用for循环和粘贴创建多个变量

将二进制数据库转换为频率表

有没有一种方法可以同时对rhandsontable进行排序和从rhandsontable中删除?

为什么在BASE R中绘制线条时会看到线上的点?

将向量元素重新排序为R中的第二个

在R中创建连续的期间

自定义交互作用图的标签

当由base::限定时,`[.factor`引发NextMethod错误

注释不会绘制在所有ggplot2面上