2
votes

Took very basic shiny scripts and were able to play around the generic data sets. When I tried to put in my own and run ggplot, I've come across several errors. Most recent is what appears in my main panel of shiny app and console in Rstudio ... "ggplot2 doesn't know how to deal with data of class reactive" ... In general, I stripped down my ggplot to the most basic elements and still not sure from where ggplot is calling data while in shiny. I am guessing the reactive function, but honestly, I am lost.

Below are scripts

_____ui.R________

shinyUI(pageWithSidebar(
  headerPanel('Mock Risk Scorecard'),
  sidebarPanel(
    selectInput('xcol', 'X Axis', names(RandomRiskCard)),
    selectInput('ycol', 'Y Axis', names(RandomRiskCard),
                selected=names(RandomRiskCard)[[2]]),
                 min = 1, max = 9),
  mainPanel(
    plotOutput('plot1')
  )
)
)

_____server.R____

palette(c("#E41A1C", "#377EB8"))
shinyServer(function(input, output, session) { 
  # Combine the selected variables into a new data frame
  selectedData <- reactive({
    RandomRiskCard[, c(RandomRiskCard$xcol, RandomRiskCard$ycol)]
  }) 
  output$plot1 <- renderPlot({
    p <- ggplot(selectedData, aes(x = RandomRiskCard$xcol, y = RandomRiskCard$ycol))
  p <- p + geom_point()
  })
})

I also loaded up my data and Run Shiny in different script windows as follow

install.packages("shiny")
library(shiny)
library(ggplot2)
runApp("U:/App-1")

as well as

RandomRiskCard = read.csv("U:/App-1/RandomRiskCard.csv")

I am eventually hoping to incorporate factor function and annotate with colors like I had done with my original ggplot. If it wasn't already obvious I am a newbie at this, but shiny has me completely twisted.

1
It is unclear exacly what you are asking here. Could you please be specific?dcarson
All cleared up now. I was trying to fix the server to run my ggplot plot without error signals. Changing the reactive expression and variables fixed that.RobertRayNYC

1 Answers

3
votes
  1. Reactive expressions should be called in the same way as parameter-less functions, with following parentheses: ggplot(selectedData(),...

  2. xcol and ycol should be obtained via input:

    p <- ggplot(selectedData(), aes(x = input$xcol, y = input$ycol)) in output$plot, and

    RandomRiskCard[, c(input$xcol, input$ycol)] in selectedData