【问题标题】:In R using Shiny: how to make a weblink work in the output在 R 中使用 Shiny:如何使 web 链接在输出中起作用
【发布时间】:2015-08-11 22:14:57
【问题描述】:

我正在使用 RStudio 的 Shiny 进行基本的 MBTI 性格测试。用户回答四个问题,并获得他的性格类型(例如 ENTJ)以及相应的 Wikipedia 链接以阅读有关他的类型的更多信息(例如https://en.wikipedia.org/wiki/ENTJ)。

为此,首先,我使用 actionButton,如 here 所述。其次,我在 server.R 中使用了一堆函数来制作一个有效的网络链接:

shinyServer(function(input, output) {

    # After the Submit button is clicked 
    init <- reactiveValues()
    observe({
            if(input$submit > 0) {
                    init$pasted <- isolate(paste("Your personality type is ", 
                                                 input$b1, input$b2, input$b3, input$b4, sep=""))
                    init$link <- paste("https://en.wikipedia.org/wiki/", 
                                               input$b1, input$b2, input$b3, input$b4, sep="")
                    init$linktext <- a("Find out more about it here", href=init$link, target="_blank")
            }
    })

    # Output
    output$text1 <- renderText({ 
            init$pasted
    })

    output$text2 <- renderText({ 
            init$linktext
    })

})

问题是,当我运行应用程序时,init$pasted 工作得很好,而 init$linktext 却不行 - 说

    Error in cat(list(...), file, sep, fill, labels, append) : 
  argument 1 (type 'list') cannot be handled by 'cat'

任何想法如何解决这个问题?谢谢!

【问题讨论】:

    标签: r shiny rstudio


    【解决方案1】:

    a(...) 的输出是一个列表,无法使用renderText 呈现。您可以在服务器端的ui.R 和renderUI 中使用htmlOutput,这是一个示例:

    server <- function(input, output) {
            output$html_link <- renderUI({
                    a("Find out more about it here", href=paste("https://en.wikipedia.org/wiki/","a","b","c","d", sep=""), target="_blank") 
            })
    }
    
    ui <- shinyUI(fluidPage(
            htmlOutput("html_link")
    ))
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-08
      • 1970-01-01
      • 2019-09-28
      • 2021-09-19
      • 2021-09-15
      • 2016-01-25
      • 1970-01-01
      相关资源
      最近更新 更多