【发布时间】:2021-11-05 20:03:28
【问题描述】:
下面 Shiny 中的代码效果很好。基本上,代码会根据我选择的日期/类别生成图表。请注意,我有两个 tabPanel,Graph1 和 Graph2。事实上,它为两个tabPanel 生成相同的图形。但是,我想对 Graph2 中的图表进行更改。首先plot函数在函数内部,但是对于Graph2的图形的生成,我想使用其他坐标,分别是:
plot(Weekdays ~ Reserv, xlim= c(0,80), ylim= c(0,50),
xaxs='i',data = datas,main = paste0(dmda, "-", CategoryChosse))
所以它与函数的情节不同,那么如何在我的代码中调整它?一个可能的解决方案是创建一个函数f2,只更改绘图规范,但代码会被复制。必须有更简单的方法来解决这个问题。你能帮帮我吗?
代码如下:
library(shiny)
library(shinythemes)
library(dplyr)
library(tidyverse)
library(lubridate)
Test <- structure(
list(date1= c("2021-06-28","2021-06-28"),
date2 = c("2021-07-01","2021-07-01"),
Category = c("FDE","ABC"),
Week= c("Friday","Monday"),
DR1 = c(14,11),
DR01 = c(14,12), DR02= c(14,12),DR03= c(19,15),
DR04 = c(15,14),DR05 = c(15,14),
DR06 = c(12,14)),
class = "data.frame", row.names = c(NA, -2L))
f1 <- function(df1, dmda, CategoryChosse) {
x<-df1 %>% select(starts_with("DR0"))
x<-cbind(df1, setNames(df1$DR1 - x, paste0(names(x), "_PV")))
PV<-select(x, date2,Week, Category, DR1, ends_with("PV"))
med<-PV %>%
group_by(Category,Week) %>%
summarize(across(ends_with("PV"), median))
SPV<-df1%>%
inner_join(med, by = c('Category', 'Week')) %>%
mutate(across(matches("^DR0\\d+$"), ~.x +
get(paste0(cur_column(), '_PV')),
.names = '{col}_{col}_PV')) %>%
select(date1:Category, DR01_DR01_PV:last_col())
SPV<-data.frame(SPV)
mat1 <- df1 %>%
filter(date2 == dmda, Category == CategoryChosse) %>%
select(starts_with("DR0")) %>%
pivot_longer(cols = everything()) %>%
arrange(desc(row_number())) %>%
mutate(cs = cumsum(value)) %>%
filter(cs == 0) %>%
pull(name)
(dropnames <- paste0(mat1,"_",mat1, "_PV"))
datas<-SPV %>%
filter(date2 == ymd(dmda)) %>%
group_by(Category) %>%
summarize(across(starts_with("DR0"), sum)) %>%
pivot_longer(cols= -Category, names_pattern = "DR0(.+)", values_to = "val") %>%
mutate(name = readr::parse_number(name))
colnames(datas)[-1]<-c("Days","Numbers")
if(as.Date(dmda) < min(as.Date(df1$date1))){
datas <- datas %>%
group_by(Category) %>%
slice(1:max(Days)+1) %>%
ungroup
}else{
datas <- datas %>%
group_by(Category) %>%
slice((as.Date(dmda) - min(as.Date(df1$date1) [
df1$Category == first(Category)])):max(Days)+1) %>%
ungroup
}
plot(Numbers ~ Days, xlim= c(0,45), ylim= c(0,30),
xaxs='i',data = datas,main = paste0(dmda, "-", CategoryChosse))
model <- nls(Numbers ~ b1*Days^2+b2,start = list(b1 = 0,b2 = 0),data = datas, algorithm = "port")
new.data <- data.frame(Days = with(datas, seq(min(Days),max(Days),len = 45)))
new.data <- rbind(0, new.data)
lines(new.data$Days,predict(model,newdata = new.data),lwd=2)
coef_val<-coef(model)[2]
points(0, coef_val, col="red",pch=19,cex = 2,xpd=TRUE)
}
ui <- fluidPage(
ui <- shiny::navbarPage(theme = shinytheme("flatly"), collapsible = TRUE,
br(),
tabPanel("",
sidebarLayout(
sidebarPanel(
uiOutput("date"),
uiOutput("mycode"),
),
mainPanel(
tabsetPanel(
tabPanel("Graph1", plotOutput("graph",width = "100%", height = "600")),
tabPanel("Graph2", plotOutput("graph2",width = "100%", height = "600"))
)
)
))
))
server <- function(input, output,session) {
data <- reactive(Test)
output$date <- renderUI({
req(data())
all_dates <- seq(as.Date('2021-01-01'), as.Date('2021-01-15'), by = "day")
disabled <- as.Date(setdiff(all_dates, as.Date(data()$date2)), origin = "1970-01-01")
dateInput(input = "date2",
label = h4("Data"),
min = min(data()$date2),
max = max(data()$date2),
value = min(data()$date2))
})
output$mycode <- renderUI({
req(input$date2)
df1 <- data()
df2 <- df1[as.Date(df1$date2) %in% input$date2,]
selectInput("code", label = h4("Category"),choices=unique(df2$Category))
})
output$graph <- renderPlot({
req(input$date2,input$code)
f1(data(),as.character(input$date2),as.character(input$code))
})
output$graph2 <- renderPlot({
req(input$date2,input$code)
f1(data(),as.character(input$date2),as.character(input$code))
})
}
shinyApp(ui = ui, server = server)
关于Reserv 和Weekdays:
....
datas<-SPV %>%
filter(date2 == ymd(dmda)) %>%
group_by(Category) %>%
summarize(across(starts_with("DR0"), sum)) %>%
pivot_longer(cols= -Category, names_pattern = "DR0(.+)", values_to = "val") %>%
mutate(name = readr::parse_number(name))
colnames(datas)[-1]<-c("Reserv","Weekdays")
if(as.Date(dmda) < min(as.Date(df1$date1))){
datas <- datas %>%
group_by(Category) %>%
slice(1:max(Reserv)+1) %>%
ungroup
}else{
datas <- datas %>%
group_by(Category) %>%
slice((as.Date(dmda) - min(as.Date(df1$date1) [
df1$Category == first(Category)])):max(Reserv)+1) %>%
ungroup
}
plot(Weekdays ~ Reserv, xlim= c(0,80), ylim= c(0,50),
xaxs='i',data = datas,main = paste0(dmda, "-", CategoryChosse))
model <- nls(Weekdays ~ b1*Reserv^2+b2,start = list(b1 = 0,b2 = 0),data = datas, algorithm = "port")
new.data <- data.frame(Reserv = with(datas, seq(min(Reserv),max(Reserv),len = 45)))
new.data <- rbind(0, new.data)
lines(new.data$Reserv,predict(model,newdata = new.data),lwd=2)
coef_val<-coef(model)[2]
points(0, coef_val, col="red",pch=19,cex = 2,xpd=TRUE)
}
【问题讨论】:
-
您还没有展示如何在您的函数中获取
Reserv和Weekdays。因此,很难猜测它应该是什么。 -
YBS,我插入了更改位置的问题。基本上不是
Days,而是Reserv,而不是Numbers,而是Weekdays。但是,我只希望对第二个图形生成进行此更改。 -
@YBS,不知道你有没有看到我之前的回答。在这种情况下,您认为您必须将两个图都插入服务器吗?也就是说,一个用于
output$graph,另一个用于output$graph2? -
是的,我想我已经回答了这个问题。
-
只是之前没讲过我是怎么得到
Reserv和Weekdays的,所以才回答。你的答案没有变化吗?我问这个是因为函数中有plot(Numbers ~ Days, xlim= limx, ylim= limy..,但Numbers和Days仅适用于图1。