【发布时间】:2018-08-09 15:49:05
【问题描述】:
我正在尝试为 Shiny 构建一个 ggplot 输出。 我用gather() 将我的初始数据框收集成整洁的格式。
variable value
1 Region Africa
2 Region Europe
3 Region Europe
4 Region Latin America
13 Country Sudan
14 Country Russia
15 Country United Kingdom
16 Country Peru
17 Country Malaysia
35 Client Bank
36 Client Bank2
37 PG PG 1
38 PG PG 2
54 Status High
55 Status Signed
56 Status Low
现在我正在尝试创建一个简单的图表,其中变量的变化应该导致情节的变化。
# UI code
ui <- fluidPage(theme = shinytheme("united"),
sidebarLayout(
sidebarPanel(
selectInput("dataInput", "Choose to filter by:",
choices = c("Region",
"Country",
"Client",
"PG",
"Status"),
selected = "Choose to display by")
),
mainPanel(
# Output
tabsetPanel(type = "tabs",
# tabPanel("Map", leafletOutput(outputId = 'map', height = 850)),
tabPanel("Plot", plotOutput("plot", height = 850)))
)
)
)
我的服务器是这样的
server <- function(input, output) {
# 1. Select among columns
selectedData <- reactive({
data_pg_df1[!is.na(data_pg_df1$variable) & data_pg_df1$variable == input$dataInput, ]
})
# Output
output$plot <- renderPlot({
ggplot(data_pg_df1, aes(selectedData)) + geom_bar()
})
}
shinyApp(ui = ui, server = server)
我收到一个错误
不知道如何为 reactiveExpr/reactive 类型的对象自动选择比例。默认为连续。
警告:错误:列x 必须是一维原子向量或列表
试图解决,但失败了。
我的 ggplot 方法正确吗?或者我还需要在数据输入中添加“值”列。我不这么认为,因为我只想根据“变量”进行选择。 我错过了什么?
我想要的输出看起来像这样。我有 eval(),但想以一种整洁的方式来做,如上所述。
output$plot <- renderPlot({
eval(parse(text = paste0("ggplot(data_pg_df, aes(x=",input$dataInput,")) + geom_bar()" )))
})
【问题讨论】: