【问题标题】:How do I add a hyperlink and plots alternatively via renderUI in R shiny?如何通过 R Shiny 中的 renderUI 添加超链接和绘图?
【发布时间】:2020-01-13 12:39:37
【问题描述】:

我通过反应返回一个分组的绘图对象,并希望交替呈现超链接和绘图。

我尝试用交替标签制作一个标签列表

#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]]
          })

        })
      }
    })

只在主面板中获取文本

例如:Output div plot1 闪亮绘图输出宽度:1000px;高度:500px a 网址 div plot2 闪亮绘图输出宽度:1000px;高度:500px a 网址 div plot3 闪亮绘图输出宽度:1000px;高度:500px a 网址 div plot4 闪亮绘图输出宽度:1000px;高度:500px a 网址 div plot5 闪亮绘图输出宽度:1000px;高度:500px a 网址

预期输出: 阴谋 网址 阴谋 网址等

【问题讨论】:

  • 你能说得更具体点吗?什么工作/不工作。您当前的输出和期望的输出有什么区别?既然您想改进 UI,最好能截屏并向我们展示您所拥有的。
  • 编辑:为清楚起见添加了屏幕截图

标签: r shiny


【解决方案1】:

在线plot_output_list &lt;- c(plot_output_list,c(plot.tag,link.tag))

您不应该使用c 附加shiny::tags,它会破坏所有标签的结构。将它们放在一个列表中并改用append

librar(shiny)

使用c 会破坏结构

> 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"

这不会破坏结构。

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

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

请看下面的演示:

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)

【讨论】:

  • 应用程序现在无响应且没有错误。 r append(list(plot_output_list),list(plot.tag)) append(list(plot_output_list),list(link.tag)) 添加了这段代码
猜你喜欢
  • 1970-01-01
  • 2020-03-22
  • 1970-01-01
  • 2013-10-28
  • 2017-03-29
  • 2020-09-29
  • 2016-11-28
  • 2017-06-22
  • 2021-08-12
相关资源
最近更新 更多