James Abraham James Abraham - 3 months ago 13
R Question

Rshiny App displaying wrong script

Please forgive me if this question is not worded efficiently; this is my first time posting a question.
I am a writing a Shiny App, and it has been going pretty well until a few days ago. I have encountered a problem where, whenever I run the app, an output is displayed that does not match my app at all. Rather, the output appears to be related to the last script a ran (whatever that may be.
Here is my code:

`

library(ROracle)
library(shiny)
library(shinyBS)

shinyUi= fluidPage(


tabsetPanel(

tabPanel("By Day",

titlePanel("Daily Web Counts, by Quarter-Hour"),
sidebarLayout(
sidebarPanel(
#selectInput(inputId = "month", label = "Choose Which Months to Include",
# choices = c("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"), selected = "01"),

selectInput(inputId = "year1", label = "Choose Year",
choices = c("2011", "2012", "2013", "2014", "2015", "2016"), selected = "2016"),

dateInput(inputId ="date1", "Date:"),
bsButton(inputId= "go1", label = "Refresh")),

mainPanel(


plotOutput("recordsPlot")


# tableOutput("daytable"),


))),

tabPanel("By Month",

sidebarLayout(
sidebarPanel(
actionButton(inputId = "go2", label = "Refresh"),

selectInput(inputId = "month2", label = "Choose Month", choices = c("01", "02", "03", "04",
"05", "06", "07", "08",
"09", "10", "11", "12"))
),

mainPanel(

textOutput("ByMonthTotal"),
tableOutput("ByMonthCount")

))),

tabPanel("By Year",




titlePanel("Counts by Year and Month"),

sidebarLayout(
sidebarPanel(

#column(2,
selectInput(inputId = "month3", label = "Choose Month", choices = c("01", "02", "03", "04",
"05", "06", "07", "08",
"09", "10", "11", "12")),

# column(2,
numericInput(inputId = "year3", label = "Type a Year", value = 2016),

#column(2,
actionButton(inputId ="go3", label = "Refresh")),

#,


#fluidRow(
# column(2,

mainPanel(



plotOutput("monthgraph"),



tableOutput("MonthsTable"))
)


)
))

shinyApp(ui = ui, server = server)


`

and here is my server:

library(ROracle)
library(shiny)
library(plyr)
library(sqldf)
library(ggplot2)
library(shinyBS)

shinyServer(function(input, output, session) {


drv <- dbDriver("Oracle")
con <- dbConnect(drv, username="", password="", dbname= "")

qry = reactive({paste0('select MONTH, YEAR, ENTRY_DATE_TIME from where YEAR = ',input$year1,'')})



output$recordsPlot <- renderPlot({



data2 = eventReactive(input$go1, {dbGetQuery(con, qry())})



ntry_day = reactive({as.Date(trunc(data2()$ENTRY_DATE_TIME, "days"), "%Y%m%d")})

entry_day = ntry_day()


a <-reactive({as.POSIXlt(data2()$ENTRY_DATE_TIME)})
b <-reactive({as.POSIXlt(round(as.double(a())/(15*60))*(15*60),origin= (as.POSIXlt('1970-01-01')))})
f = reactive({format(b(), "%H:%M")})

entry_quarter_hour = f()

data3 = reactive({cbind(data2(), entry_quarter_hour, entry_day)})



data10 = reactive({subset(data3(), data3()$entry_day == input$date1)})



plot(data10()$entry_quarter_hour,
main = input$date,
xlab = "Time of Day",
ylab = "Number of Records",
xlim = c(0, 96),
ylim = c(0, 500)
)

titlepaste = paste0('Counts from this day, as of ',Sys.time(),'')
contentcount = reactive({nrow(data10())})

addPopover(session,
id = "recordsPlot",
title = titlepaste,
content = contentcount(),
placement = "top",
trigger = "hover",
options = NULL)

})

qry2 = reactive({paste0('select MONTH, YEAR, ENTRY_DATE_TIME from
where MONTH = ',input$month2,'')})

datamonth2 = eventReactive(input$go2, {dbGetQuery(con, qry2())})

totaldatamonth2 = reactive({nrow(datamonth2())})

countdatamonth2 = reactive({count(datamonth2(), "YEAR")})

output$ByMonthTotal = renderText({paste("The total count for this month, across all years, is",
totaldatamonth2())})

output$ByMonthCount = renderTable({countdatamonth2()})

qry3 = reactive({paste0('select MONTH, YEAR, ENTRY_DATE_TIME from
where YEAR = ',input$year3,'')})
drv <- dbDriver("Oracle")
con <- dbConnect(drv, username="", password="", dbname= "")

datayear3 =eventReactive(input$go3, {dbGetQuery(con, qry3())})
nextdatayear3 = eventReactive(input$go3, {subset(datayear3(), datayear3()$MONTH %in% input$month3)})

countdatayear3 = reactive({table(nextdatayear3()$MONTH)})


output$MonthsTable = renderTable({countdatayear3()})


drv <- dbDriver("Oracle")
con <- dbConnect(drv, username="", password="", dbname= "")

qry4 = reactive({paste0('select MONTH, YEAR, ENTRY_DATE_TIME from
where YEAR = ',input$year3,'')})

output$monthgraph <- renderPlot({

data20 = eventReactive(input$go3, {dbGetQuery(con, qry4())})


data21 =data20()



entry2_day = reactive({as.Date(trunc(data20()$ENTRY_DATE_TIME, "days"), "%Y%m%d")})

z <-reactive({as.POSIXlt(data20()$ENTRY_DATE_TIME)})

entry3_day <-reactive({as.POSIXlt(round(as.double(z())/(1440*60))*(1440*60),origin=(as.POSIXlt('1970-01-01')))})

entry4_day = reactive({format(entry3_day(), "%d")})
entry5_day = entry2_day()
entry6_day = entry4_day()

monthnames = reactive({

if (input$month3 == "01")
{"January"}
else if (input$month3 == "02")
{"February"}
else if (input$month3 == "03")
{"March"}
else if (input$month3 == "04")
{"April"}
else if (input$month3 == "05")
{"May"}
else if (input$month3 == "06")
{"June"}
else if (input$month3 == "07")
{"July"}
else if (input$month3 == "08")
{"August"}
else if (input$month3 == "09")
{"September"}
else if (input$month3 == "10")
{'October'}
else if (input$month3 == "11")
{'November'}
else if (input$month3 == "12")
{"December"}

})

monthnames2 = monthnames()

data30 = reactive({cbind(data21, entry6_day)})

data22 = reactive({subset(data30(), data30()$MONTH == input$month3)})




plot(data22()$entry6_day,
main = monthnames2,
xlab = "Day of the Month",
ylab = "Number of Records",
xlim = c(0, 35),
ylim = c(0, 7500)
)

titlepaste2 = paste0('Counts from this month, as of ',Sys.time(),'')
contentcount2 = reactive({nrow(data22())})


addPopover(session,
id = "monthgraph",
title = titlepaste2,
content = contentcount2(),
trigger = "hover",
options = NULL
)


})
})



shinyApp(ui = ui, server = server)


I've also attached a screenshot of the result I get when I run the App:The Result
The problem is that this image is from a completely different R script. Specifically, this is the last R script I ran before running the script displayed in my code.
I have no idea what is causing shiny to display the result from the script in the picture rather than the result from the script I am actually running.
If anyone has any suggestions on how I could isolate or fix the problem, I would be very thankful.

Answer

If this app consists of two seperate files Ui.R and server.R and you run it by pressing a button runapp in RStudio, you should remove in both scripts the last line

shinyApp(ui = ui, server = server)

because it runs a shiny app you defined before - this app from the screenshot which is defined in terms of ui and server variables. You should also change the first line in Ui.R from

shinyUi= fluidPage(` to fluidPage(

Now after pressing runapp button the app should work fine.


The other possibility is to overwrite ui and server variables and run it via shinyApp (as a one-file shiny app). Just copy two parts of the app to the same script and then change

    shinyUi= fluidPage( to ui <- fluidPage(

and

server <- shinyServer(function(input, output, session) {

at the end just execute

shinyApp(ui = ui, server = server)