用户选择主题 R shiny ggplot
user chooses theme R shiny ggplot
我希望用户能够为 ggplot 选择主题。我对非标准评估有点不稳定,但我猜这就是我所缺少的。任何建议表示赞赏。
library(shiny)
library(ggplot2)
themes <- ls("package:ggplot2", pattern = "^theme_")
themes <- themes[themes != "theme_get" &
themes != "theme_set" &
themes != "theme_replace" &
themes != "theme_test" &
themes != "theme_update"]
themes <- paste0(themes, "()")
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Choose bin width:",
min = 5,
max = 15,
value = 10
),
selectInput(
"theme",
"choose a theme",
themes,
selected = "theme_minimal()",
multiple = FALSE
)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$distPlot <- renderPlot({
ggplot(faithful, aes(waiting)) +
geom_histogram(binwidth = input$bins) +
labs(title = input$theme) +
input$theme
})
}
# Run the application
shinyApp(ui = ui, server = server)
#>
#> Listening on http://127.0.0.1:6760
#> Warning: Error in ggplot_add: Can't add `input$theme` to a ggplot object.
由 reprex package (v2.0.1)
于 2022-05-05 创建
您可以使用 get()
从函数名称中获取函数:
server <- function(input, output) {
output$distPlot <- renderPlot({
ggplot(faithful, aes(waiting)) +
geom_histogram(binwidth = input$bins) +
labs(title = input$theme) +
get(gsub("\(\)","",input$theme))()
})
如果在主题列表中省略括号,则可以避免gsub
我认为这可以满足您的需求:
output$distPlot <- renderPlot({
ggplot(faithful, aes(waiting)) +
geom_histogram(binwidth = input$bins) +
labs(title = input$theme) +
match.fun(stringr::str_sub(input$theme, 1, -3))()
})
解释:
match.fun()
按名称查找函数。您需要从 input$theme
的值中删除 ()
。因此调用 stringr::substr()
。所以match.fun(stringr::str_sub(input$theme, 1, -3))
给大家参考需要的主题功能。最后的()
调用它。
我希望用户能够为 ggplot 选择主题。我对非标准评估有点不稳定,但我猜这就是我所缺少的。任何建议表示赞赏。
library(shiny)
library(ggplot2)
themes <- ls("package:ggplot2", pattern = "^theme_")
themes <- themes[themes != "theme_get" &
themes != "theme_set" &
themes != "theme_replace" &
themes != "theme_test" &
themes != "theme_update"]
themes <- paste0(themes, "()")
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Choose bin width:",
min = 5,
max = 15,
value = 10
),
selectInput(
"theme",
"choose a theme",
themes,
selected = "theme_minimal()",
multiple = FALSE
)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$distPlot <- renderPlot({
ggplot(faithful, aes(waiting)) +
geom_histogram(binwidth = input$bins) +
labs(title = input$theme) +
input$theme
})
}
# Run the application
shinyApp(ui = ui, server = server)
#>
#> Listening on http://127.0.0.1:6760
#> Warning: Error in ggplot_add: Can't add `input$theme` to a ggplot object.
由 reprex package (v2.0.1)
于 2022-05-05 创建您可以使用 get()
从函数名称中获取函数:
server <- function(input, output) {
output$distPlot <- renderPlot({
ggplot(faithful, aes(waiting)) +
geom_histogram(binwidth = input$bins) +
labs(title = input$theme) +
get(gsub("\(\)","",input$theme))()
})
如果在主题列表中省略括号,则可以避免gsub
我认为这可以满足您的需求:
output$distPlot <- renderPlot({
ggplot(faithful, aes(waiting)) +
geom_histogram(binwidth = input$bins) +
labs(title = input$theme) +
match.fun(stringr::str_sub(input$theme, 1, -3))()
})
解释:
match.fun()
按名称查找函数。您需要从 input$theme
的值中删除 ()
。因此调用 stringr::substr()
。所以match.fun(stringr::str_sub(input$theme, 1, -3))
给大家参考需要的主题功能。最后的()
调用它。