【问题标题】:Dynamically add plot with title in shiny动态添加带有闪亮标题的绘图
【发布时间】:2018-12-31 19:38:59
【问题描述】:

我想使用闪亮的动态生成图,但每个图都有不同的标题。

我曾尝试使用 for bucle 生成 n 个图并使用 observeEvent 显示它们,但这对我不起作用,因为图的主要内容被忽略了。

为了确保每个情节都有自己的主标题,我所做的是将标题存储到 data.frame 并从情节中访问它。

代码如下:

    library(shiny)

ui <- fluidPage(

    textInput("title","Title",""),
    actionButton("generate","Plot"),
    div(class="aux",style="width:300px;height:200px")

)


server <- function(input,output){

    observeEvent(input$generate,{
                  insertUI(
                      selector= ".aux",
                      where="beforeBegin",
                      ui = plotOutput(paste0("plot",input$generate))

                  )

        if(input$generate == 1){

            data <<- data.frame(title = input$title)
        }else{
            aux <- data.frame(title=input$title)
            data <<- rbind(data,aux)

        }   


    })

    for(i in 1:10){

        output[[paste0("plot",i)]] <- renderPlot(
            plot(rnorm(100),main=data[i,"title"])

        )


    }



}


shinyApp(ui,server)

这个其他代码做了我真正想做的事情,但是手动声明绘图不是很好的编程:

library(shiny)

ui <- fluidPage(

    textInput("title","Title",""),
    actionButton("generate","Plot"),
    div(class="aux",style="width:300px;height:200px")

)


server <- function(input,output){

    observeEvent(input$generate,{
                  insertUI(
                      selector= ".aux",
                      where="beforeBegin",
                      ui = plotOutput(paste0("plot",input$generate))

                  )

        if(input$generate == 1){

            data <<- data.frame(title = input$title)
        }else{
            aux <- data.frame(title=input$title)
            data <<- rbind(data,aux)

        }   


    })



        output$plot1 <- renderPlot(
            plot(rnorm(100),main=data[1,"title"])
        )
        output$plot2 <-    output$plot1 <- renderPlot(
            plot(rnorm(100),main=data[2,"title"])
        )





}


shinyApp(ui,server)

已编辑

使用 Stephane Laurent 的建议,我将 insertUI 和 output[[plot]] 放在了 observeEvent 中,但这并不能解决能够编辑更改 data.frame 标题的绘图标题的问题。代码如下:

library(shiny)
library(data.table)

ui <- fluidPage(
  column(6,
  textInput("title","Title",""),
  actionButton("generate","Plot"),
  div(id="aux")),
  column(6,
  textInput("newt","New title",""),
  selectInput("row","Row",choices=c(1:10)),
  actionButton("change","Change title"))


)


server <- function(input,output){
  observeEvent(input$change,{
    df$title <<- as.character(df$title)
    df[input$row,"title"]<-input$newt 


  })
  k <- 0
  observeEvent(input$generate, {
    insertUI(
      selector= "#aux",
      where="beforeBegin",
      ui = plotOutput(paste0("plot",input$generate))
    )
    k <- k + input$generate
    if(input$generate==1){
      df <<- data.frame(title = input$title)
      df$title <<- as.character(df$title)

    }else{

      aux <- data.frame(title = input$title)
      df <<- rbind(df,aux)
      df$title <<- as.character(df$title)
    }


    output[[paste0("plot",input$generate)]] <- renderPlot(
      plot(rnorm(100), main = df[k,"title"])      
    )









  })  
}

shinyApp(ui,server)

【问题讨论】:

  • 我认为你必须使用local,比如这里:stackoverflow.com/questions/52414001/…
  • 谢谢,但该解决方案的问题是当您单击生成图时会生成所有图。我想要的是每次点击只会添加一个情节。
  • 那你为什么要循环呢?
  • 因为循环只生成图而不显示它们,只有当我点击生成图时,insertUI 才会插入图。问题是这个图没有动态地从 data.frame 中获取标题的值。

标签: r shiny


【解决方案1】:

renderPlot放在观察者里面:

library(shiny)

ui <- fluidPage(

  textInput("title","Title",""),
  actionButton("generate","Plot"),
  div(id="aux")

)


server <- function(input,output){

  observeEvent(input$generate, {
    insertUI(
      selector= "#aux",
      where="beforeBegin",
      ui = plotOutput(paste0("plot",input$generate))
    )

    output[[paste0("plot",input$generate)]] <- renderPlot(
      plot(rnorm(100), main = isolate(input$title))      
    )
  })  
}

shinyApp(ui,server)

编辑

已编辑问题的解决方案:

library(shiny)

ui <- fluidPage(

  column(6,
         textInput("title","Title",""),
         actionButton("generate","Plot"),
         div(id="aux")),
  column(6,
         textInput("newt","New title",""),
         selectInput("row","Row",choices=c(1:10)),
         actionButton("change","Change title"))  
)    

server <- function(input,output){

  titles <- reactiveValues()

  observeEvent(input$change, {
    titles[[input$row]] <- input$newt
  })

  values <- replicate(10, rnorm(100))

  for(i in 1:10){
    local({
      ii <- i
      output[[paste0("plot",ii)]] <- renderPlot(
        plot(values[,ii], main = titles[[as.character(ii)]])
      )
    })
  }

  observeEvent(input$generate, {
    titles[[as.character(input$generate)]] <- input$title
    insertUI(
      selector = "#aux",
      where = "beforeBegin",
      ui = plotOutput(paste0("plot",input$generate))
    )
  })

}

shinyApp(ui,server)

【讨论】:

  • 它有效,并且是一个非常好的近似值,但我需要将标题存储在 data.frame 中,因此如果您更改 data.frame 值,标题将会更改。我已经在 observeEvent 中包含了 data.frame 的生成,并通过 main=df[input$generate,"title"] 更改了 main=isolate...,但它不起作用
猜你喜欢
  • 2022-10-04
  • 2013-03-30
  • 2021-08-03
  • 1970-01-01
  • 1970-01-01
  • 2016-07-02
  • 1970-01-01
  • 2015-10-19
  • 1970-01-01
相关资源
最近更新 更多