闪亮的R直方图

Fah*_*bar 2 r histogram shiny

我正在使用以下代码来倾斜Shiny R,当我运行此代码时,它给了我这个错误:

警告:hist.default中的错误:默认值'x'必须为数字[没有可用的堆栈跟踪]

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)
Run Code Online (Sandbox Code Playgroud)

有什么帮助为什么会这样说?

Por*_*hop 5

尽量不要将所有内容放在一行中,因为它不会提高可读性,您可以根据需要使用Google的R样式指南。要回答您的问题,可以通过以下方式访问变量[[]]

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)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明