0
votes

I'm trying to build an R shiny app to display histograms from different data sources following the example on the r shiny homepage (https://shiny.rstudio.com/tutorial/written-tutorial/lesson5/).

I'm running into a problem when I add "color" and "legend" to my switch function. I tried adding input$range[1], input$range[2] to my hist() statement but that didn't help. Can anyone provide insights as to why I'm getting the error I am at the bottom of this script?

If you add color=color and legend = legend, you'll see what it's supposed to look like (of course without color or legend).

THis throws the following error:

Listening on http://127.0.0.1:6511 Warning: Error in &&: invalid 'x' type in 'x && y' Stack trace (innermost first): 106: plot.histogram 105: plot 104: hist.default 103: hist 102: renderPlot [#24] 92: 81: plotObj 80: origRenderFunc 79: output$distPlot 4: 3: do.call 2: print.shiny.appobj 1:

N<- 500
M<-31

Data1 <-matrix( rnorm(N*M,mean=30,sd=3.5), N, M)
Data2 <-matrix( rnorm(N*M,mean=23,sd=3), N, M)
Data3 <- matrix( rnorm(N*M,mean=30,sd=4), N, M) 

# Define UI  ----
ui <- fluidPage(
  titlePanel(code(strong("Tool"), style = "color:black")),
  sidebarLayout(
    sidebarPanel(
      strong("Tools:"),
      selectInput("Test", 
                  label = "Choose a measure to display",
                  choices = c("Test1", 
                              "Test2",
                              "Test3"
                  ),
                  selected = "Test1"),

      sliderInput(inputId="slider1", label = "Bins",
                  min = 1, max = 300, value = 200)),
    mainPanel(
      code(strong("Study Readout")),
      plotOutput(outputId = "distPlot")
    )))

# Define server logic ----
server <- function(input, output) {
  output$distPlot <- renderPlot({
    slider1 <- seq(floor(min(x)), ceiling(max(x)), length.out = input$slider1 + 1)
    x    <- switch(input$Test, 
                   "Test1" = Data1,
                   "Test2" = Data2, 
                   "Test3" = Data3)
    color <- switch(input$Test, 
                    "Test1" = "darkgreen",
                    "Test2" = "darkorange",
                    "Test3" = "darkviolet")

    legend <- switch(input$Test, 
                     "Test1" = "Test1",
                     "Test2" = "Test2",
                     "Test3" = "Test3")


    #hist = qplot(x, breaks = slider1, fill=..count.., geom="histogram") +
    #  scale_fill_gradient(low="orangered2",high="yellow",guide = 'none')+
    #  theme_bw()+labs(x="Population Range of Executive Functioning") + labs(y="")


    hist(x, color, legend, breaks = slider1)

  })
}


# Run that shit ----
shinyApp(ui = ui, server = server)
1
Can you fix the issues in this code snippet? I can't run it without errors. I also noticed in output$distPlot that x, Matrix1, etc are all used without being defined anywhere.greg L
@gregL, thanks for your comment. I've fixed the code and added some data to run it with. right now it breaks. If you add color=color and legend = legend, you'll see what it's supposed to look like (of course without color or legend).Isaac

1 Answers

0
votes

The problem turned out to be the labels for the hist. I was following the example from the website but didn't think about how hist labels color and legend. Here is the fix:

hist(x, col=color, main=legend, breaks = slider1)