R:如何将滑块和单选按钮链接到ggplot
我对shiny很陌生,读过其他类似的帖子,但没有成功,包括: 我想绘制健康结果(从单选按钮中选择)与自付成本(OOP),并根据滑块绘制年份 这是我到目前为止的代码,未显示绘图 用户界面:R:如何将滑块和单选按钮链接到ggplot,r,ggplot2,shiny,R,Ggplot2,Shiny,我对shiny很陌生,读过其他类似的帖子,但没有成功,包括: 我想绘制健康结果(从单选按钮中选择)与自付成本(OOP),并根据滑块绘制年份 这是我到目前为止的代码,未显示绘图 用户界面: 与往常一样,使用dput和预期输出发布可再现数据是有帮助的。不过,这里是我的解决方案,如果需要更新,请告诉我 library(shiny) library(ggplot2) library(dplyr) #No data changed to NA Input = ("Country Year OOP
与往常一样,使用
dput
和预期输出发布可再现数据是有帮助的。不过,这里是我的解决方案,如果需要更新,请告诉我
library(shiny)
library(ggplot2)
library(dplyr)
#No data changed to NA
Input = ("Country Year OOP Mortality Probability
1 Afghanistan 2000 NA 934.3 34.2
2 Afghanistan 2005 79 947.7 33.6
3 Afghanistan 2010 79 919.6 32.2
4 Afghanistan 2015 78.4 898.0 31.0
5 Albania 2000 64.6 710.3 20.0
6 Albania 2005 52.5 688.9 19.7")
data = read.table(textConnection(Input),header=TRUE)
ui <- shinyUI(fluidPage(
titlePanel(title=h3("Out of pocket expenditure on health")),
mainPanel(
h5(textOutput("subheading")),
plotOutput("view")),
fluidRow(
column(5,
radioButtons("outcome", label=h4("Select Health Outcomes"),
choices=c("Mortality rate", "Premature death risk (age 30-70)"), selected="Mortality rate"),
checkboxInput("smooth", "Add trend line")
),
column(5,
sliderInput("years", label=h4("Year"),
min=min(data$Year), max=max(data$Year), value=(min(data$Year)), step=1, sep="", animate=TRUE)
)
)
))
server <- shinyServer(function(input, output, session){
formulaText <- reactive({
paste("Health outcome:",input$outcome)
})
output$subheading <- renderText({formulaText()})
datareact <- reactive({
print(input$years) #to check variable before passing
print(input$outcome)
data <- data %>%
#change input$outcome from renderPlot to reactive
filter(Year >= input$years)
if (input$outcome == "Mortality rate") {data$outcome <- data$Mortality} else {
data$outcome <- data$Probability #Please note this is not the best solution for large data set
}
data
})
observe(print(datareact())) #to check which data you get
output$view <- renderPlot({
p <- ggplot(datareact(), aes(x=OOP, y=outcome, color=Country))+
geom_point()
if(input$smooth)
p <- p + geom_smooth()
p #Enforce renderPlot to return p
})
})
shinyApp(ui,server)
库(闪亮)
图书馆(GG2)
图书馆(dplyr)
#没有数据更改为NA
输入=(“国家年OOP死亡率概率
1阿富汗2000 NA 934.3 34.2
2阿富汗2005 79 947.7 33.6
3阿富汗2010 79919.6 32.2
4阿富汗2015 78.4 898.0 31.0
5阿尔巴尼亚2000 64.6 710.3 20.0
6阿尔巴尼亚2005 52.5688.9 19.7“
data=read.table(textConnection(输入),header=TRUE)
ui我猜,input$output
正在传入一个字符串<代码>aes
与此无关。请尝试aes\u string
。感谢您的回复@A.Suliman-我已经在原始帖子中添加了我的数据。运行代码后,我得到“警告:过滤器中的错误\u impl:评估错误:找不到对象‘结果’。167:“您能提供进一步的想法吗?”?感谢您的数据不包括结果变量,您计划如何绘制ggplot(datareact(),aes(x=OOP,y=input$outcome))
我希望根据单选按钮输入的y变量(选择死亡率或概率),绘制所有国家的“死亡率”v“OOP”和“概率”v“OOP”。还可以使用滑动条选择年份。我希望这有意义?非常感谢你的努力和回应。当我尝试运行时,不幸的是,我得到了:警告:UseMethod中的错误:没有适用于“filter_”的方法应用于类“function”的对象。也许是“反应性”功能中的某些东西?我非常感谢您的建议,感谢您的建议-本次演讲的目的是单独讨论这些结果,所以我认为最好不要一起展示。也许会有一个有趣的情节。再次感谢你,阿克
library(dplyr)
library(ggplot2)
shinyServer(
function(input, output){
formulaText <- reactive({
paste("Health outcome:",input$outcome)
})
output$subheading <- renderText({formulaText()})
datareact <- reactive({
data %>%
filter(Year == input$years) %>%
select(Country, OOP, Mortality, Probability)
})
output$view <- renderPlot({
p <- ggplot(datareact(), aes(x=OOP, y=input$outcome))+
geom_point(aes(fill=Country))
if(input$smooth)
p <- p + geom_smooth()
})
})
Country Year OOP Mortality Probability
1 Afghanistan 2000 No data 934.3 34.2
2 Afghanistan 2005 79 947.7 33.6
3 Afghanistan 2010 79 919.6 32.2
4 Afghanistan 2015 78.4 898.0 31.0
5 Albania 2000 64.6 710.3 20.0
6 Albania 2005 52.5 688.9 19.7
library(shiny)
library(ggplot2)
library(dplyr)
#No data changed to NA
Input = ("Country Year OOP Mortality Probability
1 Afghanistan 2000 NA 934.3 34.2
2 Afghanistan 2005 79 947.7 33.6
3 Afghanistan 2010 79 919.6 32.2
4 Afghanistan 2015 78.4 898.0 31.0
5 Albania 2000 64.6 710.3 20.0
6 Albania 2005 52.5 688.9 19.7")
data = read.table(textConnection(Input),header=TRUE)
ui <- shinyUI(fluidPage(
titlePanel(title=h3("Out of pocket expenditure on health")),
mainPanel(
h5(textOutput("subheading")),
plotOutput("view")),
fluidRow(
column(5,
radioButtons("outcome", label=h4("Select Health Outcomes"),
choices=c("Mortality rate", "Premature death risk (age 30-70)"), selected="Mortality rate"),
checkboxInput("smooth", "Add trend line")
),
column(5,
sliderInput("years", label=h4("Year"),
min=min(data$Year), max=max(data$Year), value=(min(data$Year)), step=1, sep="", animate=TRUE)
)
)
))
server <- shinyServer(function(input, output, session){
formulaText <- reactive({
paste("Health outcome:",input$outcome)
})
output$subheading <- renderText({formulaText()})
datareact <- reactive({
print(input$years) #to check variable before passing
print(input$outcome)
data <- data %>%
#change input$outcome from renderPlot to reactive
filter(Year >= input$years)
if (input$outcome == "Mortality rate") {data$outcome <- data$Mortality} else {
data$outcome <- data$Probability #Please note this is not the best solution for large data set
}
data
})
observe(print(datareact())) #to check which data you get
output$view <- renderPlot({
p <- ggplot(datareact(), aes(x=OOP, y=outcome, color=Country))+
geom_point()
if(input$smooth)
p <- p + geom_smooth()
p #Enforce renderPlot to return p
})
})
shinyApp(ui,server)