关键词:
【中文标题】Shiny Reactive renderUI 和多个依赖/耦合输入【英文标题】:Shiny Reactive renderUI and multiple dependent / coupled inputs 【发布时间】:2018-07-04 13:44:21 【问题描述】:我有以下示例应用程序,我需要能够切换 multiple_choice_1_source
的输入 OR multiple_choice_2_type
而不会破坏应用程序和隐藏 submit_request_button_ui
和ColnamesInput
当输入改变时。基本上,用户应该能够在单击“提交”按钮后修改输入,并且应用程序应该重置为之前的状态。
我尝试过的:
shinyjs()
- 这只是隐藏而不清除输入。这意味着一旦我按下submit_request_button
,那么对multiple_choice_2_type
所做的任何更改都会被处理并做出反应。在实际应用程序中,我将提交绑定到非常大的表。我想阻止获取
selected_data()
重新运行并清除和隐藏前两个选项中创建的元素。
reactive
- 我试图让观察者监听一些反应性触发器,这些触发器从多个输入中获取依赖关系。我使用user_input_rv
来存储值等,但这失败了,因为观察者被多次触发,所以当我点击提交按钮时,reactive()
中的 if 语句被触发两次,实际上是多次下载每个数据集。它也失败了。
isolate
- 我无法完成这项工作。我尝试了多种隔离组合,但没有成功。
library(shiny)
library(tidyverse)
ui <- fluidPage(
selectizeInput(inputId ='multiple_choice_1_source',
choices = c("db1","db2","db3","db4"), # like this because we want the selected to be blank on initialisation
label = "1. Select source",
multiple = FALSE,
size = 10,
width = '100%'
)
,uiOutput(outputId="multiple_choice_2_type_ui")
,uiOutput(outputId="submit_request_button_ui")
,uiOutput(outputId="ColnamesInput")
)
server <- function(input, output)
user_input_rv = reactiveValues(
source_picked = NULL,
last_used_source = NULL,
type_picked = NULL,
series_picked = NULL,
last_used_series = NULL,
selected_data = NULL,
final_selection = NULL
)
observeEvent(input$multiple_choice_1_source,
user_input_rv$source_picked <- input$multiple_choice_1_source
#change data loaded under type picked.
user_input_rv$type_picked <-
if ( input$multiple_choice_1_source == "db1") paste0(colnames(mtcars))
else if ( input$multiple_choice_1_source == "db2") paste0(colnames(diamonds))
else if ( input$multiple_choice_1_source == "db3") NULL
else if ( input$multiple_choice_1_source == "db4") NULL
output$multiple_choice_2_type_ui <- renderUI(
selectizeInput( inputId = 'multiple_choice_2_type',
choices = paste(user_input_rv$type_picked),
label= "2. Select type",
multiple = TRUE,
size = 10,
width = '100%',
options = list( placeholder = 'Type',
maxItems =1
)
)
)
) #first observeEvent for source type and data load.
observeEvent(input$multiple_choice_2_type,
output$submit_request_button_ui <- renderUI(
actionButton(
inputId = "submit_request_button",
label = " Get data "
)
)
)#second observeEvent for submit_request_button_ui
observeEvent(input$submit_request_button,
selected_data <- reactive(
if( input$multiple_choice_1_source =="db1")
mtcars
else if ( input$multiple_choice_1_source == "db1")
diamonds
else if ( input$multiple_choice_1_source == "db3") NULL
else if ( input$multiple_choice_1_source == "db4") NULL
)
user_input_rv$series_picked <- input$multiple_choice_2_type
user_input_rv$selected_data <- selected_data()
min_cols <- as.integer(1) # default 1
max_cols <- as.integer(length(colnames(selected_data())))
#print(max_cols)
#this renderUI creates the right-hand side column of the app COLUMNS
output$ColnamesInput <- renderUI(
lapply(min_cols:max_cols, function(z)
column(width = 3,
offset = 0,
selectInput( inputId = paste0("cols","_",z),
label = paste(input$multiple_choice_2_type,": ",colnames(selected_data())[z]),
choices = unique(selected_data()[[z]]),
multiple = TRUE
) #selectizeInput
)
)#lapply inner
) #renderUI for columns
) #third observeEvent for data selection and customisation
shinyApp(ui = ui, server = server)
【问题讨论】:
您是否尝试过使用eventReactive
?
是的,但我只尝试将反应式替换为selected_data()
如果您希望仅在按下提交按钮时更改值,那么您为什么使用reactive expression
或reactiveValues
?
好问题 - 总结了我对闪亮反应的舒适程度......
【参考方案1】:
这是我从中删除 reactive expression
并改用局部变量 selected_data
的代码。
observeEvent(input$submit_request_button,
# selected_data <- reactive(
# browser()
selected_data <- NULL
if( input$multiple_choice_1_source =="db1")
selected_data <- mtcars
else if ( input$multiple_choice_1_source == "db1")
selected_data <- diamonds
else if ( input$multiple_choice_1_source == "db3") selected_data <- NULL
else if ( input$multiple_choice_1_source == "db4")selected_data <- NULL
# )
user_input_rv$series_picked <- isolate(input$multiple_choice_2_type)
user_input_rv$selected_data <- selected_data
min_cols <- as.integer(1) # default 1
max_cols <- as.integer(length(colnames(selected_data)))
#print(max_cols)
#this renderUI creates the right-hand side column of the app COLUMNS
output$ColnamesInput <- renderUI(
lapply(min_cols:max_cols, function(z)
column(width = 3,
offset = 0,
selectInput( inputId = paste0("cols","_",z),
label = paste(isolate(input$multiple_choice_2_type),": ",colnames(selected_data)[z]),
choices = unique(selected_data[[z]]),
multiple = TRUE
) #selectizeInput
)
)#lapply inner
) #renderUI for columns
) #third observeEvent for data selection and customisation
现在,当您更改选择输入选项时,ColnamesInput
不会被触发。只有在您单击提交按钮后才会触发它。
[编辑]:
可能不是最好的方法,但我认为我能够实现您想要的。另外,我冒昧地使用了您的服务器中已经定义的reactiveValue
。看看下面修改后的服务器代码:
server <- function(input, output)
user_input_rv = reactiveValues(
source_picked = NULL,
last_used_source = NULL,
type_picked = NULL,
series_picked = NULL,
last_used_series = NULL,
selected_data = NULL,
final_selection = NULL
)
observeEvent(input$multiple_choice_1_source,
user_input_rv$source_picked <- input$multiple_choice_1_source
###Start: To check if the source changed#########
if(!is.null(user_input_rv$last_used_source))
if(user_input_rv$last_used_source != user_input_rv$source_picked)
shinyjs::hide("ColnamesInput")
user_input_rv$last_used_source = user_input_rv$source_picked
else
user_input_rv$last_used_source = user_input_rv$source_picked
###End: To check if the source changed#########
#change data loaded under type picked.
user_input_rv$type_picked <-
if ( input$multiple_choice_1_source == "db1") paste0(colnames(mtcars))
else if ( input$multiple_choice_1_source == "db2") paste0(colnames(diamonds))
else if ( input$multiple_choice_1_source == "db3") NULL
else if ( input$multiple_choice_1_source == "db4") NULL
output$multiple_choice_2_type_ui <- renderUI(
selectizeInput( inputId = 'multiple_choice_2_type',
choices = paste(user_input_rv$type_picked),
label= "2. Select type",
multiple = TRUE,
size = 10,
width = '100%',
options = list( placeholder = 'Type',
maxItems =1
)
)
)
) #first observeEvent for source type and data load.
observeEvent(input$multiple_choice_2_type,
###Start: To check if the series changed#########
user_input_rv$series_picked <- input$multiple_choice_2_type
if(!is.null(user_input_rv$last_used_series))
if(user_input_rv$last_used_series != user_input_rv$series_picked)
shinyjs::hide("ColnamesInput")
user_input_rv$last_used_series = user_input_rv$series_picked
else
user_input_rv$last_used_series = user_input_rv$series_picked
###End: To check if the series changed#########
output$submit_request_button_ui <- renderUI(
actionButton(
inputId = "submit_request_button",
label = " Get data "
)
)
)#second observeEvent for submit_request_button_ui
observeEvent(input$submit_request_button,
# selected_data <- reactive(
# browser()
shinyjs::show("ColnamesInput")
selected_data <- NULL
if( input$multiple_choice_1_source =="db1")
selected_data <- mtcars
else if ( input$multiple_choice_1_source == "db1")
selected_data <- diamonds
else if ( input$multiple_choice_1_source == "db3") selected_data <- NULL
else if ( input$multiple_choice_1_source == "db4")selected_data <- NULL
# )
user_input_rv$series_picked <- isolate(input$multiple_choice_2_type)
user_input_rv$selected_data <- selected_data
min_cols <- as.integer(1) # default 1
max_cols <- as.integer(length(colnames(selected_data)))
#print(max_cols)
#this renderUI creates the right-hand side column of the app COLUMNS
output$ColnamesInput <- renderUI(
lapply(min_cols:max_cols, function(z)
column(width = 3,
offset = 0,
selectInput( inputId = paste0("cols","_",z),
label = paste(isolate(input$multiple_choice_2_type),": ",colnames(selected_data)[z]),
choices = unique(selected_data[[z]]),
multiple = TRUE
) #selectizeInput
)
)#lapply inner
) #renderUI for columns
) #third observeEvent for data selection and customisation
希望对你有帮助!
【讨论】:
谢谢@SBista。问题是当两个selectizes
的选择发生变化时,ColnamesInput
仍然可见。我需要隐藏它们。
@J.Doe.,我已经编辑了代码来实现你想要的。看一看。希望对您有所帮助!
谢谢@SBista。它完成了工作。我认为它尽可能高效。我在顶部的reactiveValues
上走在正确的轨道上,但我不止一次失败了。 100% 正确的是嵌套的 if 语句。再次感谢您!如何在 R Shiny 中的 renderUI 中显示表格中的输入框?
】如何在RShiny中的renderUI中显示表格中的输入框?【英文标题】:HowtodisplayinputboxesinatablefromrenderUIinRShiny?【发布时间】:2021-05-0216:36:01【问题描述】:现在所有来自uiOutput("prefs")的数字输入框都显示在彼此下方。我想让它们显示... 查看详情
R Shiny - 将tabPanel动态添加到tabsetPanel(使用renderUI)
】RShiny-将tabPanel动态添加到tabsetPanel(使用renderUI)【英文标题】:RShiny-addtabPaneltotabsetPaneldynamically(withtheuseofrenderUI)【发布时间】:2013-10-2812:33:00【问题描述】:我正在开发一个闪亮的应用程序,我在其中使用tabsetPanel,它是在... 查看详情
如何使用 `renderUI` 响应式更新 Shiny 应用程序中的活动菜单项?
】如何使用`renderUI`响应式更新Shiny应用程序中的活动菜单项?【英文标题】:HowcanIreactivelyupdatetheactivemenuIteminaShinyappusing`renderUI`?【发布时间】:2021-03-2119:16:17【问题描述】:我正在构建一个闪亮的应用程序,它可以从数据框中... 查看详情
R Shiny Reactive 值,dplyr 过滤器错误?
】RShinyReactive值,dplyr过滤器错误?【英文标题】:RShinyReactivevalues,dplyrfiltererror?【发布时间】:2019-05-0409:54:32【问题描述】:当我在UI中选择输入以立即在页面上反映结果时,我试图弄清楚。我的搜索结果让我研究了反应式表达... 查看详情
R Shiny Dynamic 选择输入 - 捕获事件
...【发布时间】:2019-06-2918:43:04【问题描述】:我正在使用renderUI在我的UI上创建动态文本框和下拉菜单。我想捕捉文本框/下拉菜单中的变化事件并修改数据框下面是创建用户界面的代码server<-function(input,output,session)output$fileC 查看详情
如何在 Shiny 中引用 ui.R 中的反应元素
...作为dragSetUI的参数,该函数需要在ui.R中运行。我尝试了renderUI和uiOutput,它几乎可以工作,但是拖动的元素无法在放置区域中放置。正如 查看详情
Shiny R 中的 UI 功能
...中添加一些输入,我不知道该怎么做,我已经完成了一个renderUI,然后是一个uiOutput,但我没有功能......我添加代码:inputs=function(dades1)for(iin1:ncol(dades1))if(dades1[i]=="Edad")in 查看详情
闪亮的模块和 renderUI 传递 javascript 代码
】闪亮的模块和renderUI传递javascript代码【英文标题】:ShinymodulesandrenderUIpassingjavascriptcode【发布时间】:2019-02-2419:05:57【问题描述】:我一直在尝试让我的renderUI代码响应@StéphaneLaurent共享的slick.js实现。基本上我有创建表的模块... 查看详情
如何重用 uiOutput 和 renderUI 中的 selectInput?
】如何重用uiOutput和renderUI中的selectInput?【英文标题】:HowtoreusetheselectInputfromuiOutputandrenderUI?【发布时间】:2019-11-1521:11:42【问题描述】:我们如何重用用于selectInput下拉菜单的uiOutput重用Server函数中的columns2并在UI中显示?Shinyre... 查看详情
闪亮:reactive() 检查 read.csv
】闪亮:reactive()检查read.csv【英文标题】:Shiny:reactive()checkonread.csv【发布时间】:2017-08-1700:11:58【问题描述】:我正在编写一个闪亮的服务器脚本,我希望reactive()首先检查我的c驱动器上的数据,如果它在那里,那么很好,否则... 查看详情
在 Shiny 中如何用 JavaScript 改变 UI 元素的属性?
...时间】:2021-11-2810:10:02【问题描述】:我知道有一个叫做renderUI的服务器端方法,但它在某些情况下会导致更新UI非常慢,所以我现在依赖JavaScript。我的问题是跟随。我想从shinymaterial包中更新mater 查看详情
R - Shiny 上的实时图表
...(getDataIntraday()接收两个输入并返回更新的xts)。getID<-reactive 查看详情
响应式更新模块化 Shiny 应用程序中的侧边栏
...述】:我有一个使用bs4Dash的模块化Golem应用程序。我想从renderUI动态生成的actionBttn更新活动侧边栏选项卡。虽然updatebs4ControlbarMenu按预期工作asshownhere,但它在应用程序的模块 查看详情
R Shiny:reactiveValues 与响应式
】RShiny:reactiveValues与响应式【英文标题】:RShiny:reactiveValuesvsreactive【发布时间】:2017-01-1902:07:24【问题描述】:此问题与thisone有关。两者可以生成相同的功能,但实现方式略有不同。一个显着的区别是reactiveValue是一个可以有... 查看详情
R Shiny Dashboard - uiOutput 未在选项卡项内呈现
...值范围的滑块。为此,我在服务器上生成sliderInput并使用renderUI/uiOuput显示它。在下面的示例中,如果我只在一个tabPa 查看详情
分配/更改动态生成的 UI 组件的 div ID (R Shiny/Javascript)
...时间】:2021-12-2909:33:01【问题描述】:我正在使用uiOutput/renderUI在R闪亮应用程序中动态生成几个表。这些表是可编辑的,因为它们呈现检索到的数据,然后用户可以更改这 查看详情
在 R Shiny 中动态创建单选按钮
...p>available_years<-c("2019","2020","2021")output$available_years<-reactive 查看详情
shinyjs 不会隐藏使用 renderUI 创建的按钮
】shinyjs不会隐藏使用renderUI创建的按钮【英文标题】:shinyjswon\'thidebuttoncreatedwithrenderUI【发布时间】:2021-06-2616:14:02【问题描述】:我有一个使用shinyproxy制作的闪亮应用程序。在server.R上,我使用renderUI创建了一些UI元素,如下... 查看详情