2016-06-06 2 views
0

나는 15에서 20.25 시간 사이의 가능한 가장 긴 시간 간격으로 데이터 포인트를 표시하는 R로 첫 Shiny 앱을 만들려고 노력했습니다. dat은 24 시간 동안 모두 원래 데이터 테이블이며 초기화되었습니다. dat5를 새로운 시간 간격으로 슬라이더의 2 개 입력으로 새로운 데이터 테이블로 만들려고합니다. 내 코드는 아래에 있으며 실제 응용 프로그램에서이 오류가 나타납니다. 슬라이더가 좋은 것처럼 보이지만 "closure '형식의 개체는 하위 집합이 아닙니다. 어떤 도움이라도 대단히 감사하겠습니다. 이것은 내가 아래의 코드와 콘솔에지고있어 오류입니다 :첫 번째 반짝이는 앱에서의 오류

오류

Warning in if (!is.na(attribValue)) { 
    the condition has length > 1 and only the first element will be used 
Warning in charToRaw(enc2utf8(text)) : 
    argument should be a character vector of length 1 
all but the first element will be ignored 
Warning in if (!is.na(attribValue)) { : 
    the condition has length > 1 and only the first element will be used 
Warning in charToRaw(enc2utf8(text)) : 
    argument should be a character vector of length 1 
all but the first element will be ignored 
Warning: Error in $: object of type 'closure' is not subsettable 
Stack trace (innermost first): 
    68: output$plot1 
    1: runApp 

Warning messages: 
1: In .HTMLsearch(query) : Unrecognized search field: title 
2: In .HTMLsearch(query) : Unrecognized search field: keyword 
3: In .HTMLsearch(query) : Unrecognized search field: alias 
4: In .HTMLsearch(query) : Unrecognized search field: title 
5: In .HTMLsearch(query) : Unrecognized search field: keyword 
6: In .HTMLsearch(query) : Unrecognized search field: alias 

코드 나는 간단한 작업 예를 구성

ui <- fluidPage(

    sidebarLayout(
    sidebarPanel(sliderInput(Tc, "Time Interval", min = 15, max =20.25, 
         2, value = c(15,20.25))), 
    mainPanel(
     plotOutput("plot1") 
    ) 
) 
) 

server <- function(input, output, session) { 

    dat5 <- reactive({ 
    dat5 <- copy(dat[Tc %between%c(input[1],input[2])]) 
    }) 

    output$plot1 <- renderPlot({ 

    ggplot(dat2, aes(x = dat5$Tc, y = dat5$LastPrice)) + 
     geom_line(size = 2, alpha = 0.5) + 
     geom_point(size = 3) + 
     xlab("Time") + 
     ylab("Price")+ 
     theme(text = element_text(size = 18), 
     legend.position = 'bottom') 
    }) 

} 
shinyApp(ui = ui, server = server) 
+0

내가 한 후에는 오류 : reactvalues ​​객체의 단일 브래킷 색인 생성이 허용되지 않습니다. – CheddaShredda

+0

왜 dat5를 dat5()라고해야하는지 모르겠습니다. 함수가 아닌 data.table이거나 뭔가 빠졌습니까? – CheddaShredda

+0

이제 "ggplot2는 반응 형 클래스의 데이터를 처리하는 방법을 모릅니다."즉 반응 함수 내에서 무언가를 고쳐야한다는 것을 의미하지만, – CheddaShredda

답변

1

:

library(data.table) 
library(ggplot2) 
library(shiny) 

set.seed(707) 

dat <- data.table(
    Tc = seq(0, 23.75, by = 0.25), 
    LastPrice = exp(rnorm(24 * 4)) 
) 


ui <- fluidPage(
    sidebarLayout(
    sidebarPanel(sliderInput('Tc', "Time Interval", min = 15, max =20.25, 
          2, value = c(15,20.25))), 
    mainPanel(
     plotOutput("plot1") 
    ) 
) 
) 


server <- function(input, output, session) { 
    dat5 <- reactive({ 
    dat5 <- copy(dat[Tc %between% c(input$Tc[1],input$Tc[2])]) 
    }) 

    output$plot1 <- renderPlot({ 
    ggplot(dat5(), 
      aes(x = Tc, 
       y = LastPrice)) + 
     geom_line(size = 2, 
       alpha = 0.5) + 
     geom_point(size = 3) + 
     xlab("Time") + 
     ylab("Price")+ 
     theme(text = element_text(size = 18), 
      legend.position = 'bottom') 
    }) 
} 


shinyApp(ui = ui, server = server) 

필요한 수정 :

  • 첫 번째 인수 sliderInput에 대한 요구는 문자 값이 될 수 있습니다.
  • 이 값은 해당 입력에 대한 ID이며 슬라이더 값을 가져 오는 데 사용해야합니다. 그래서 c(input[1], input[2]) 필요 c(input$Tc[1], input$TC[2])

또 다른 변화로 변경할 수 : 때문에 평소 ggplot2 구문의

  • , 당신은 미학 인수에 열 이름 앞에 dat5()$을 지정할 필요가 없습니다.