r - Shiny 的 R 直方图

标签 r shiny histogram

我正在使用下面的代码来学习 Shiny R ,当我运行此代码时,它给了我这个错误:

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)

任何帮助为什么会这么说?

最佳答案

尽量不要将所有内容都放入一行,因为这不会提高可读性,您可以使用 Google's R Style Guide如果您愿意。要回答您的问题,您可以通过 [[]] 访问变量,如下所示:

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

关于r - Shiny 的 R 直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52611963/

相关文章:

r - 在 R 中,如何获得一组数据的最佳拟合方程?

r - Shiny - 如何使用 ggiraph

r - 将 Shiny(非 ggplot)图输出为 PDF

r - R中两个变量的直方图

python - 如何循环直方图来获取图片的颜色?

r - 使用 R 填充圆圈中的 9 个区域

r - 如何使用 Shiny 的条件面板

r - 如何使actionButton仅在所有输入都以R Shiny 形式给出时才起作用?

r - 如何在 R 的直方图中突出显示观察的 bin

r - 有没有办法使用 spark_write_csv 在 sparklyr 中为 csv 文件设置名称?