Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shiny - plot with renderUI not display in shiny

Tags:

r

shiny

i just new in Shiny and i have a problem in shiny. i have a plot but the plot not display in shiny. and no message error.this is the code...

UI

library(shiny)
ui = fluidPage(
  titlePanel("Hello Shiny!"),
  sidebarLayout(
    sidebarPanel(

    ),
    mainPanel(

     uiOutput("scatter")

               ))
  )

server

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
    )


    })

}
like image 310
lukman Avatar asked May 11 '16 13:05

lukman


2 Answers

Actually you also can use uiOutput, and it is very useful sometimes, because you can create a user interface from the server side. This is the solution:

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)
like image 187
Wlademir Ribeiro Prates Avatar answered Oct 29 '22 08:10

Wlademir Ribeiro Prates


Change your renderUI function in server to renderPlot while uiOutput to plotOutput in ui correspondingly.

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)
like image 39
Psidom Avatar answered Oct 29 '22 08:10

Psidom