需要有关具有多种交互的闪亮动态 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 算法的帮助 的相关文章

  • 在 Shiny 中显示反应式 htmlTable 表格

    我正在制作我的第一个 Shiny 应用程序 但找不到任何有关如何显示使用 htmlTable 包创建的表格的示例 我基本上想在按下按钮时创建一个表格并显示它 Shiny 显示 html 代码而不是表格 我不知道用什么替换服务器部分中的 re
  • 在 Shiny 应用程序中更改 bsModal 的背景

    我正在开发一个 Shiny 应用程序 我需要确保最终用户不会意外关闭 bsModal 因为它上面有一些操作按钮 我做了一些研究并了解到我需要覆盖背景和键盘参数 但即使我看到了一些建议 我也不知道这到底需要放在我的代码中的哪里 我不精通 Ja
  • 如何在 Shiny 应用程序中访问/打印/跟踪当前选项卡选择?

    我正在一个闪亮的应用程序中工作 我希望能够访问用户在会话中当前所在选项卡上的信息 我有一个观察事件 用于侦听要单击的特定按钮 简而言之 我想存储 打印用户单击此按钮时所在的当前选项卡 单击此按钮后 选项卡将更改为带有 updateTabIt
  • 闪亮的演示文稿 (ioslides):自定义 CSS 和徽标

    我安装了以下内容 RStudio 预览版 版本 0 98 864 2014 年 5 月 24 日 knitr 和shiny 的开发版本 来自 devtools install github c yihui knitr rstudio shi
  • r Shiny 中的 fileInput 函数没有响应

    我是 R 和 R闪亮的新手 一直致力于构建一个统计应用程序 该应用程序将允许用户导入文件 然后对数据运行不同的统计程序 直到最近 fileData 函数一直对我来说运行良好 现在每当我尝试上传文件时 都不会打开任何内容 我已尝试了所有我能想
  • 在 R Shiny 中显示/隐藏整个框元素

    我目前正在尝试找到一种方法来隐藏 显示 R Shiny 中的整个 box 元素 以及里面的所有内容 我想创建一个可能的按钮 它允许用户展开特定框 然后使用相同 甚至不同 的按钮隐藏它 我不想使用条件面板 因为我的应用程序非常大并且会产生一些
  • 将 csv 文件上传到shinyApps.io

    我的应用程序在本地运行良好 并且我能够成功地将应用程序部署到shinyapps io 服务器 但是当我尝试使用shinyapps URL 在浏览器中加载应用程序时 收到以下错误消息 错误对象 数据 不是成立 我认为这是因为 data 变量从
  • 单击 R Shiny 中的按钮后将输入字段重置为 null

    我正在构建一个应用程序 用户可以在其中按列输入表的数据值 单击 添加 按钮后 输入的值将按列附加到现有值 例如 如果输入 col1 2 3 并单击 ADD 我们将在显示屏中看到 col1 2 3 如果输入 col2 4 7 并单击 ADD
  • R Shiny 中表格的条件格式

    我正在尝试可视化队列分析 并想使用RenderDataTable闪亮以获得这种可视化效果 我将能够突出显示基于具有值 1 0 的单独列的所有单元格 其中 1 被着色 0 不被着色 我尝试了几件事 包括尝试使用geom tile in ggp
  • 根据用户输入将 n 个反应式单选按钮添加到闪亮的应用程序

    我正在尝试创建一个闪亮的应用程序 用户可以在其中从数据框中选择变量以便对数据进行子集化 输出 最终 将是包含用户子集的数据表 我需要根据用户为子集选择的变量数量创建 n 个输入框 理想情况下 输入框将是动态单选按钮 用于子集因子 我还没有开
  • 使用shinyjs通过javascript在闪亮的应用程序中操作现有的Leaflet地图

    我有一个闪亮的应用程序 其中包含现有的传单地图 我希望能够在渲染后使用自定义 javascript 通过shinyjs包裹 一个最小的例子如下 app R packages library dplyr library leaflet lib
  • 闪亮的本地部署错误:输入字符串 1 无效 UTF-8

    我很惊讶地发现一个突然的错误 我的 ShinyApp 停止工作并出现未知错误 提示 输入字符串 1 无效 UTF 8 即使在昨天 该应用程序也可以正常运行 但是突然停止了 下面是我运行时的错误描述runApp gt runApp Liste
  • 有没有办法在 R Shiny 应用程序加载时自动验证对 GoogleSheets 的访问? Googlesheets4 身份验证问题

    我目前正在使用 R Shiny 应用程序 它利用 googlesheets4 从 GoogleSheet 读取数据 我认识到身份验证对于访问 GoogleSheets 很重要 因此我尝试使用 app R 文件中的以下代码对应用程序进行身份验
  • 使用 R Shiny 从 XLConnect 下载 Excel 文件

    有没有人尝试过使用 R Shiny 中的下载处理程序通过 XLConnect 下载新创建的 Excel 文件 在 ui R 中有一行不起眼的行 downloadButton downloadData Download 在 server R
  • 使用 Shiny 发布平行坐标图表时出现“错误:路径[1]="”:没有这样的文件或目录”

    我有一个似乎很常见但我还没有找到解决方案的问题 当尝试使用 rCharts Parcoords 发布 Web 应用程序时 出现以下错误 错误 路径 1 没有这样的文件或目录 奇怪的是 该应用程序在我的笔记本电脑上运行得很好 下面是我正在使用
  • 在 Shiny 中的用户会话之间共享反应数据集

    我有一个相当大的反应数据集 该数据集是通过轮询文件然后按预定义的时间间隔读取该文件而派生的 数据更新频繁 需要不断重新加载 诚然 重新加载可以增量完成并附加到 R 中的现有对象 但事实并非如此 然而目前 尽管会话中的数据相同 但此操作是针对
  • 闪亮应用程序中的本地图像没有 img(src())?

    我想按照以下说明在我的闪亮应用程序中包含本地图像文件 在闪亮的应用程序中嵌入图像 https stackoverflow com questions 21996887 embedding image in shiny app 然而 由于某种
  • R闪亮数据表在开始时不显示记录(行)

    我正在构建一个带有数据表的闪亮应用程序 我想要的是启动时不显示任何记录 行 这样您只能看到表格顶部的过滤器 当您开始输入时 会显示行 我在数据表中找不到选项 这可能吗 下面是示例代码 shinyApp ui navbarPage title
  • 在 Shiny 中设置一个绘图缩放以匹配另一个绘图缩放

    我正在尝试使用情节重排获取一个图的 x 轴缩放限制 并将它们应用到 Shiny 中的另一个图 到目前为止 我可以从 plot1 x轴限制 获取相关的plotly relayout数据 将其转换 从数字到日期 并在绘制 plot2 之前将其提
  • 非闪亮上下文中的反应式对象绑定

    实际问题 你怎样才能近似反应性环境 行为 http shiny rstudio com tutorial lesson6 建立者shiny http shiny rstudio com函数 或者甚至可能在一个函数中使用这些函数无光泽上下文以

随机推荐