Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
光泽R直方图_R_Shiny_Histogram - Fatal编程技术网

光泽R直方图

光泽R直方图,r,shiny,histogram,R,Shiny,Histogram,我正在使用下面的代码来运行R,当我运行此代码时,它会给我以下错误: 警告:hist.default中出错:“x”必须是数字 [没有可用的堆栈跟踪] 库(闪亮) ui尽量不要将所有内容都放在一行中,因为这不会提高可读性,如果您愿意,可以使用。要回答您的问题,可以通过[[]]访问变量,如下所示: library(shiny) ui <- fluidPage( selectInput("Ind","Indipendent Variable",choices = names(mtcars))

我正在使用下面的代码来运行R,当我运行此代码时,它会给我以下错误:

警告:hist.default中出错:“x”必须是数字 [没有可用的堆栈跟踪]

库(闪亮)

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

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)
库(闪亮)

嗨,猪排,很好的建议。它起作用了,但很快就会问,[]和[[]]之间有什么区别双括号用于访问更复杂对象(如列表)中的变量,您可以在这个漂亮的博客和手册中阅读更多内容,为什么我们需要req函数?我还注意到我们用它来做柱状图,而不是方框图,为什么呢??非常感谢您的帮助使用
req()
只是我的一个习惯,我认为这是一个很好的实践,除非您正在动态生成content@Fahadakbar可能还值得指出的是,它之所以不起作用,首先是因为
get(data1())
被转换为,例如,
get(“mpg”)
,它返回整个
mtcars
数据帧。因此,您最初是在数据帧上调用
hist
,而不是在特定变量上调用。这个答案通过直接从
mtcars
数据集调用变量来解决这个问题。
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)