Hello; I have a web app with two tabs. my app appropriately plots data and renders text in one tab, but when i try the other tab, it plots only the data and throws an error when i render the text. below is my first effort at a reprex. thanks!
...``` r
here are my practice data:
dat
p q r s t
1 AK 70.1 0.000 280.7 0.000
2 AL 98.3 0.082 437.9 0.082
3 AR 88.9 0.088 421.4 0.088
4 AZ 57.5 0.000 268.7 0.000
5 CA 69.3 0.008 284.6 0.008
6 CO 67.0 0.000 248.6 0.000
7 CT 52.6 0.040 285.9 0.040
8 DC 71.8 0.000 393.8 0.000
9 DE 77.7 0.170 324.0 0.170
10 FL 69.5 0.140 290.9 0.140
library(reprex)
dput(dat, "tempDat")
#> Error in dput(dat, "tempDat"): object 'dat' not found
datAgain<-dget("tempDat")
print(datAgain)
#> NULL
here is my code:
library(shiny); library(tidyverse)
ui <- fluidPage(
titlePanel("Practice and Troubleshooting"),
sidebarLayout(
sidebarPanel(
h3("Slope"),
textOutput("slopeOut"),
h3("Intercept"),
textOutput("intOut")
),
mainPanel(
tabsetPanel(
tabPanel("Tab1", plotOutput("plot1", brush=brushOpts(id="brushP"))),
tabPanel("Tab2", plotOutput("plot2", brush=brushOpts(id="brushP")))
)
)))
server<-function(input,output){
# Create a reactive expression for tab1 input
model1 <- reactive({
brushed_data1<-brushedPoints(dat, input$brushP, xvar="r", yvar="q")
if(nrow(brushed_data1) < 2){
return(NULL)
}
lm(q~r, input$brushP, data = brushed_data1)
})
# Create a reactive expression for tab2 input
model2 <- reactive({
brushed_data2<-brushedPoints(dat, input$brushP, xvar="t", yvar="s")
if(nrow(brushed_data2) < 2){
return(NULL)
}
lm(s~t, data = brushed_data2)
})
# render slope text for model1
output$slopeOut <- renderText({
if(is.null(model1())){
"No Model Found"
} else {
model1()[[1]][2]
}
})
# render intercept text for model 1
output$intOut <- renderText({
if(is.null(model1())){
"No Model Found"
} else {
model1()[[1]][1]
}
})
# render slope text for model 2
output$slopeOut <- renderText({
if(is.null(model2())){
"No Model Found"
} else {
model2()[[1]][2]
}
})
# render intercept text for model 2
output$intOut <- renderText({
if(is.null(model2())){
"No Model Found"
} else {
model2()[[1]][1]
}
})
# render tab1 plot
output$plot1<-renderPlot({
plot(dat$r, dat$q, xlab="xlab", ylab="ylab", main="Practice Plot 1", cex=1.5, pch=16, bty="n")
if(!is.null(model1())){
abline(model1(), col="blue", lwd=2)
}
})
# render heart disease plot
output$plot2<-renderPlot({
plot(dat$t, dat$s, xlab="xlab", ylab="ylab", main="Practice Plot 2", cex=1.5, pch=16, bty="n")
if(!is.null(model2())){
abline(model2(), col="blue", lwd=2)
}
})
}
shinyApp(ui=ui,server=server)
<div style="width: 100% ; height: 400px ; text-align: center; box-sizing: border-box; -moz-box-sizing: border-box; -webkit-box-sizing: border-box;" class="muted well">Shiny applications not supported in static R Markdown documents</div>
<sup>Created on 2022-01-26 by the [reprex package](https://reprex.tidyverse.org) (v2.0.1)</sup>