플롯에 색상 추가하기: 색상 입력
colourpicker 패키지는 colourInput() 함수를 통해 색상 입력 위젯을 제공합니다. 색상 입력은 shiny 패키지의 일부는 아니지만, 다른 입력과 동일한 방식으로 동작합니다.
색상 입력에는 살펴볼 수 있는 다양한 인수가 있지만, 여기서는 기본 인수인 inputId, label, value만 사용하겠습니다. value 인수에는 초기값으로 사용할 색상을 지정합니다. 색상은 여러 형식으로 지정할 수 있지만, 가장 간단한 방법은 "red"나 "yellow"처럼 영어색 이름을 사용하는 것입니다.
이 연습은 강의의 일부입니다
사례 연구: R의 Shiny로 웹 애플리케이션 만들기
연습 안내
이전 연습 문제의 Shiny 앱 코드가 제공됩니다. 라디오 버튼으로 색상을 선택하던 부분을 색상 입력으로 바꾸는 것이 과제입니다. 구체적으로는 다음을 수행하세요.
colourpicker패키지를 불러오세요.- 색상 선택용 라디오 버튼을 만드는 UI 함수를 찾아 색상 입력으로 교체하세요(12행).
- 색상 입력의 ID는 "color", 라벨은 "Point color", 기본 색상은 "blue"로 설정하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Load the colourpicker package
___
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
textInput("title", "Title", "GDP vs life exp"),
numericInput("size", "Point size", 1, 1),
checkboxInput("fit", "Add line of best fit", FALSE),
# Replace the radio buttons with a color input
radioButtons("color", "Point color",
choices = c("blue", "red", "green", "black")),
selectInput("continents", "Continents",
choices = levels(gapminder$continent),
multiple = TRUE,
selected = "Europe"),
sliderInput("years", "Years",
min(gapminder$year), max(gapminder$year),
value = c(1977, 2002))
),
mainPanel(
plotOutput("plot")
)
)
)
# Define the server logic
server <- function(input, output) {
output$plot <- renderPlot({
data <- subset(gapminder,
continent %in% input$continents &
year >= input$years[1] & year <= input$years[2])
p <- ggplot(data, aes(gdpPercap, lifeExp)) +
geom_point(size = input$size, col = input$color) +
scale_x_log10() +
ggtitle(input$title)
if (input$fit) {
p <- p + geom_smooth(method = "lm")
}
p
})
}
shinyApp(ui = ui, server = server)