在 R 中使用 Shiny:如何使网络链接在输出中工作
In R using Shiny: how to make a weblink work in the output
我正在使用 RStudio 的 Shiny 进行基本的 MBTI 性格测试。用户回答四个问题,并获得他的性格类型(例如 ENTJ)以及相应的 link 到维基百科以阅读有关他的类型的更多信息(例如 https://en.wikipedia.org/wiki/ENTJ)。
为此,我首先使用 actionButton,如 here 所述。其次,我在 server.R 中使用了一堆函数来制作一个工作网络 link:
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 没有 - saying
Error in cat(list(...), file, sep, fill, labels, append) :
argument 1 (type 'list') cannot be handled by 'cat'
有什么解决办法吗?谢谢!
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)
我正在使用 RStudio 的 Shiny 进行基本的 MBTI 性格测试。用户回答四个问题,并获得他的性格类型(例如 ENTJ)以及相应的 link 到维基百科以阅读有关他的类型的更多信息(例如 https://en.wikipedia.org/wiki/ENTJ)。
为此,我首先使用 actionButton,如 here 所述。其次,我在 server.R 中使用了一堆函数来制作一个工作网络 link:
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 没有 - saying
Error in cat(list(...), file, sep, fill, labels, append) :
argument 1 (type 'list') cannot be handled by 'cat'
有什么解决办法吗?谢谢!
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)