需要有关具有多种交互的闪亮动态 UI 算法的帮助

2024-01-16

我的闪亮算法(app.R 代码在底部):

  • 要求用户上传文件
  • 提供带有选项“无”、“国家”、“州”、“城市”的下拉字段
  • 选择“无”时,仅应出现文本输出
  • 选择“国家/地区”时,仅应显示国家/地区过滤器
  • 选择“州”时,应同时显示国家/地区和州过滤器
  • 选择“城市”时,国家和城市过滤器都应该出现,并且之前选择的任何现有州过滤器都应该消失

我在代码中做了什么: 使用 IF 条件表示“无”和“国家/地区”;使用“州”和“城市”的开关。

我的问题:一切都按预期工作,除了:如果我们在选择“无”后选择“州”,我会看到文本输出和州过滤器,而不是国家和州过滤器。发生这种情况是因为“None”或“Country”中的 IF 语句与 switch() 不同,仅打印 UI,并且如果进行其他选择则不会清除 UI

我的约束:

  • 国家过滤器输入需要在代码中重复(“State” 应该同时提供国家和州过滤器,与“城市”类似)但是 我们不能在闪亮的代码中重复具有相同 ID 的输入

  • 我无法使用不同的国家/地区过滤器 ID 来提供相同的输入 因为我需要读取国家过滤器中的输入值 多个地方,我需要复制所有这些 会让一切变得复杂

我需要帮助的地方:我唯一的问题是,如果用户在“无”之后选择(“州”/“城市”)(绕过“国家”),我需要同时显示“国家”和(“州”/“城市”)过滤器! )。

确实需要修复这个东西。任何帮助,将不胜感激

Thanks!

虚拟数据:https://docs.google.com/spreadsheets/d/1E9dbtOMm1-7ZjIHu3Ra_NyFBHrCQdITG2_xuMIMKDOs/edit?usp=sharing https://docs.google.com/spreadsheets/d/1E9dbtOMm1-7ZjIHu3Ra_NyFBHrCQdITG2_xuMIMKDOs/edit?usp=sharing

应用程序R代码

library(shiny)

ui <- fluidPage(

  fileInput("file_attr", "Door attributes:"),
  selectInput("select", label = "Region Drop down", choices = list("None", "Country", "State","City"), selected = "None"),
  uiOutput("NoneCountryFilter"),
  uiOutput("StateCityFilter")
)

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

  #Reading input
  data_attr <- reactive({
    file1 <- input$file_attr
    if(is.null(file1)){return()} 
    read.table(file=file1$datapath, sep=",", header = TRUE, stringsAsFactors = FALSE)
  })

  #Filter interactivity

  #Reading Lists
  countries <- reactive({
    if(is.null(data_attr()$Country)){return()}
    data_attr()$Country
  })

  states <- reactive({
    if(is.null(data_attr()$State)){return()}
    data_attr()$State[data_attr()$Country %in% input$show_vars]
  })

  cities <- reactive({
    if(is.null(data_attr()$City)){return()}
    data_attr()$City[data_attr()$Country %in% input$show_vars]
  })


  #Filters based on Region Drop down

  observeEvent(input$file_attr,{ 
    observe({

      if ("None" %in% input$select){
        output$NoneCountryFilter <- renderUI({
          if(is.null(data_attr()$Country)){return()}
          h4("No region Selected")              
        })        
      }

      if ("Country" %in% input$select){
        output$NoneCountryFilter <- renderUI({
          if(is.null(data_attr()$Country)){return()}
          selectizeInput('show_vars', 'Country Filter', choices = c("Select All","None", unique(countries())), multiple = TRUE)
        })    
      }        
    })

    output$StateCityFilter <- renderUI({

      switch(input$select,                
             "State" = (
               selectizeInput('show_vars_state', 'State Filter', choices = c("Select All","None", unique(states())), multiple = TRUE)
             ),
             "City" = (
               selectizeInput('show_vars_city', 'City Filter', choices = c("Select All","None", unique(cities())), multiple = TRUE)
             )         
      )         
    })       
  })

  #Giving "Select ALL" and "None" Functionality to each filter (This part is redundant for the current problem. I am keeping this so that I could check any solution from stackoverflow should not effect other functionalities)

  #Countries- SelectAll & None
  observe({
    if ("Select All" %in% input$show_vars){
      selected_choices <- setdiff(c("Select All",unique(countries())), "Select All")
      updateSelectizeInput(session, 'show_vars', choices = c("Select All","None",unique(countries())), selected = selected_choices) 
    }
  })  


  observe({
    if ("None" %in% input$show_vars){
      updateSelectizeInput(session, 'show_vars', choices = c("Select All", "None", unique(countries())),selected = "")
    }
  })


  #State- SelectAll & None

  observe({
    if ("Select All" %in% input$show_vars_state){
      selected_choices <- setdiff(c("Select All",unique(states())), "Select All")

      updateSelectizeInput(session, 'show_vars_state', choices = c("Select All","None",unique(states())), selected = selected_choices)

    }
  })

  observe({
    if ("None" %in% input$show_vars_state){
      updateSelectizeInput(session, 'show_vars_state', choices = c("Select All", "None", unique(states())),selected = "")
    }
  })        

  #City- SelectAll & None

  observe({

    if ("Select All" %in% input$show_vars_city){

      selected_choices <- setdiff(c("Select All",unique(cities())), "Select All")

      updateSelectizeInput(session, 'show_vars_city', choices = c("Select All", "None", unique(cities())), selected = selected_choices)

    }
  })

  observe({

    if ("None" %in% input$show_vars_city){
      updateSelectizeInput(session, 'show_vars_city', choices = c("Select All", "None", unique(cities())),selected = "")
    }
  })    
}

shinyApp(ui = ui, server = server)

你可以使用conditionalPanel根据特定条件确定要显示哪些元素。

Notes:

  • I used req() in data_attr因为这是检查file_attr。你不必使用is.null().

  • 我用了三个conditionalPanel国家、州、城市,并指定每一项的条件。

  • 条件内,input.select被使用不input$select.


library(shiny)

ui <- fluidPage(

  fileInput("file_attr", "Door attributes:"),
  selectInput("select", label = "Region Drop down",
              choices = list("None", "Country", "State","City"), selected = "None"),

  # Selectize Inputs ---------------
  uiOutput("Country_List"),
  uiOutput("State_List"),
  uiOutput("City_List")
)

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

  #Reading input
  data_attr <- reactive({
    req(input$file_attr) # make sure a file was uploaded

    read.table(file=input$file_attr$datapath, sep=",", header = TRUE, stringsAsFactors = FALSE)
  })

  # Country selectizeInput ----------------------
  output$Country_List <- renderUI({
    conditionalPanel(
      condition = "input.select == 'Country' | input.select == 'State'| input.select == 'City' ",
      selectizeInput('show_var', 'Country Filter',
                     choices = c("Select All","None", unique(data_attr()$Country)), multiple = TRUE)
    )
  })

  # State selectizeInput ----------------------
  output$State_List <- renderUI({
    conditionalPanel(
      condition = "input.select == 'State' ",
      selectizeInput('show_vars_state', 'State Filter',
                     choices = c("Select All","None", unique(data_attr()$State)), multiple = TRUE)
    )
  })

  # City selectizeInput -----------------------
  output$City_List <- renderUI({
    conditionalPanel(
      condition = "input.select == 'City' ",
      selectizeInput('show_vars_city', 'City Filter',
                     choices = c("Select All","None", unique(data_attr()$City)), multiple = TRUE)
    )
  })

}

shinyApp(ui = ui, server = server)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

需要有关具有多种交互的闪亮动态 UI 算法的帮助 的相关文章

  • 闪亮的如何阻止用户访问选项卡?

    我需要阻止用户访问其他选项卡 直到完成某些操作 在这个可重现的示例中 我想阻止用户访问Tab 2直到他按下按钮 该应用程序如下所示 这是该应用程序的代码 library shiny ui lt shinyUI navbarPage tabP
  • 在闪亮的应用程序和多个页面中进行身份验证

    在我正在开发的系统中 我有 3 个不同的参与者 用户 管理员 支持团队 使用 Shiny App 我想知道如何向这三个参与者进行身份验证 每个参与者只能访问他们的页面 我发现使用闪亮的服务器专业版可以实现这一点 但它不是免费的 有什么方法可
  • R Shiny - 修复了 Shiny 仪表板中的侧边栏和主标题

    我有一个简化的闪亮仪表板 请参阅下面的代码 我想修复侧边栏和主标题 因此 在其他帖子的帮助下 我编写了一个 CSS 文件来解决该问题 sidebar color FFF position fixed width 220px white sp
  • 在R闪亮中,如何在UI端使用在SERVER端计算的值?

    在我的 R闪亮应用程序中 我想调整我的高度d3heatmap 见包装d3热图 https cran r project org web packages d3heatmap index html 作为我的数据框行数的函数 有一个论点heig
  • 在闪亮的应用程序中使用传单地图作为过滤器

    是否可以使用传单地图来过滤闪亮应用程序中的数据 就像在 Tableau 或 Power BI 中一样 方法是单击地图上的某个状态 然后根据条件过滤另一个图表或表格选择 None
  • 将天气 iframe 嵌入到 Shiny Dashboard 中

    我正在尝试将 Forecast io 的天气预报嵌入到闪亮的仪表板中 我最初在使用 符号时遇到了麻烦 但看到一篇文章提供了如何使用特殊字符格式化 HTML 代码的示例 但是 当我运行该应用程序时 我看到一个简单的 未找到 即使我知道该链接有
  • R 闪亮仪表板中的动态重复条件面板

    我正在尝试创建一个动态条件面板 所以我的条件如下 在用户界面中输入 selectInput inpt Input Number seq 1 50 1 selectize FALSE 我的条件面板 UI 输入是 conditionalPane
  • 在闪亮的数据表中为每个单元格显示工具提示或弹出窗口?

    有没有什么方法可以为 r闪亮数据表中的每个单元格获取工具提示 有很多方法可以获取悬停行或列 但我找不到一种方法来获取行和列索引并为每个单元格显示不同的悬停工具提示 任何人都可以修改以下代码吗 library shiny library DT
  • 闪亮井板宽度

    library shiny library shinydashboard ui lt dashboardPage dashboardHeader dashboardSidebar dashboardBody wellPanel tags d
  • 在 Shiny 应用程序中更改 bsModal 的背景

    我正在开发一个 Shiny 应用程序 我需要确保最终用户不会意外关闭 bsModal 因为它上面有一些操作按钮 我做了一些研究并了解到我需要覆盖背景和键盘参数 但即使我看到了一些建议 我也不知道这到底需要放在我的代码中的哪里 我不精通 Ja
  • 如何修改反应链以便最后修改的对象控制其他链接的对象?

    新注释 1 最终解决的代码发布在最底部 反映了 ismirsehregal 于 2021 年 12 月 3 日的解决方案 以及一些标记为 ADDED 和 MODIFIED 的小调整 ADD 是为了解决我在矩阵 2 添加值后从矩阵 1 中删除
  • 使用 sprintf 打印换行符 - 有光泽

    我试图在打印时进行换行 这是我的代码 temp lt LETTERS 1 11 print sprintf Rank s s n 1 11 temp output 1 Rank 1 A n Rank 2 B n Rank 3 C n Ran
  • 使用Shiny和Shinydashboard时如何使图标大小一致?

    我在闪亮的应用程序中添加可点击的图标以显示弹出信息框 请参阅以下屏幕截图和代码示例 我的策略是将我的文本和代码包装起来actionLink in the HTML功能 这效果很好 然而 图标的大小是由关联的大小决定的 我想知道是否可以使所有
  • 如何在Shiny中动态生成的条件面板中格式化条件?

    我正在尝试使用 for 循环在 Shiny 中创建小部件 每个块包含 label 复选框 选择选择器 两个数字输入 我想根据复选框的值和选择选择器的值来设置显示或隐藏两个数字输入的条件 在我创建的 for 循环中 我为每个小部件变量添加了一
  • 单击并按住 R 中的按钮闪亮?

    我希望能够通过单击 R 闪亮按钮来更改参数的值 所以我需要按钮 一个用于增加值 一个用于减少值 我想在按住按钮的同时保持值以一定的速度减少 增加 通过释放按钮的点击 动作应该停止 到目前为止我还没有找到这个选项actionButtons在
  • 在 Shiny 中使用 readlines(prompt = )

    我有一个代码 使用以下方式获取输入readlines prompt 功能 您能告诉我 Shiny 中的哪个输入函数足以将此代码适应 Shiny 应用程序吗 我需要一个交互功能 我无法使用简单的输入selectInput 因为我有很多read
  • R:如何更改ggvis闪亮应用程序中特定范围的绘图背景颜色

    I have a simple shiny app like below and you can run it The plots are created by ggvis and user can choose student name
  • DT数据表中的列对齐

    In my shiny我正在使用的应用程序datatable函数来自DT库构建一个表格并希望将列居中对齐 我可以用formatStyle column textAlign center 但它只影响列体而不影响标题 我们必须设置columnD
  • Shiny :针对所有错误显示一条消息

    我在 R 的 Shiny 中有一个应用程序 我想处理消息 以便用户看不到发生了什么错误 我知道通过 tags style type text css shiny output error visibility hidden shiny ou
  • 如何将 Shiny 中生成的反应图传递到 Rmarkdown 以生成动态报告

    简而言之 我希望能够通过单击按钮从我的闪亮应用程序生成动态 Rmarkdown 报告文件 pdf 或 html 为此 我想我将使用 Shiny 的参数化报告 但不知何故 我无法将单个谜题转移到所需的目标 使用此代码 我们可以在 R Shin

随机推荐