我正在尝试在 Shiny 中实现这 3 行代码,这是我想要的输出。
install.packages("ggalt")
library(ggalt)
health <- read.csv("https://raw.githubusercontent.com/selva86/datasets/master/health.csv")
health$Area <- factor(health$Area, levels=as.character(health$Area)) # did something for graph
ggplot(health, aes(x=pct_2013, xend=pct_2014, y=Area))+
geom_dumbbell()
在 Shiny 中,我希望用户能够选择棒棒糖图的起点和终点。我还希望用户能够选择一个将显示在 y 轴上的分类变量(在许多分类变量中 - 尽管目前示例数据只有一个分类变量)。下面是我到目前为止所做的工作,从字面上将所有变量替换为输入变量(所以我认为它应该起作用,但它不起作用..)
ui<-pageWithSidebar(
headerPanel('Lollipop Chart'),
sidebarPanel(
selectInput('xcol', 'X Variable', names(health), selected=names(health)[1]),
selectInput('val1', 'Starting Value', names(health), selected=names(health)[3]),
selectInput('val2', 'Ending Value', names(health), selected=names(health)[2]) ),
mainPanel(
plotOutput('plot1')
)
)
server<-function(input, output, session) {
health < read.csv("https://raw.githubusercontent.com/selva86/datasets/master/health.csv")
health$Area <- factor(health$Area, levels=as.character(health$Area)) # did something for graph
selectedData <- reactive({
health[, c(input$xcol, input$val1, input$val2)]
})
output$plot1 <- renderPlot({
ggplot(selectedData(), aes(x=input$val1, xend=input$val2, y=input$xcol))+
geom_dumbbell()
})
}
shinyApp(ui, server)
谁能帮助我为什么没有得到所需的输出? :(