-1

I want to create a RShiny plot. When I launch the app, the sidebar is visible but there is no plot. What's causing the error?

Here's my code:

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput('xcol', 'X Variable', dimnames(dat)[[2]], 
                  selected = rownames(dat)),
      selectInput('ycol', 'Y Variable', dimnames(dat)[[2]],
                  selected = colnames(dat)),
      selectInput('color', 'Point Color', c("red", "orange", "yellow", "green", "blue", "violet", "purple"))
    ), 
    mainPanel(
      plotOutput('plot1')
    )
  )
)


server <- function(input, output) {
  
  selectedData <- reactive({
    dat[, c(input$xcol, input$ycol)]
  })
  output$plot<-renderPlot({
    plot(dat$axis(side=1,at=c(1:24),labels=dimnames(dat)[[2]],cex.axis=0.4,las=2), dat$axis(side=2))
  }
  )
}

shinyApp(ui = ui, server = server)

snapshot of data

dput(dat[1:2,1:2])
structure(list(cdc15_10 = c(-0.16, NA), cdc15_30 = c(0.09, NA
)), row.names = c("YAL001C", "YAL002W"), class = "data.frame")
meloi
  • 1
  • 2

2 Answers2

3

There are a few issues with your code:

  1. You're not using the reactive selectedData object anywhere.
  2. You've got plotOutput('plot1') but then use output$plot. This might just be a simple typo.
  3. The general base R plotting call looks odd. I'd start from a basic plot(selectedData()) and then work from there in terms of adding axes labels etc.

Here is a minimal example

library(shiny)
dat <- data.frame(a = rnorm(10), b = rnorm(10), c = rnorm(10))

ui <- fluidPage(
    sidebarLayout(
        sidebarPanel(
            selectInput(
                'xcol', 
                'X Variable', 
                dimnames(dat)[[2]], 
                selected = rownames(dat)),
            selectInput(
                'ycol', 
                'Y Variable', 
                dimnames(dat)[[2]],
                selected = colnames(dat)),
            selectInput(
                'color', 
                'Point Color', 
                c("red", "orange", "yellow", "green", "blue", "violet", "purple"))
        ), 
        mainPanel(plotOutput('plot1'))
    )
)


server <- function(input, output) {
    
    selectedData <- reactive({
        dat[, c(input$xcol, input$ycol)]
    })
    
    output$plot1 <- renderPlot({
        plot(selectedData())
    })
}

shinyApp(ui = ui, server = server)

which produces

enter image description here

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
0

You can try the below code which selects data with two columns from the dropdown and plots one column against the other with the selected color.

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput('xcol', 'X Variable', dimnames(dat)[[2]], 
                  selected = rownames(dat)),
      selectInput('ycol', 'Y Variable', dimnames(dat)[[2]],
                  selected = colnames(dat)),
      selectInput('color', 'Point Color', c("red", "orange", "yellow", "green", "blue", "violet", "purple"))
    ), 
    mainPanel(
      plotOutput('plot')
    )
  )
)


server <- function(input, output) {
  
  selectedData <- reactive({
    dat[, c(input$xcol, input$ycol)]
  })
  output$plot<-renderPlot({
    dat <- selectedData()
    plot(dat[[1]], dat[[2]], col = input$color)
  }
  )
}

shinyApp(ui = ui, server = server)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213