2
votes

I am using below code to lean Shiny R , when i run this code, it gives me this error:

Warning: Error in hist.default: 'x' must be numeric [No stack trace available]

library(shiny)

ui <- fluidPage(
  selectInput("Ind","Indipendent Variable",choices = names(mtcars)),
  selectInput('Dep','  Dependent Variable',choices = names(mtcars)),
  plotOutput("BoxPlot"),
  plotOutput('Hist'))

server <- function(input, output, session) {

  data1 <- reactive({input$Ind})
  data2 <- reactive({input$Dep})

  output$BoxPlot <- renderPlot({boxplot(get(data2()) ~ get(data1()) , data=mtcars)})

  output$Hist <- renderPlot({hist(get(data1())}) 

}

shinyApp(ui, server)

any help why would it say so ?

1

1 Answers

5
votes

Try not to put everything into 1 line as it doesnt improve readability, you can use Google's R Style Guide if you like.To answer your questions you can access the variable via [[]] like so:

library(shiny)

ui <- fluidPage(
  selectInput("Ind","Indipendent Variable",choices = names(mtcars)),
  selectInput('Dep','  Dependent Variable',choices = names(mtcars)),
  plotOutput("BoxPlot"),
  plotOutput('Hist')
)
server <- function(input, output, session) {

  data1 <- reactive({
    input$Ind
  })
  data2 <- reactive({
    input$Dep
  })

  output$BoxPlot <- renderPlot({
    boxplot(get(data2()) ~ get(data1()) , data=mtcars)
  })

  output$Hist <- renderPlot({
    req(data1())
    hist(mtcars[[data1()]])
  }) 

}

shinyApp(ui, server)

enter image description here