简体   繁体   中英

How do I add a hyperlink and plots alternatively via renderUI in R shiny?

I am returning a grouped plot object via reactive and want to alternatively render the hyperlink and the plot.

I tried making a taglist with alternating tags

#UI Snippet
mainPanel(uiOutput("plots"))

#Server Snippet
plots2 <- data.test.consecutive.filter %>%
        group_by(SupplierKey, MaterialKey, TareRange,URL) %>%
        arrange (SupplierKey, MaterialKey, TareRange, ShipmentDate) %>%
        do(
          plots = ggplot(data = .)
          + aes(x = ShipmentDate, y = last20.ZScore, group = 1) + ggtitle(
            paste(
              "Supplier:",
              .$SupplierNo,
              " ,",
              "Material Key:",
              .$MaterialKey,
              " ,",
              "Tare Range:",
              .$TareRange
            )
          ) + geom_line(linetype = "dashed") + 
            geom_point() + 
            geom_hline(yintercept =0) + 
            geom_hline(yintercept = 1.5, color = "red") +
            theme(axis.title.x=element_blank(),
                  axis.text.x=element_blank(),
                  axis.ticks.x=element_blank())
        )
vals <- plots2 #reactiveReturn

    output$plots <- renderUI({
      plot_list <- vals()
      plot_output_list  <- list()
      for(i in 1:length(plot_list$plots)){
        plotname <- paste("plot", i, sep = "")
        plot.tag <- plotOutput(plotname, height = 500, width = 1000)
        link.tag <- tags$a(href=plot_list$URL[i],plot_list$SupplierKey[i])
        plot_output_list <- c(plot_output_list,c(plot.tag,link.tag))
      }

      tagList(plot_output_list)

    })


    plot_object <- vals()
    plot_list <- plot_object$plots
    withProgress(message = 'Rendering plots', value = 0, {
      for (i in 1:length(plot_list)) {
        local({
          my_i <- i
          plotname <- paste("plot", my_i, sep="")
          output[[plotname]] <- renderPlot({
            plot_list[[my_i]]
          })

        })
      }
    })

Getting just text in the main panel

Eg: Output div plot1 shiny-plot-output width: 1000px ; height: 500px a URL div plot2 shiny-plot-output width: 1000px ; height: 500px a URL div plot3 shiny-plot-output width: 1000px ; height: 500px a URL div plot4 shiny-plot-output width: 1000px ; height: 500px a URL div plot5 shiny-plot-output width: 1000px ; height: 500px a URL

Expected output: Plot URL Plot URL and so on

In line plot_output_list <- c(plot_output_list,c(plot.tag,link.tag)) .

You shouldn't use c to append shiny::tags , it breaks the structure for all tags. put them in a list and use append instead:

librar(shiny)

Using c breaks the structure

> c(tags$a("test"),tags$a("test1"))
$name
[1] "a"

$attribs
list()

$children
$children[[1]]
[1] "test"


$name
[1] "a"

$attribs
list()

$children
$children[[1]]
[1] "test1"

This doesn't break the structure.

> append(list(tags$a("test")),list(tags$a("test1")))
[[1]]
<a>test</a>

[[2]]
<a>test1</a>

Please see the demo below:

library(shiny)

ui <- fluidPage(
    tags$p("Below doesn't work"),
    uiOutput("hyperlink1"),
    tags$hr(),
    tags$p("Below works"),
    uiOutput("hyperlink2")
)

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

    output$hyperlink1 <- renderUI({
        tagList(
            c(
                tags$a("test"),
                tags$a("test1")
            )
        )
    })


    output$hyperlink2 <- renderUI({
        tagList(
            append(
                list(tags$a("test")),
                list(tags$a("test1"))
            )
        )
    })
}

shinyApp(ui, server)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM