【问题标题】:A way for users to store selections as inputs for later use一种用户将选择存储为输入以供以后使用的方法
【发布时间】:2020-04-20 06:56:06
【问题描述】:

我正在尝试创建一种用户可以将他们的选择存储为输入的方式。这是我想要完成的示例:

1) 运行应用程序,它会立即生成一个包含三行的表。您可以使用 Sample Label 命名它,例如“Sample1”

2) 点击Save Sample 按钮,然后在我创建的面板中创建一个复选框作为输入,点击该复选框后,将自动将过滤器调整为点击Save Sample 按钮时的样子。

3) 然后将 disp 设置为 160,将 hp 设置为 110,这将返回两行。重复相同的步骤。您将其命名为“Sample2”,然后点击“Save Sample”。现在有两个复选框:Sample1 和 Sample2。您现在可以切换它们,以便用户可以选择他们想要的任何设置。理论上,您可以根据需要多次执行此操作。

library(shiny)
library(shinyWidgets)

ui <- fluidPage(

  selectInput("disp", "Disp", choices = unique(sort(mtcars$disp)), selected = 275.8),
  selectInput("hp", "hp", choices = unique(sort(mtcars$hp)), selected = 180),
  div(style="display:inline-block", textInput(('sample_name'), label = 'Sample Name',width = 200)),
  div(style="display:inline-block", actionButton(('select_sample'),icon = icon('save'), label = 'Save Sample')),
  panel(h2("User Created Inputs go here")),
  DT::dataTableOutput("cardata")

)

server <- function(input,output,session) {

  compileData <- reactive({

    res <- mtcars %>% filter(hp == input$hp & disp == input$disp)

  })

  output$cardata <- DT::renderDataTable({

    compileData()

  })

}

shinyApp(ui,server)

【问题讨论】:

    标签: r shiny shiny-server shiny-reactivity shinyapps


    【解决方案1】:

    也许这对你有帮助。

    您可以将过滤器设置保存在 reactiveValues 数据框中。创建过滤器后,复选框组将自动更新为新名称。

    表格数据将根据这些选中的复选框进行过滤。对于此示例,如果未选择任何框,则显示的数据基于两个输入选择。但是,如果选中任何复选框,则数据表将包含根据这些选择过滤的数据。

    让我知道这是否接近您的想法。

    library(shiny)
    library(shinyWidgets)
    
    ui <- fluidPage(
    
      selectInput("disp", "Disp", choices = unique(sort(mtcars$disp)), selected = 275.8),
      selectInput("hp", "hp", choices = unique(sort(mtcars$hp)), selected = 180),
      div(style="display:inline-block", textInput(('sample_name'), label = 'Sample Name',width = 200)),
      div(style="display:inline-block", actionButton(('select_sample'),icon = icon('save'), label = 'Save Sample')),
      panel(h2("User Created Inputs go here"),
            uiOutput("checkboxes")
            ),
      DT::dataTableOutput("cardata")
    )
    
    server <- function(input,output,session) {
    
      rv <- reactiveValues(filters = data.frame(
        id = character(),
        disp = double(),
        hp = double()
      ))
    
      compileData <- reactive({
        if (is.null(input$checkboxes)) {
          mtcars %>% filter(hp == input$hp & disp == input$disp)
        } else {
          merge(mtcars, rv$filters[rv$filters$id %in% input$checkboxes, ], by = c("disp", "hp"))
        }
      })
    
      output$cardata <- DT::renderDataTable({
        compileData()
      })
    
      observeEvent(input$select_sample, ignoreInit = FALSE, {
        rv$filters <- rbind(rv$filters, data.frame(
          id = input$sample_name,
          disp = input$disp,
          hp = input$hp
          )
        )
      })
    
      output$checkboxes <- renderUI({
        checkboxGroupInput("checkboxes", "Filters:", choices = rv$filters$id, selected = NULL)
      })
    }
    
    shinyApp(ui,server)
    

    【讨论】:

    • 是的!这看起来很棒!谢谢!还有一个问题:有没有一种方法可以保存它们以备将来使用?
    • 是的,你可以。将看看this 关于闪亮应用程序中的本地和远程数据存储。另外,如果您觉得这有帮助,请随时点赞或接受答案。
    猜你喜欢
    • 2020-04-07
    • 1970-01-01
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    • 1970-01-01
    • 2014-05-01
    相关资源
    最近更新 更多