在 Shiny 中选择绘图的维度
selecting dimensions of plot in Shiny
我正在开发一个 ShinyApp,我必须在其中打印出很多类似的图。我认为让用户通过输入选择所需的绘图的 y 维度将是一个优雅的解决方案,因此可以避免出现大量相似的绘图输出。
我通过以下代码得到了这个 运行:
library(tidyverse)
library(shiny)
set.seed(1)
name <- c("a", "b", "c")
v1 <- sample(1:100, 3)
v2 <- sample(1:100, 3)
df <- data_frame(name, v1, v2)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
uiOutput("selector")
),
mainPanel(
plotOutput("plot")
)
)
)
server <- function(input, output){
output$selector <- renderUI({
selectizeInput("select", "select column:", choices = colnames(df), selected = "v1")
})
data <- reactive({
df %>%
select(name, y = input$select)
})
output$plot <- renderPlot({
ggplot(data())+
geom_col(aes(x = name, y = y))+
geom_hline(yintercept = mean(data()$y, na.rm = TRUE), color = "red", size = 1)
})
}
shinyApp(ui = ui, server = server)
代码执行且应用按预期运行,但我仍然收到错误消息:
Error in : input$select
must resolve to integer column positions,
not NULL
我认为这是由于 select 命令同时针对变量名和索引(通过输入 $select)。
我想知道是否有更简洁或更优雅的方法来执行此操作。
您可以为此使用 req()
,请参阅该函数的文档 here。下面显示了一个工作示例。
library(ggplot2)
library(dplyr)
library(shiny)
set.seed(1)
name <- c("a", "b", "c")
v1 <- sample(1:100, 3)
v2 <- sample(1:100, 3)
df <- data_frame(name, v1, v2)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
uiOutput("selector")
),
mainPanel(
plotOutput("plot")
)
)
)
server <- function(input, output){
output$selector <- renderUI({
selectizeInput("select", "select column:", choices = colnames(df), selected = "v1")
})
data <- reactive({
req(input$select)
df %>%
select(name, y = input$select)
})
output$plot <- renderPlot({
ggplot(data())+
geom_col(aes(x = name, y = y))+
geom_hline(yintercept = mean(data()$y, na.rm = TRUE), color = "red", size = 1)
})
}
shinyApp(ui = ui, server = server)
或者,您可以简单地在 UI 中创建输入,但这可能无法实现,具体取决于您的应用程序:
library(ggplot2)
library(dplyr)
library(shiny)
set.seed(1)
name <- c("a", "b", "c")
v1 <- sample(1:100, 3)
v2 <- sample(1:100, 3)
df <- data_frame(name, v1, v2)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectizeInput("select", "select column:", choices = colnames(df), selected = "v1")
),
mainPanel(
plotOutput("plot")
)
)
)
server <- function(input, output){
data <- reactive({
df %>%
select(name, y = input$select)
})
output$plot <- renderPlot({
ggplot(data())+
geom_col(aes(x = name, y = y))+
geom_hline(yintercept = mean(data()$y, na.rm = TRUE), color = "red", size = 1)
})
}
shinyApp(ui = ui, server = server)
希望对您有所帮助!
我正在开发一个 ShinyApp,我必须在其中打印出很多类似的图。我认为让用户通过输入选择所需的绘图的 y 维度将是一个优雅的解决方案,因此可以避免出现大量相似的绘图输出。 我通过以下代码得到了这个 运行:
library(tidyverse)
library(shiny)
set.seed(1)
name <- c("a", "b", "c")
v1 <- sample(1:100, 3)
v2 <- sample(1:100, 3)
df <- data_frame(name, v1, v2)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
uiOutput("selector")
),
mainPanel(
plotOutput("plot")
)
)
)
server <- function(input, output){
output$selector <- renderUI({
selectizeInput("select", "select column:", choices = colnames(df), selected = "v1")
})
data <- reactive({
df %>%
select(name, y = input$select)
})
output$plot <- renderPlot({
ggplot(data())+
geom_col(aes(x = name, y = y))+
geom_hline(yintercept = mean(data()$y, na.rm = TRUE), color = "red", size = 1)
})
}
shinyApp(ui = ui, server = server)
代码执行且应用按预期运行,但我仍然收到错误消息:
Error in :
input$select
must resolve to integer column positions, not NULL
我认为这是由于 select 命令同时针对变量名和索引(通过输入 $select)。
我想知道是否有更简洁或更优雅的方法来执行此操作。
您可以为此使用 req()
,请参阅该函数的文档 here。下面显示了一个工作示例。
library(ggplot2)
library(dplyr)
library(shiny)
set.seed(1)
name <- c("a", "b", "c")
v1 <- sample(1:100, 3)
v2 <- sample(1:100, 3)
df <- data_frame(name, v1, v2)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
uiOutput("selector")
),
mainPanel(
plotOutput("plot")
)
)
)
server <- function(input, output){
output$selector <- renderUI({
selectizeInput("select", "select column:", choices = colnames(df), selected = "v1")
})
data <- reactive({
req(input$select)
df %>%
select(name, y = input$select)
})
output$plot <- renderPlot({
ggplot(data())+
geom_col(aes(x = name, y = y))+
geom_hline(yintercept = mean(data()$y, na.rm = TRUE), color = "red", size = 1)
})
}
shinyApp(ui = ui, server = server)
或者,您可以简单地在 UI 中创建输入,但这可能无法实现,具体取决于您的应用程序:
library(ggplot2)
library(dplyr)
library(shiny)
set.seed(1)
name <- c("a", "b", "c")
v1 <- sample(1:100, 3)
v2 <- sample(1:100, 3)
df <- data_frame(name, v1, v2)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectizeInput("select", "select column:", choices = colnames(df), selected = "v1")
),
mainPanel(
plotOutput("plot")
)
)
)
server <- function(input, output){
data <- reactive({
df %>%
select(name, y = input$select)
})
output$plot <- renderPlot({
ggplot(data())+
geom_col(aes(x = name, y = y))+
geom_hline(yintercept = mean(data()$y, na.rm = TRUE), color = "red", size = 1)
})
}
shinyApp(ui = ui, server = server)
希望对您有所帮助!