R保持标题和文本框的更新

R保持标题和文本框的更新,r,shiny,R,Shiny,在闪亮的应用程序中,我希望每次用户更改打印类型时,标题都是默认标题,但我希望用户能够手动更改文本框中的值,并更新打印标题。这基本上相当于: 规则 1) 每次用户更改下拉列表中的打印类型时,请使用默认标题并使用标题更新文本框 2) 每次用户在文本框中进行更改时,将标题替换为文本框中的文本 例如: 1) 如果用户选择打印类型“n”,则标题将为“打印类型为n”,文本框将相同 2) 然后,如果用户在文本框中键入“诸如此类”,则绘图标题将变为“诸如此类” 3) 然后,当用户将绘图类型更改为“p”时,标题变

在闪亮的应用程序中,我希望每次用户更改打印类型时,标题都是默认标题,但我希望用户能够手动更改文本框中的值,并更新打印标题。这基本上相当于:

规则 1) 每次用户更改下拉列表中的打印类型时,请使用默认标题并使用标题更新文本框

2) 每次用户在文本框中进行更改时,将标题替换为文本框中的文本

例如: 1) 如果用户选择打印类型“n”,则标题将为“打印类型为n”,文本框将相同

2) 然后,如果用户在文本框中键入“诸如此类”,则绘图标题将变为“诸如此类”

3) 然后,当用户将绘图类型更改为“p”时,标题变为“绘图类型为p”

等等,建议

library(shiny)

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

  output$plot <- renderUI({

    h <- input$height
    w <- input$width
    list(
      tags$h3("Plot information"),
      renderPlot({

        # if input$title has been changed by the user update the
        # plot title to input$title, otherwise use default_title

        default_title <- paste("Plot type is", input$type)
        updateTextInput(session, "title", value=default_title)
        plot(1:10, main=default_title, type=input$type)

      })
    )
  })

} 

ui <- fluidPage(
  tags$div(class="panel-body",

           textInput("title", "Give a title",  "This is my initital title"),
           selectInput("type", "choose type", choices=c("p", "n", "s"), selected="s")
  ),

  uiOutput("plot")

)

shinyApp(ui = ui, server = server)
库(闪亮)

服务器下面的代码应该可以工作。我将逻辑分为两个不同的
observe

library(shiny)

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

  # This updates both the text input and the plot when type changes
  observe({
    type <- input$type
    default_title <- paste("Plot type is", input$type)
    updateTextInput(session, "title", value = default_title)
    output$plot <- renderPlot({
      plot(1:10, main = default_title, type = input$type)
    })
  })

  # This updates the plot when input changes
  observe({
    title <- input$title
    output$plot <- renderPlot({
      plot(1:10, main = title, type = input$type)
    })
  })

  output$plotInfo <- renderUI({
    list(tags$h3("Plot information"))
  })

}


ui <- fluidPage(

  tags$div(
    class = "panel-body",
    textInput("title", "Give a title",  "This is my initital title"),
    selectInput(
      "type",
      "choose type",
      choices = c("p", "n", "s"),
      selected = "s"
    )
  ),

  uiOutput("plotInfo"),
  plotOutput("plot")

)

shinyApp(ui = ui, server = server)
库(闪亮)

服务器下面的代码应该可以工作。我将逻辑分为两个不同的
observe

library(shiny)

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

  # This updates both the text input and the plot when type changes
  observe({
    type <- input$type
    default_title <- paste("Plot type is", input$type)
    updateTextInput(session, "title", value = default_title)
    output$plot <- renderPlot({
      plot(1:10, main = default_title, type = input$type)
    })
  })

  # This updates the plot when input changes
  observe({
    title <- input$title
    output$plot <- renderPlot({
      plot(1:10, main = title, type = input$type)
    })
  })

  output$plotInfo <- renderUI({
    list(tags$h3("Plot information"))
  })

}


ui <- fluidPage(

  tags$div(
    class = "panel-body",
    textInput("title", "Give a title",  "This is my initital title"),
    selectInput(
      "type",
      "choose type",
      choices = c("p", "n", "s"),
      selected = "s"
    )
  ),

  uiOutput("plotInfo"),
  plotOutput("plot")

)

shinyApp(ui = ui, server = server)
库(闪亮)

服务器更改
selectInput(“type”)
时,还是用户直接在
textInput()
框中键入内容时,您希望更改打印标题?感谢更新-您现在的要求更清楚。更改
selectInput(“type”)
时,您希望更改打印标题吗,或者当用户直接在
textInput()
框中键入内容时?谢谢您的更新-您现在的问题更清楚了。