Rshiny with a 2 way discrete variable : Why doesn't it work?

Viewed 65

I want to build an app that allows me to switch variables on a 2 discrete variables plot but i get an error when executing :

Error in parse(file, keep.source = FALSE, srcfile = src, encoding = enc) : C:\Users\HH Portable\Desktop\Dossier R\Jeux de données\Thai/ui.R:13:0: unexpected end of input 11:
mainPanel(plotOutput('plot1')) 12: ) ^ Warning: Error in sourceUTF8: Error sourcing C:\Users\HHPORT~1\AppData\Local\Temp\RtmpekudB9\fileab876054ad6 [No stack trace available]

library(shiny)

# ----- UI ----------------------------
pageWithSidebar(

  headerPanel('Representer les variables'),
  sidebarPanel(
    selectInput('xcol', 'X Variable', names(the)),
    selectInput('ycol', 'Y Variable', names(the)
       ),
  mainPanel(plotOutput('plot1'))
)

#----- Server.r ------------------------------
library(shiny)
library(ggplot2)
# ---- Server ----------------------------------
# ---- ggplot with options ------------------------
server=shinyServer(function(input, output, session) {
  output$plot1 <- renderPlot({
    ggplot(the, aes(x=input$xcol, ..count..*100/sum(..count..))) + 
      geom_bar(aes(fill = input$ycol), position = "fill") +
      geom_text(aes( label = scales::percent(..prop..),y= ..prop.. ), 
                stat= "count", vjust = -.5)
      })
})
1 Answers

Try using get() around your input$... as input$xcol and input$ycol are strings and aes() does not accept strings.

Next time when you post questions, please add a small reproducible example for others to help you resolve.

Hope this helps.

# ---- ggplot with options ------------------------
server=shinyServer(function(input, output, session) {
  output$plot1 <- renderPlot({
    ggplot(the, aes(x=get(input$xcol), ..count..*100/sum(..count..))) + 
      geom_bar(aes(fill = get(input$ycol)), position = "fill") +
      geom_text(aes(label = scales::percent(..prop..),y= ..prop.. ), 
                stat= "count", vjust = -.5)
      })
})
Related