【发布时间】:2022-01-27 12:26:29
【问题描述】:
我试图让问题对新输入更具反应性,这是给出错误的地方
Pwh_Design=reactive((input$Pwh+0.2*(input$Pso-input$Pwh)))
Gtd=Pwh_Design-P_operating_point_formation/(0-D_operating_point)
这是在服务器中。
整个应用程序:
用户界面:
library(shiny)
# Define UI for application that draws a histogram
shinyUI(fluidPage(
# Application title
titlePanel("OTIS Gas Lift"),
fluidRow(column(width = 6,
numericInput(inputId = "Dmax",label = "Depth to mid perforation",value = 7500),
numericInput("Pwh","Pwh",100),
numericInput("Tres","Tres",182),
numericInput("Pso","P Casing at Surface",870),
numericInput("Psc","P Casing at Depth D1",1050),
numericInput("D1","D1",7000),
numericInput("P_across_valve","P_across_valve",100),
numericInput("GL","Load Grad",0.5),
numericInput("Pwf","Pwf",1760),
numericInput("RGOR","Required GOR",400)
),
column(width = 6,
numericInput("FGOR","Formation GOR",200),
numericInput("Twh","Twh",100),
numericInput("Pko","kill pressure at Surface",920),
numericInput("Pk","Kill Pressure at D2",1100),
numericInput("D2","D2",7000),
numericInput("T_inj","T_inj",100),
numericInput("q","Desired Production Rate STB/d",600),
numericInput("R","R",0.1534),
numericInput("Gf","Flowing Grad before inj",0.4)
)),
fluidRow(submitButton(text = "Apply Changes",width = "100%")),
splitLayout(tableOutput(outputId = "table"),plotOutput(outputId = "plot"))))
'''
这是服务器
library(shiny)
shinyServer(function(input, output) {
Dmax=reactiveValues() #to mid perforation ft
#one of those two next must be given
FGOR=reactiveValues() #scf/STB Formation GOR
Pwh=reactiveValues() #psig
Twh=reactiveValues() #degree fahrenheit
Tres=reactiveValues() #degree fahrenheit
#casing and kill gradients
Pso=reactiveValues() #psig
Pcs=reactiveValues() #psig at 7000ft
D1=reactiveValues() #ft
Pko=reactiveValues() #psig
Pk=reactiveValues() #psig at 7000ft
D2=reactiveValues() #ft
P_across_valve=reactiveValues() #psig
T_inj=reactiveValues() #degree fahrenheit
GL=reactiveValues() #load gradient (kill fluid grad)
q=reactiveValues()
Pwf=reactiveValues() #psi or BHP
RGOR=reactiveValues() #scf/STB required GOR
R=reactiveValues()
Gf=reactiveValues() #flowing gradient before injection default value
library(ggplot2)
library(dplyr)
#plot basics
plot<-reactive(ggplot+
coord_cartesian(xlim = c(0,input$Pwf+500))+
scale_y_continuous(trans = "reverse")+
scale_x_continuous(position = "top"))
#Flowing Gradient
#slope Gf
#point (pwf,Dmax)
#get p1 at depth 4000ft
p1=reactive(input$Pwf-input$Gf*(input$Dmax-4000))
flowing_gradient<-reactive(data.frame(D=c(4000,input$Dmax),P=c(p1,input$Pwf)))
plot0<-plot+
geom_point(data = flowing_gradient,aes(x=P,y=D))+
geom_line(data = flowing_gradient,aes(x=P,y=D))
#casing gradient
casing_gradient<-reactive(data.frame(D=c(0,input$D1),P=c(input$Pso,input$Pcs)))
plot1<-plot0+geom_point(data=casing_gradient,aes(x=P,y=D))+
geom_line(data=casing_gradient,aes(x=P,y=D))
Gc=reactive((input$Pso-input$Pcs)/(0-input$D1))
#killing gradient
killing_gradient<-reactive(data.frame(D=c(0,input$D2),P=c(input$Pko,input$Pk)))
plot2<-plot1+geom_point(data=killing_gradient,aes(x=P,y=D))+
geom_line(data=killing_gradient,aes(x=P,y=D))
Gk=reactive((input$Pko-input$Pk)/(0-input$D2))
#balance point
d1=reactive((input$Pwf-input$Pso-input$Gf*input$Dmax)/(Gc-input$Gf))
d_balance_point=d1
#operating_point
d1=reactive((input$Pso-input$Pwf+input$Gf*input$Dmax-input$P_across_valve)/(input$Gf-Gc))
D_operating_point=d1
P_operating_point_casing=reactive(input$Pso+D_operating_point*Gc)
P_operating_point_formation=reactive(input$Pwf-input$Gf*(input$Dmax-D_operating_point))
plot3<-plot2+geom_point(aes(x=P_operating_point_formation,y=D_operating_point))
#Tubing grad
tubing_grad<-reactive(data.frame(D=c(0,D_operating_point),P=c(input$Pwh,P_operating_point_formation)))
plot4<-plot3+geom_point(data = tubing_grad,aes(x=P,y=D))+
geom_line(data = tubing_grad,aes(x=P,y=D))
#Tubing_design_grad
Pwh_Design=reactive((input$Pwh+0.2*(input$Pso-input$Pwh)))
Gtd=Pwh_Design-P_operating_point_formation/(0-D_operating_point) #Tubing design grad
tubing_design_grad<-data.frame(D=c(0,D_operating_point),P=c(Pwh_Design,P_operating_point_formation))
plot5<-plot4+geom_point(data = tubing_design_grad,aes(x=P,y=D))+
geom_line(data = tubing_design_grad,aes(x=P,y=D))
#first valve
D_valve1=reactive((input$Pwh-input$Pko)/(Gk-input$GL))
PK_valve1=reactive(D_valve1*Gk+input$Pko)
PTD_valve1=Gtd*D_valve1+Pwh_Design
df<-reactive(data.frame(D=c(0,D_valve1),P=c(input$Pwh,PK_valve1)))
#ploting
plot6<-plot5+geom_point(data = df, aes(x=P,y=D))+
geom_line(data = df, aes(x=P,y=D))
df<-data.frame(D=c(D_valve1,D_valve1),P=c(PTD_valve1,PK_valve1))
plot7<-plot6+geom_point(data = df, aes(x=P,y=D))+
geom_line(data = df, aes(x=P,y=D))
plot<-plot7
Final_table<-data.frame(depth=D_valve1,
Pc=PK_valve1,
Pt=PTD_valve1)
dvalve=D_valve1
PTDvalve=PTD_valve1
Pcvalve=PK_valve1
D_valve=D_valve1
PTD_valve=PTD_valve1
while (dvalve<D_operating_point) {
dvalve=reactive((PTDvalve-input$Pso-dvalve*input$GL)/(Gc-input$GL))
Pcvalve=reactive(input$Pso+Gc*dvalve)
if(dvalve<D_operating_point){
df<-data.frame(D=c(D_valve,dvalve),P=c(PTD_valve,Pcvalve))
plot<-plot+geom_point(data = df, aes(x=P,y=D))+
geom_line(data = df, aes(x=P,y=D))
PTDvalve=Gtd*dvalve+Pwh_Design
df<-data.frame(D=c(dvalve,dvalve),P=c(PTDvalve,Pcvalve))
plot<-plot+geom_point(data = df, aes(x=P,y=D))+
geom_line(data = df, aes(x=P,y=D))
valve<-c(dvalve,Pcvalve,PTDvalve)
Final_table<-rbind(Final_table,valve)
D_valve=dvalve
PTD_valve=PTDvalve
}}
output$plot<-renderPlot(plot)
valve<-c(D_operating_point,P_operating_point_casing,P_operating_point_formation)
Final_table<-rbind(Final_table,valve)
GTemp=reactive((input$Tres-input$Twh)/input$Dmax)
CT_data<-data.frame(Temp=c(61:300),Ct=c(0.998,0.996,0.994,0.991,0.989,0.987,0.985,0.983,0.981,0.979,0.977,0.975,0.973,0.971,0.969,0.967,0.965,0.963,0.961,0.959,0.957,0.955,0.953,0.951,0.949,0.947,0.945,0.943,0.941,0.939,0.938,0.936,0.934,0.932,0.93,0.928,0.926,0.924,0.923,0.921,0.919,0.917,0.915,0.914,0.912,0.91,0.908,0.906,0.905,0.903,0.901,0.899,0.898,0.896,0.894,0.893,0.891,0.889,0.887,0.886,0.884,0.882,0.881,0.879,0.877,0.876,0.874,0.872,0.871,0.869,0.686,0.866,0.864,0.863,0.861,0.86,0.858,0.856,0.855,0.853,0.852,0.85,0.849,0.847,0.845,0.844,0.842,0.841,0.839,0.838,0.836,0.835,0.833,0.832,0.83,0.829,0.827,0.826,0.825,0.823,0.822,0.82,0.819,0.817,0.816,0.814,0.813,0.812,0.81,0.809,0.807,0.806,0.805,0.803,0.802,0.8,0.799,0.798,0.796,0.795,0.794,0.792,0.791,0.79,0.788,0.787,0.786,0.784,0.783,0.782,0.78,0.779,0.778,0.776,0.775,0.774,0.772,0.771,0.77,0.769,0.767,0.766,0.765,0.764,0.762,0.761,0.76,0.759,0.757,0.756,0.755,0.754,0.752,0.751,0.75,0.749,0.748,0.746,0.745,0.744,0.743,0.742,0.74,0.739,0.738,0.737,0.736,0.735,0.733,0.732,0.731,0.73,0.729,0.728,0.727,0.725,0.724,0.723,0.722,0.721,0.72,0.719,0.718,0.717,0.715,0.714,0.713,0.712,0.711,0.71,0.709,0.708,0.707,0.706,0.705,0.704,0.702,0.701,0.7,0.699,0.698,0.697,0.696,0.695,0.694,0.693,0.692,0.691,0.69,0.689,0.688,0.687,0.686,0.685,0.684,0.683,0.682,0.681,0.68,0.679,0.678,0.677,0.676,0.675,0.674,0.673,0.672,0.671,0.67,0.699,0.668,0.667,0.666,0.665,0.664,0.663,0.662,0.662,0.661,0.66))
Final_table<-Final_table %>% mutate(reactive(Pbt=input$Pc*(1-input$R)+Pt*input$R),
reactive(TEMP=input$Twh+GTemp*depth),
Temp=ceiling(TEMP))
Final_table<-merge(x=Final_table,y=CT_data,by = "Temp",all.x = T)
Final_table<-Final_table%>%select(depth,Pc,Pt,Pbt,TEMP,Ct)
Final_table<-Final_table %>% mutate(Pb=Pbt*Ct,
Pvo=reactive(Pb/(1-input$R)))
output$table<-tableOutput(Final_table)})
我尝试使用 () 中包含的反应式赋值创建任何变量,但它给出了这个错误4
“警告:错误:在没有活动反应上下文的情况下不允许操作。
- 您试图做一些只能在反应式消费者内部完成的事情。 58: 错误:如果没有活动的反应上下文,则不允许操作。
- 您试图做一些只能在反应式消费者内部完成的事情。”
我很确定我在调用它们时只对反应性分配的变量这样做了
编辑
在我尝试解决它给出的问题后:
警告:data.frame 中的错误:参数暗示不同的行数:1、0 1:运行应用程序
每当我按下操作按钮时 新服务器
library(shiny)
library(ggplot2)
library(dplyr)
shinyServer(function(input, output) {
plot<-reactiveVal(NULL)
Final_table<-reactiveVal(data.frame())
observeEvent(input$submit,{
plot(ggplot()+
coord_cartesian(xlim = c(0,input$Pwf+500))+
scale_y_continuous(trans = "reverse")+
scale_x_continuous(position = "top"))
#Flowing Gradient
#slope Gf
#point (pwf,Dmax)
#get p1 at depth 4000ft
p1=input$Pwf-input$Gf*(input$Dmax-4000)
flowing_gradient<-data.frame(D=c(4000,input$Dmax),P=c(p1,input$Pwf))
plot(plot()+
geom_point(data = flowing_gradient,aes(x=P,y=D))+
geom_line(data = flowing_gradient,aes(x=P,y=D)))
#casing gradient
casing_gradient<-data.frame(D=c(0,input$D1),P=c(input$Pso,input$Pcs))
plot(plot()+geom_point(data=casing_gradient,aes(x=P,y=D))+
geom_line(data=casing_gradient,aes(x=P,y=D)))
Gc=(input$Pso-input$Pcs)/(0-input$D1)
#killing gradient
killing_gradient<-data.frame(D=c(0,input$D2),P=c(input$Pko,input$Pk))
plot(plot()+geom_point(data=killing_gradient,aes(x=P,y=D))+
geom_line(data=killing_gradient,aes(x=P,y=D)))
Gk=(input$Pko-input$Pk)/(0-input$D2)
#balance point
d1=(input$Pwf-input$Pso-input$Gf*input$Dmax)/(Gc-input$Gf)
d_balance_point=d1
#operating_point
d1=(input$Pso-input$Pwf+input$Gf*input$Dmax-input$P_across_valve)/(input$Gf-Gc)
D_operating_point=d1
P_operating_point_casing=input$Pso+D_operating_point*Gc
P_operating_point_formation=input$Pwf-input$Gf*(input$Dmax-D_operating_point)
plot(plot()+geom_point(aes(x=P_operating_point_formation,y=D_operating_point)))
#Tubing grad
tubing_grad<-data.frame(D=c(0,D_operating_point),P=c(input$Pwh,P_operating_point_formation))
plot(plot()+geom_point(data = tubing_grad,aes(x=P,y=D))+
geom_line(data = tubing_grad,aes(x=P,y=D)))
#Tubing_design_grad
Pwh_design=input$Pwh+0.2*(input$Pso-input$Pwh)
Gtd=(Pwh_design-P_operating_point_formation)/(0-D_operating_point) #Tubing design grad
tubing_design_grad<-data.frame(D=c(0,D_operating_point),P=c(Pwh_design,P_operating_point_formation))
plot(plot()+geom_point(data = tubing_design_grad,aes(x=P,y=D))+
geom_line(data = tubing_design_grad,aes(x=P,y=D)))
#first valve
D_valve1=(input$Pwh-input$Pko)/(Gk-input$GL)
PK_valve1=D_valve1*Gk+input$Pko
PTD_valve1=Gtd*D_valve1+Pwh_design
df<-data.frame(D=c(0,D_valve1),P=c(input$Pwh,PK_valve1))
#ploting
plot(plot()+geom_point(data = df, aes(x=P,y=D))+
geom_line(data = df, aes(x=P,y=D)))
df<-data.frame(D=c(D_valve1,D_valve1),P=c(PTD_valve1,PK_valve1))
plot(plot()+geom_point(data = df, aes(x=P,y=D))+
geom_line(data = df, aes(x=P,y=D)))
Final_table(data.frame(depth=D_valve1,
Pc=PK_valve1,
Pt=PTD_valve1))
dvalve=D_valve1
PTDvalve=PTD_valve1
Pcvalve=PK_valve1
D_valve=D_valve1
PTD_valve=PTD_valve1
while (dvalve<D_operating_point) {
dvalve=(PTDvalve-input$Pso-dvalve*input$GL)/(Gc-input$GL)
Pcvalve=input$Pso+Gc*dvalve
if(dvalve<D_operating_point){
df<-data.frame(D=c(D_valve,dvalve),P=c(PTD_valve,Pcvalve))
plot(plot()+geom_point(data = df, aes(x=P,y=D))+
geom_line(data = df, aes(x=P,y=D)))
PTDvalve=Gtd*dvalve+Pwh_design
df<-data.frame(D=c(dvalve,dvalve),P=c(PTDvalve,Pcvalve))
plot(plot()+geom_point(data = df, aes(x=P,y=D))+
geom_line(data = df, aes(x=P,y=D)))
valve<-c(dvalve,Pcvalve,PTDvalve)
Final_table(rbind(Final_table(),valve))
D_valve=dvalve
PTD_valve=PTDvalve
}}
valve<-c(D_operating_point,P_operating_point_casing,P_operating_point_formation)
Final_table(rbind(Final_table(),valve))
GTemp=(input$Tres-input$Twh)/input$Dmax
CT_data<-data.frame(Temp=c(61:300),Ct=c(0.998,0.996,0.994,0.991,0.989,0.987,0.985,0.983,0.981,0.979,0.977,0.975,0.973,0.971,0.969,0.967,0.965,0.963,0.961,0.959,0.957,0.955,0.953,0.951,0.949,0.947,0.945,0.943,0.941,0.939,0.938,0.936,0.934,0.932,0.93,0.928,0.926,0.924,0.923,0.921,0.919,0.917,0.915,0.914,0.912,0.91,0.908,0.906,0.905,0.903,0.901,0.899,0.898,0.896,0.894,0.893,0.891,0.889,0.887,0.886,0.884,0.882,0.881,0.879,0.877,0.876,0.874,0.872,0.871,0.869,0.686,0.866,0.864,0.863,0.861,0.86,0.858,0.856,0.855,0.853,0.852,0.85,0.849,0.847,0.845,0.844,0.842,0.841,0.839,0.838,0.836,0.835,0.833,0.832,0.83,0.829,0.827,0.826,0.825,0.823,0.822,0.82,0.819,0.817,0.816,0.814,0.813,0.812,0.81,0.809,0.807,0.806,0.805,0.803,0.802,0.8,0.799,0.798,0.796,0.795,0.794,0.792,0.791,0.79,0.788,0.787,0.786,0.784,0.783,0.782,0.78,0.779,0.778,0.776,0.775,0.774,0.772,0.771,0.77,0.769,0.767,0.766,0.765,0.764,0.762,0.761,0.76,0.759,0.757,0.756,0.755,0.754,0.752,0.751,0.75,0.749,0.748,0.746,0.745,0.744,0.743,0.742,0.74,0.739,0.738,0.737,0.736,0.735,0.733,0.732,0.731,0.73,0.729,0.728,0.727,0.725,0.724,0.723,0.722,0.721,0.72,0.719,0.718,0.717,0.715,0.714,0.713,0.712,0.711,0.71,0.709,0.708,0.707,0.706,0.705,0.704,0.702,0.701,0.7,0.699,0.698,0.697,0.696,0.695,0.694,0.693,0.692,0.691,0.69,0.689,0.688,0.687,0.686,0.685,0.684,0.683,0.682,0.681,0.68,0.679,0.678,0.677,0.676,0.675,0.674,0.673,0.672,0.671,0.67,0.699,0.668,0.667,0.666,0.665,0.664,0.663,0.662,0.662,0.661,0.66))
Final_table(Final_table() %>% mutate(Pbt=Pc*(1-input$R)+Pt*input$R,
TEMP=input$Twh+GTemp*depth,
Temp=ceiling(TEMP)))
Final_table(merge(x=Final_table(),y=CT_data,by = "Temp",all.x = T))
Final_table(Final_table()%>%select(depth,Pc,Pt,Pbt,TEMP,Ct))
Final_table(Final_table() %>% mutate(Pb=Pbt*Ct,
Pvo=Pb/(1-input$R)))
})
output$plot<-renderPlot({
isolate(plot())
})
output$table<-renderTable({
isolate(Final_table())
})
})
【问题讨论】:
-
如果你定义了一个反应值,比如
df <- reactive({iris})。为了能够使用它,您需要在反应式上下文中像函数一样调用它。例如observe({ df() })。你把这个错误拖到了整个应用程序中。此外,您可以通过将表达式传递给observe或reactive来大大减少reactive的数量。例如,x <- reactive({local_var <- input$someid print(local_var))}) -
@jpdugo17 很抱歉给您带来不便,但这是我第一次在我的应用程序中使用反应性,看来我不太理解它,您能否指出一个参考来解释您明确指出的概念这将是非常有帮助的提前谢谢
-
是的,当然,我添加了一个答案来创建第一个情节,您可以为每个情节遵循该模式,注意
reactiveVal的使用,它与reactive的概念相似。有关反应性的更多信息,您可以阅读Reactivity - an Overview。 -
非常感谢您的帮助我将通过您提到的源代码和代码来完全理解它再次感谢您的帮助