gpt4 book ai didi

r - Shiny 的 react 性 ggplot 输出

转载 作者:行者123 更新时间:2023-12-03 01:37:25 26 4
gpt4 key购买 nike

我有以下代码,但尝试运行失败。我只是希望能够根据输入范围过滤图形输出。在此示例中,如果范围输入设置为 1-5,我希望在图表上看到 5 个点,同样范围输入 2-5 显示 4 个点。

##UI   
require(shiny)
shinyUI(fluidPage(
titlePanel(title=h4("Races", align="center")
),
sidebarPanel(
sliderInput("num", "Number:",
min = 0, max = 5,step=1,value=c(0,2)),
mainPanel(
plotOutput("plot2")))))

##server
library(dplyr)
library(ggplot2)
shinyServer(function(input,output){
num<-c(1,2,3,4,5)
let<-c("A","B","C","D","E")
date<-c("2015-5-1","2015-6-1","2015-7-1","2015-8-1","2015-9-1")
df<-data.frame(num,let,date)
dat<-reactive({
df %>% filter(num==input$num)})
output$plot2<-renderPlot({
ggplot(dat(),aes(x=date,y=num))+geom_point(colour='red')},height = 400,
width = 600)})

我想根据输入范围在图表上显示值的范围,但只显示 1 个值,而不是具有相应日期的值的范围。

提前致谢。

最佳答案

这是你想要的吗? 编辑:现在您将能够看到您指定的所有点

#rm(list = ls())
library(shiny)
library(ggplot2)

num<-c(1,2,3,4,5)
let<-c("A","B","C","D","E")
date<-c("2015-5-1","2015-6-1","2015-7-1","2015-8-1","2015-9-1")
df <- data.frame(num,let,date)

ui <- fluidPage(
titlePanel(title=h4("Races", align="center")),
sidebarPanel(
sliderInput("num", "Number:",min = 0, max = 5,step=1,value=c(1,2))),
mainPanel(plotOutput("plot2")))

server <- function(input,output){

dat <- reactive({
test <- df[df$num %in% seq(from=min(input$num),to=max(input$num),by=1),]
print(test)
test
})

output$plot2<-renderPlot({
ggplot(dat(),aes(x=date,y=num))+geom_point(colour='red')},height = 400,width = 600)}
shinyApp(ui, server)

enter image description here

关于r - Shiny 的 react 性 ggplot 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32969659/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com