动态添加带有标题的剧情

时间:2018-12-30 11:58:27

标签: r shiny

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

我尝试使用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)

1 个答案:

答案 0 :(得分:1)

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)

编辑

已编辑问题的解决方案:

private FirebaseStorage storage = FirebaseStorage.getInstance();
private StorageReference storageRef = storage.getReferenceFromUrl("gs://show-b8129.appspot.com/Mesh_Beagle.gltf");