R:在服务器的同一个渲染器的ui中多次使用?

R:在服务器的同一个渲染器的ui中多次使用?,r,shiny,R,Shiny,我需要在UI的多个选项卡中重复使用用户在第一个选项卡中提供的输入 似乎不可能在服务器中使用renderUI并在我的不同选项卡中使用uiOutput调用其输出。这是一个可复制的代码 ui <- pageWithSidebar( headerPanel("Hello !"), sidebarPanel( tabsetPanel( tabPanel("a", textInput(inputId = "xyz", label = "abc", value = "abc")), tabPanel(

我需要在UI的多个选项卡中重复使用用户在第一个选项卡中提供的输入

似乎不可能在服务器中使用renderUI并在我的不同选项卡中使用uiOutput调用其输出。这是一个可复制的代码

ui <- pageWithSidebar(
headerPanel("Hello !"),
sidebarPanel(
tabsetPanel(
tabPanel("a", 
textInput(inputId = "xyz", label = "abc", value = "abc")),
tabPanel("b", uiOutput("v.xyz"))
,tabPanel("b", uiOutput("v.xyz"))
)
),

mainPanel())

server <- function(input,output){
output$v.xyz <- renderUI({
input$xyz
})
}

runApp(list(ui=ui,server=server))
ui在HTML文档中不能(不应该)有两个ID相同的元素(无论是否使用Shining)。当然,当使用具有相同ID的多个元素时,会出现问题。我还主观地认为,通过使用有意义的变量名,您可以大大改进代码

还不太清楚你想用这个输入做什么。是否要在多个选项卡上显示输入框?或者在多个选项卡上显示textInput的值

如果是前者,在我看来,没有一种明显的方法可以做到这一点,而不违反“具有相同ID的多个元素”子句。后者会容易得多(只需使用
renderText
并将其发送到
逐字输出
),但我认为这不是您所要求的

因此,您真正想要的是多个同步的文本输入(具有不同的ID)。您可以在服务器上的单独观察者中使用以下内容:

ui <- pageWithSidebar(
  headerPanel("Hello !"),
  sidebarPanel(
    tabsetPanel(
      tabPanel("a", 
               textInput(inputId = "text1", label = "text1", value = "")),
      tabPanel("b", 
               textInput(inputId = "text2", label = "text2", value = ""))
    )
  ),

  mainPanel()
)

INITIAL_VAL <- "Initial text"

server <- function(input,output, session){  
  # Track the current value of the textInputs. Otherwise, we'll pick up that
  # the text inputs are initially empty and will start setting the other to be
  # empty too, rather than setting the initial value we wanted.
  cur_val <- ""

  observe({
    # This observer depends on text1 and updates text2 with any changes
    if (cur_val != input$text1){
      # Then we assume text2 hasn't yet been updated
      updateTextInput(session, "text2", NULL, input$text1)
      cur_val <<- input$text1
    }
  })

  observe({
    # This observer depends on text2 and updates text1 with any changes
    if (cur_val != input$text2){
      # Then we assume text2 hasn't yet been updated
      updateTextInput(session, "text1", NULL, input$text2)
      cur_val <<- input$text2
    }
  })

  # Define the initial state of the text boxes  
  updateTextInput(session, "text1", NULL, INITIAL_VAL)
  updateTextInput(session, "text2", NULL, INITIAL_VAL)

}

runApp(list(ui=ui,server=server))

ui杰夫·艾伦的例子只有在将
ui
server
功能都保存在同一个文件中时才有效。一旦将它们拆分为ui.R和server.R文件,您就会收到一个错误,抱怨输入值不存在:

警告:观察者中存在未处理的错误:参数的长度为零

在Shiny中有一个可用的解决方案。它还可以处理许多输入小部件,因为扩展代码以观察多个输入小部件变得更加容易。感谢Jeff的例子,我找到了以下解决方案:

ui.R

function(input,output, session){  
# Observe the current value of the textInputs with the Shiny Event Handler. 

  observeEvent(input$text1, function(){
  # Observe changes in input$text1, and change text2 on event
      updateTextInput(session, "text2", NULL, input$text1)
  })

  observeEvent(input$text2, function(){
  # Observe changes in input$text2, and change text1 on event
      updateTextInput(session, "text1", NULL, input$text2)
  })
}
pageWithSidebar(
headerPanel(“最小事件处理程序示例”),
侧栏面板(
选项卡面板(
选项卡面板(“a”,
textInput(inputId=“text1”,label=“text1”,value=”“),
选项卡面板(“b”,
textInput(inputId=“text2”,label=“text2”,value=”“)
)
),
主面板()
)
server.R

function(input,output, session){  
# Observe the current value of the textInputs with the Shiny Event Handler. 

  observeEvent(input$text1, function(){
  # Observe changes in input$text1, and change text2 on event
      updateTextInput(session, "text2", NULL, input$text1)
  })

  observeEvent(input$text2, function(){
  # Observe changes in input$text2, and change text1 on event
      updateTextInput(session, "text1", NULL, input$text2)
  })
}

请注意,默认情况下,
ignoreNULL=TRUE
,因此启动时不会失败。

根据FvD的回答,如果您碰巧使用
uiOutput
renderUI
创建UI元素,则在选择相应的选项卡面板之前,shiny似乎不会创建这些元素,这可能会导致他的解决方案在一开始就失败。(一旦用户使用您希望同步的UI元素在所有选项卡面板中循环,一切正常,因为所有UI元素都已创建)

为了解决这个问题,我创建了一个被动变量来存储用户选择或创建的输入值。然后,当第一次选择另一个具有同步UI元素的tabPanel时,UI元素将使用此被动变量的值呈现

例如,我希望同步多个面板上的
selectInput
元素,这些元素的
selections
是在应用程序加载时创建的(基于文件结构中的任何内容):


ui问题是您在
renderUI
中使用的表达式。您应该使用“返回闪亮标记对象、HTML或此类对象列表的表达式”。(
?renderUI
),不能使用“input$xyz”。谢谢Julien。很抱歉,我是Shiny的新手,对HTML/标记一点都不了解,你能给我建议一个解决方案吗?(我尝试了tag()和HTML(),但都不起作用,可能是因为我也不知道如何使用这些函数)我没有看到,但你遇到的第一个问题是,你不能有几个id相同的输出“x.vyz”(尝试不使用第三个选项卡,您将看到,表达式“input$xyz”将只返回值)。然后您不能用这种方式定义“常规输出”。好的,谢谢Julien。非常感谢Jeff。您提供的内容非常有用,但实际上我需要的是您建议的第一件事(您希望输入框显示在多个选项卡上吗?)。那么有没有一种有效的方法可以使用循环在服务器中写入不同的名称呢?我已经尝试了以下代码,但从初始化
output$v.xyz开始,ui@user1431694不起作用