闪亮 - 绘图与renderUI不显示闪亮

时间:2016-05-11 13:51:42

标签: r 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
    )


    })

}

3 个答案:

答案 0 :(得分:5)

实际上你也可以使用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)

答案 1 :(得分:1)

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)

答案 2 :(得分:0)

您需要为绘图和文本指定单独的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是一个例外,但您仍然必须使用renderPlotggplot中显示shiny个对象。