2017-09-20 12 views
1

빛나는 조건을 적용하는 방법으로 quote/substitute를 사용하려고합니다. 나는 따옴표/대체물이나 빛나는 것에 익숙하지 않아서 - 나는이 문제에 대해 올바른 방향으로 나아 가지 않을 것이 틀림 없다.빛나는 응용 프로그램 내에서 조건에 quote() 및 substitute() 사용

나는 내가 얻은 문제점을 설명하는 간단한 예제를 만들었습니다.

#Create test dataframe 
test<-data.frame(x=c(0:10), y=c(rep(1,5),rep(2,6)), z=c("A","A","A","B","B","B","C","C","C","C","C")) 

#example of what I would like to do outside shiny app 
test[test$x > 5,] 

#or using quote and eval 
test[eval(quote(test$x > 5)),] 

위의 코드는 모두 작동합니다. 하지만 지금은 (조건 선택하는 사용자를 허용) 반짝이는 응용 프로그램 내에서 적용하고 싶은 말은 할 수 있습니다 :

#create simple shiny app 
require(shiny) 


# Server 
server <- function(input, output) { 


    # subset of nodes 
    df <- reactive({ 

     #eliminate certain observations 
     x <- test[eval(input$condition),] 

    }) 

    output$table <- renderTable({ 
     df <- df() 


    }) 


} 



# UI 
ui <- fluidPage(

radioButtons("conditon", "Condition", choices = c("cond_1" = substitute(test$x > 5), "cond_2" = substitute(test$x<5))), 

tableOutput("table") 


) 


# Create app 
shinyApp(ui = ui, server = server) 

을하지만이 오류를 준다 "이름이어야합니다"선택 "의 모든 하위 목록") . 나는 이것을 어떻게 해석 할 지 모르겠다. 그래서 붙어있다. Shiny - All sub-lists in "choices" must be named?의 답변을 살펴 보았지만 도움이되지 않았습니다.

이 문제를 해결할 수있는 방법이나 더 나은 방법을 제안합니다.하지만 실제로는 복잡한 예제를 사용하면 문제가 발생하므로 미리 부분 집합을 만들 수는 없습니다.

답변

1

빠른 수정은 deparse으로 랩 한 다음 eval(parse을 사용할 수 있습니다. 입력이 표현식이어야하는 이유는 명확하지 않습니다. 이것은 단지 부분 집합에 대한 경우, 같은

library(shiny) 

-UI

ui <- fluidPage(
    radioButtons("conditon", "Condition", 
        choices = list(cond_1 = deparse(substitute(test$x > 5)), 
           cond_2 = deparse(substitute(test$x<5))), 
      selected = deparse(substitute(test$x > 5))), 

    tableOutput("table") 

) 

-server

server <- function(input, output) { 

    # subset of nodes 
    df <- reactive({ 

    #eliminate certain observations 
    test[eval(parse(text=input$conditon)),, drop = FALSE] 


    }) 

    output$table <- renderTable({ 
    df() 


    }) 


} 

-Create 응용 프로그램

shinyApp(ui = ui, server = server) 

을 수행하는 쉬운 방법이 있습니다 -output

enter image description here

enter image description here

+0

내가 정확하게 코드를 복사 한 내가 "잘못된 첨자 형 '목록'"오류가 발생 6,. 왜 그것이 당신을 위해 달리고, 나를 위해서는 안되는 지 아십니까? –

+0

@ Robert.S 해당 사항이 있으면'shiny_1.0.3'을 사용하고 있습니다. – akrun

+0

shiny_1.0.3도 사용하고 있습니다. –