我遇到了我工作中的一些問題。爲了解決這個問題,我以鑽石數據爲例。在閃亮的應用程序中的selectinput小部件中包含選項NULL
我做了一個函數sca.plot。如果參數color.by = NULL,則繪製散點圖並將所有點顏色設爲紅色。如果color.by是一些因子變量,則使用變量繪製散點圖和顏色點。
我的問題是,當我使交互式散點圖閃亮。如何在selectinput小部件中包含NULL選項,以便我可以選擇這些點是否由某個變量着色?
如果我在selectinput中的選項中包含NULL,我得到了錯誤。想不到......
非常感謝。
下面是我的代碼:
library(ggplot2)
sca.plot <- function (color.by=NULL) {
if (is.null(color.by)) {
diamonds %>% ggplot(aes(x, y))+
geom_point (color='red')
} else {
color.by = sym(color.by)
diamonds %>% ggplot(aes_(~x, ~y, color=color.by))+
geom_point()
}
}
sca.plot(color.by='clarity')
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput('colorby', label = h5(strong('Color By')),
choices = list('clarity', 'cut'),
selected = NULL)
),
mainPanel(
plotOutput('plot')
)
)
)
server <- function(input, output) {
output$plot <- renderPlot ({
sca.plot(color.by=input$colorby)
})
}
runApp(shinyApp(ui, server))
[This](https://github.com/rstudio/shiny/issues/559)鏈接應該可以幫到你。 – SBista
@SBista這個鏈接回答我的問題。謝謝! – zesla