2

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.

2
  • It is unclear exacly what you are asking here. Could you please be specific? Commented Jan 5, 2015 at 0:04
  • 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. Commented Jan 6, 2015 at 14:01

1 Answer 1

3
  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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Perfect. re 1: Thank you for pointing out the function has to be parameter-less in reactive expressions re 2: I keep mixing up whether to use input variables or direct data variables. Thanks for pointing this out. All cleared up and code is running fine now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.