R闪亮 - 简单的反应问题

时间:2014-02-07 19:18:06

标签: r shiny

我正在尝试在Shiny中构建我的第一个应用程序。 我只想选择一个变量的名称,以便一次绘制一个变量,但每次我试图将变量传递给我得到的情节函数或xlim错误或“尝试应用非函数”我的代码如下。 ..非常感谢你!

UI.R

library(shiny)

shinyUI(pageWithSidebar(

  headerPanel("Hello Shiny!"),

  sidebarPanel(
    selectInput(
      "variable","The value to change below",
      list("Cylinders"="cyl",
           "Transmission"="am"))
  ),

  mainPanel(
    plotOutput("linePlot")
  )
))

server.R

library(shiny)
library(datasets)
shinyServer(function(input, output) {

  abc<-reactive({
   abc<-mtcars$input$variable
  })


  output$linePlot<-renderPlot({
    plot(abc(),type='l')
  })
})

1 个答案:

答案 0 :(得分:4)

而不是:

abc<-mtcars$input$variable

尝试这样做:

abc <- mtcars[, input$variable]

你的版本不起作用,因为它以mtcars开头,寻找名为“input”的列表元素(不存在),然后寻找名为“variable”的列表元素(也不是不存在。。

第二个版本解析了输入$ variable(“cyl”或“am”)然后从mtcars获取该列。