【发布时间】:2018-02-22 08:22:22
【问题描述】:
我正在开发一个 ShinyApp,我必须打印出很多类似的情节。我认为让用户通过 Input 选择所需的绘图 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)
代码执行,应用按要求运行,但我仍然收到错误消息:
错误:
input$select必须解析为整数列位置, 不为空
我认为这是由于 select 命令同时针对变量名称和索引(通过 input$select)。
我想知道是否有更清洁或更优雅的方式来执行此操作。
【问题讨论】: