사례 연구: R의 Shiny로 웹 애플리케이션 만들기
Dean Attali
Shiny Consultant
x 값이 바뀌면, x에 의존하는 모든 것이 다시 계산됩니다일반 R과 대비:
x <- 5
y <- x + 1
x <- 10
y의 값은? 6 또는 11?
렌더 함수 안의 input$<inputId>는 출력을 다시 렌더링하게 합니다
output$my_plot <- renderPlot({
plot(rnorm( input$num ))
})
output$my_plot은 input$num에 의존합니다
input$num이 바뀌면 ⇒
output$my_plot이 반응합니다
render*() 함수는 반응 컨텍스트입니다server <- function(input, output) {
print(input$num)
}
ERROR: Operation not allowed without an active reactive context.
observe({ ... })로 반응 변수를 읽습니다
server <- function(input, output) {
observe({
print( input$num )
})
}
디버깅에 유용, 반응 변수 추적
observe({
print( input$num1 )
print( input$num2 )
})
reactive({ ... })로 반응 변수를 만듭니다
잘못된 예:
server <- function(input, output) {
x <- input$num + 1
}
ERROR: Operation not allowed without an active reactive context.
올바른 예:
server <- function(input, output) {
x <- reactive({
input$num + 1
})
}
()를 추가server <- function(input, output){
x <- reactive({
input$num + 1
})
observe({
print( input$num )
print( x() )
})
}
사례 연구: R의 Shiny로 웹 애플리케이션 만들기