2014-02-07 119 views
0

我想在Shiny中構建我的第一個應用程序。 我只想選擇一個變量的名稱,以便每次繪製一個變量,但每次我嘗試將變量傳遞給繪圖函數時,我都會收到一個xlim錯誤或「嘗試應用非函數」我的代碼在下面...非常感謝你!!R閃亮 - 簡單的反應問題

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') 
    }) 
}) 

回答

4

取而代之的是:

abc<-mtcars$input$variable

嘗試這樣做:

abc <- mtcars[, input$variable]

,因爲它開始mtcars,尋找其命名爲「輸入」(不存在)的列表元素你的版本將無法正常工作,然後找其命名爲「變量」列表元素(也沒有按不存在)。

第二個版本解析輸入$變量(「cyl」或「am」),然後從mtcars獲取該列。

+0

或'mtcars [[input $ variable]]' –