사례 연구: R의 Shiny로 웹 애플리케이션 만들기
Dean Attali
Shiny Consultant
data <- gapminder
data <- subset(
data,
lifeExp >= input$life[1] & lifeExp <= input$life[2]
)
if (input$continent != "All") {
data <- subset(
data,
continent == input$continent
)
}
renderTable()renderPlot()downloadHandler()reactive() 변수를 사용하십시오output$my_table <- renderTable({
data <- gapminder
data <- subset(
data,
lifeExp >= input$life[1] & lifeExp <= input$life[2]
)
})
my_data <- reactive({data <- gapminder data <- subset( data, lifeExp >= input$life[1] & lifeExp <= input$life[2] )})output$my_table <- renderTable({ my_data() })
output$table <- renderTable({
fit_model(input$num)
})
output$plot <- renderPlot({
ggplot(
fit_model(input$num), ...)
})
fit_model()에 5초 소요fit_model() 2회 호출 = 10초x <- reactive({ fit_model(input$num) })output$table <- renderTable({ x() })output$plot <- renderPlot({ ggplot(x(), ...) })
x()는 두 번 호출되어도, x 내부 코드는 한 번만 실행됩니다fit_model() 1회 호출 = 5초지연 변수 = 필요할 때까지 계산하지 않음
x <- reactive({
fit_model(input$num)
})
output$download <- downloadHandler(
filename = "x.csv",
content = function(file) {
write.csv(x(), file)
}
)
x()는 input$num이 바뀔 때마다가 아니라 다운로드 요청 시에만 실행됩니다
사례 연구: R의 Shiny로 웹 애플리케이션 만들기