0

Hello i have this quite simple script i can't make to work... it give me a histogram but not for the variable i asked for ! For example when i choose the variable AGE it doesn't show the the histogram of the AGE on the population but the total population : it s very likely it selects all the column of dat instead of dat$AGE but i can't find the solution

server.R :

library(shiny)
library(ggplot2)
library(plotly)


shinyServer(function(input, output, session) {

  dat <- reactive({
    req(input$df)
    dfile <- read.csv(input$df$datapath)
    updateSelectInput(session,inputId = 'select1', label = 'Variable',
                      choices = colnames(dfile))
    return(dfile)
    })



  output$data <- renderUI({
   if (!is.null(input$df$datapath)){
    selectInput('select1', choices = names(dat()), label = h3('Variable'))
   }
  })

  output$plot <- renderPlotly( ggplotly(
      ggplot(data=dat(),aes(x=input$select1))  + geom_histogram(stat = "count")))
  })

ui.R

library(shiny)
library(ggplot2)
library(plotly)

shinyUI(fluidPage(

    sidebarLayout(
      sidebarPanel(
        fileInput("df","CSV file :"),
        uiOutput("data")
),

      mainPanel(
        h2(plotlyOutput("plot"))
      )
 )
 ))

If you can help, i searched for this all the afternoon :3

1 Answer 1

1

In your call to ggplot, change the aes mapping to

aes_string("x" = input$select1)

since input$select1 is a string. In other words, your original code does aes(x = "AGE") rather than aes(x = AGE), which is what you want.

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

1 Comment

Thank you very much

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.