简体   繁体   中英

Shiny R Histogram

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 ?

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)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM