R 修改光泽中的ggplot主题
我有一个K-Means集群应用程序。我希望用户使用selectInput选择ggplot2主题。用户将查看下拉列表,并为其情节选择一个主题 以下是我尝试过的:R 修改光泽中的ggplot主题,r,ggplot2,shiny,R,Ggplot2,Shiny,我有一个K-Means集群应用程序。我希望用户使用selectInput选择ggplot2主题。用户将查看下拉列表,并为其情节选择一个主题 以下是我尝试过的: ################################################################################################################### # Shiny App #########################################
###################################################################################################################
# Shiny App
###################################################################################################################
ui <- navbarPage("Clustering Demo",
tabPanel("K-Means", icon = icon("folder-open"),
sidebarLayout(
sidebarPanel(
sliderInput("num_centers",
label = h4("Select K (# of Clusters)"),
min = 2,
max = 10,
value = 2),
selectInput("theme", label = h4("Select theme for plot"),
choices = list("Light" = theme_light(),
"Minimal" = theme_minimal()))
),
mainPanel(
plotOutput("kmeans"))
)
)
)
server <- function(input, output, session) {
# K-Means Algorithm
k_centers <- reactive({kmeans(x = harvard_scaled, centers = input$num_centers)})
plot_theme <- reactive({input$theme})
output$kmeans <- renderPlot({
# Require number of centers
req(input$num_centers)
# K Means augmented dataset
harvard_cluster <- augment(k_centers(), harvard_processed)
# Static Plot
harvard_cluster %>%
janitor::clean_names() %>%
ggplot(aes(nevents, nplay_video, color = cluster)) +
geom_point() +
labs(x = "# of interactions with the course",
y = "# of play video events",
color = "Cluster") +
xlim(0, 52000) +
ylim(0, 12500) +
ggtitle(paste("K-Means Clustering of students where", "K =", input$num_centers)) +
plot_theme()
})
}
# Create Shiny app object
shinyApp(ui = ui, server = server)
示例数据集:已处理的哈佛大学
如果您单击主题选择的下拉列表,您可能会看到它与您期望的不一样。不能在UI层中存储非原子对象。在代码的其他地方定义一个主题列表并将其用作查找会更容易。比如说
themes <- list("Light" = theme_light(),
"Minimal" = theme_minimal())
ui <- navbarPage(...,
selectInput("theme", label = h4("Select theme for plot"), choices = names(themes)),
...)
server <- function(input, output, session) {
...
plot_theme <- reactive({themes[[input$theme]]})
...
}
如果您单击主题选择的下拉列表,您可能会看到它与您期望的不一样。不能在UI层中存储非原子对象。在代码的其他地方定义一个主题列表并将其用作查找会更容易。比如说
themes <- list("Light" = theme_light(),
"Minimal" = theme_minimal())
ui <- navbarPage(...,
selectInput("theme", label = h4("Select theme for plot"), choices = names(themes)),
...)
server <- function(input, output, session) {
...
plot_theme <- reactive({themes[[input$theme]]})
...
}
嗨,考虑使用一个带有开关的语句。b谢谢你让我知道@MrFlick。我刚刚添加了harvard_scaledAlmost,现在harvard_processed不存在。@MrFlick添加了harvard_processed这些示例数据无效,因为现在它们与绘图代码中使用的aes不匹配。确保在新的R会话中测试您的示例。但在这一点上,这真的不再重要了。如果您将我的解决方案放入代码中,它应该会起作用。但是下次有一个合适的HI要容易得多,考虑使用一个带有开关的语句。b谢谢你让我知道@MrFlick。我刚刚添加了harvard_scaledAlmost,现在harvard_processed不存在。@MrFlick添加了harvard_processed这些示例数据无效,因为现在它们与绘图代码中使用的aes不匹配。确保在新的R会话中测试您的示例。但在这一点上,这真的不再重要了。如果您将我的解决方案放入代码中,它应该会起作用。但下一次,要有一个合适的
themes <- list("Light" = theme_light(),
"Minimal" = theme_minimal())
ui <- navbarPage(...,
selectInput("theme", label = h4("Select theme for plot"), choices = names(themes)),
...)
server <- function(input, output, session) {
...
plot_theme <- reactive({themes[[input$theme]]})
...
}