[英]Shiny - plot with renderUI not display in shiny
我只是Shiny的新人,我有一个闪亮的问题。 我有一个情节,但情节不显示闪亮。 没有消息错误。这是代码......
UI
library(shiny)
ui = fluidPage(
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
),
mainPanel(
uiOutput("scatter")
))
)
服务器
library(shiny)
server = function(input, output) {
output$scatter <- renderUI({
datax <- matrix(c(1,2,3,4,5,6),6,1)
datay <- matrix(c(1,7,6,4,5,3),6,1)
titleplot<-"title"
summary <- "testing text"
pl <- plot(datax, datay, main = titleplot, xlab = "input$axis1", ylab = "input$axis2", pch=18, col="blue")
list(
pl,
summary
)
})
}
实际上你也可以使用uiOutput,它有时非常有用,因为你可以从服务器端创建用户界面。 这是解决方案:
library(shiny)
ui = fluidPage(
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
),
mainPanel(
uiOutput("scatter")
))
)
server = function(input, output) {
output$scatter <- renderUI({
datax <- matrix(c(1,2,3,4,5,6),6,1)
datay <- matrix(c(1,7,6,4,5,3),6,1)
titleplot<-"title"
summary <- "testing text"
output$plot_test <- renderPlot({
pl <- plot(datax, datay, main = titleplot, xlab = "input$axis1", ylab = "input$axis2", pch=18, col="blue")
})
plotOutput("plot_test")
})
}
# Run the application
shinyApp(ui = ui, server = server)
更改renderUI
功能的server
,以renderPlot
而uiOutput
到plotOutput
在ui
水涨船高。
library(shiny)
ui = fluidPage(
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
),
mainPanel(
plotOutput("scatter")
))
)
server = function(input, output) {
output$scatter <- renderPlot({
datax <- matrix(c(1,2,3,4,5,6),6,1)
datay <- matrix(c(1,7,6,4,5,3),6,1)
titleplot<-"title"
summary <- "testing text"
pl <- plot(datax, datay, main = titleplot, xlab = "input$axis1", ylab = "input$axis2", pch=18, col="blue")
list(
pl,
summary
)
})
}
shinyApp(ui, server)
您需要为绘图和文本指定单独的output
槽。 这是因为有光泽为每个渲染函数使用不同的(css)类。 以下代码应该做你想要的。
library(shiny)
ui <- fluidPage(
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(),
mainPanel(
plotOutput("scatter"),
textOutput("testingText")
)
)
)
server <- function(input, output) {
output$scatter <- renderPlot({
datax <- matrix(c(1, 2, 3, 4, 5, 6), 6, 1)
datay <- matrix(c(1, 7, 6, 4, 5, 3), 6, 1)
titleplot <- "title"
plot(datax, datay, main = titleplot, xlab = "input$axis1",
ylab = "input$axis2", pch = 18, col = "blue")
})
output$testingText <- renderText({
"testing text"
})
}
shinyApp(ui, server)
附加说明 :该行
pl <- plot( ... )
没有意义。 在R
,绘图不能保存为对象。 ggplots
是一个例外,但您仍然需要使用renderPlot
来显示shiny
的ggplot
对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.