【问题标题】:R Shiny keep title and text box updatedR Shiny 保持标题和文本框更新
【发布时间】:2016-03-08 21:28:44
【问题描述】:

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

规则

1) 每次用户更改下拉菜单中的绘图类型时,使用默认标题并使用标题更新文本框。

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

举个例子:

1) 如果用户选择绘图类型“n”,则标题将为“绘图类型为 n”,文本框相同。

2) 然后,如果用户在文本框中键入“Blah, blah”,则情节标题变为“Blah blah”。

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)

【问题讨论】:

  • selectInput("type") 更改时,或者当用户直接在textInput( ) 框中键入时,您希望绘图标题更改吗?
  • 感谢您的更新 - 您现在的要求更清楚了。

标签: r shiny


【解决方案1】:

下面的代码应该可以工作。我将逻辑分成两个不同的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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 2017-06-11
    • 1970-01-01
    • 2021-08-27
    • 2017-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多