Reputation: 477
For some reason a ggplot in "R Shiny" is different than that of plain R in RStudio
the runnable Shiny code is
library(ggplot2)
library(scales)
library(shiny)
ui <- fluidPage(
# Sidebar layout with a input and output definitions
sidebarLayout(
# Inputs
sidebarPanel(
# Select variable for y-axis
selectInput(inputId = "y",
label = "Y-axis:",
choices = c("Water Level" = "WaterLevel",
"Discharge Debit" = "DischargeDebit"),
selected = "WaterLevel"),
selectInput(inputId = "z",
label = "station:",
choices = c(
"NORTH SASKATCHEWAN RIVER NEAR DEER CREEK"="05EF001",
"BATTLE RIVER NEAR PONOKA"="05FA001",
"BATTLE RIVER AT DUHAMEL"="05FA011",
"PIGEON LAKE AT PIGEON LAKE PROVINCIAL PARK"="05FA013",
"MASKWA CREEK NO. 1 ABOVE BEARHILLS LAKE"="05FA014"
),
selected = "05EF001")
),
# Output
mainPanel(
plotOutput(outputId = "lineplot")
)
)
)
server <- function(input, output) {
file = "http://dd.weather.gc.ca/hydrometric/csv/SK/hourly/SK_hourly_hydrometric.csv"
skdat <- read.csv(file, head=T, sep=",", dec=".", stringsAsFactors = F)
colnames(skdat) <- c("ID", "Date", "WaterLevel", "Grade1", "Symbol1",
"QAQC-1", "DischargeDebit", "Grade2", "Symbol2",
"QAQC-2")
subds <- subset(skdat, ID=input$z)
subds$datetime <- as.POSIXct(subds$Date, format = "%Y-%m-%dT%H:%M:%OS")
output$lineplot <- renderPlot({
ggplot(subds, aes(x = datetime, y = !!as.name(input$y))) + geom_line()
})
}
# Create a Shiny app object
shinyApp(ui = ui, server = server)
and the RStudio code that produces the correct graph is
library(ggplot2)
library(scales)
library(shiny)
library(stringi)
file = "http://dd.weather.gc.ca/hydrometric/csv/SK/hourly/SK_hourly_hydrometric.csv"
skdat <- read.csv(file, head=T, sep=",", dec=".", stringsAsFactors = F)
colnames(skdat) <- c("ID", "Date", "WaterLevel", "Grade1", "Symbol1",
"QAQC-1", "DischargeDebit", "Grade2", "Symbol2",
"QAQC-2")
subds <- subset(skdat, ID=='05EF001')
#subds$datetime <- as.POSIXct(subds$Date, format = "%Y-%m-%dT%H%m%S")
subds$datetime <- as.POSIXct(subds$Date, format = "%Y-%m-%dT%H:%M:%OS")#subds$datetime <- as.POSIXct(subds$Date, format = "%Y-%m-%dT%H%m%S-06:00")
p2 <- ggplot(subds, aes(x = datetime, y = WaterLevel)) + geom_line()
p2
there must be something wrong with the rendering linein the shiny code?
output$lineplot <- renderPlot({
ggplot(subds, aes(x = datetime, y = !!as.name(input$y))) + geom_line()
})
The Shiny
graph comes out a a black block with no line that can be seen.
any help with this would be greatly appreciated.
Thanks Jake!! that helped this below did the trick
#subds <- subset(skdat, ID=input$z)
#subds$datetime <- as.POSIXct(subds$Date, format = "%Y-%m-%dT%H:%M:%OS")
#output$lineplot <- renderPlot({
# ggplot(subds, aes(x = datetime, y = !!as.name(input$y))) + geom_line()
#})
output$lineplot <- renderPlot({
subds <- subset(skdat, ID == input$z)
subds$datetime <- as.POSIXct(subds$Date, format = "%Y-%m-%dT%H:%M:%OS")
ggplot(subds, aes(x = datetime, y = !!as.name(input$y))) +
geom_line()
Upvotes: 3
Views: 1202
Reputation: 8072
There isn't a difference in ggplot2
graphics between viewing in RStudio or Shiny. There is a difference in the code that you used to make both.
=
when you intended ==
)reactive
as.name
, if you're going full tidyeval
you might as well use the proper match of sym
.The code below works, but you might want to check the IDs you want to filter on in the input conditions. Unsure if those are to be added later, but they don't exist in your data as is.
library(ggplot2)
library(scales)
library(shiny)
ui <- fluidPage(
# Sidebar layout with a input and output definitions
sidebarLayout(
# Inputs
sidebarPanel(
# Select variable for y-axis
selectInput(inputId = "y",
label = "Y-axis:",
choices = c("Water Level" = "WaterLevel",
"Discharge Debit" = "DischargeDebit"),
selected = "WaterLevel"),
selectInput(inputId = "z",
label = "station:",
choices = c(
"NORTH SASKATCHEWAN RIVER NEAR DEER CREEK"="05EF001",
"BATTLE RIVER NEAR PONOKA"="05FA001",
"BATTLE RIVER AT DUHAMEL"="05FA011",
"PIGEON LAKE AT PIGEON LAKE PROVINCIAL PARK"="05FA013",
"MASKWA CREEK NO. 1 ABOVE BEARHILLS LAKE"="05FA014"
),
selected = "05EF001")
),
# Output
mainPanel(
plotOutput(outputId = "lineplot")
)
)
)
server <- function(input, output) {
file = "http://dd.weather.gc.ca/hydrometric/csv/SK/hourly/SK_hourly_hydrometric.csv"
skdat <- read.csv(file, head=T, sep=",", dec=".", stringsAsFactors = F)
colnames(skdat) <- c("ID", "Date", "WaterLevel", "Grade1", "Symbol1",
"QAQC-1", "DischargeDebit", "Grade2", "Symbol2",
"QAQC-2")
# Switched the date time to skdat from subds to not mess with reactivity until later
skdat$datetime <- as.POSIXct(skdat$Date, format = "%Y-%m-%dT%H:%M:%OS")
# You need to have a logical condition for subset, == not =
subds <- reactive({subset(skdat, ID == input$z)})
# Switched the use of as.name to rlang::sym
output$lineplot <- renderPlot({
ggplot(subds(), aes(x = datetime, y = !!sym(input$y))) + geom_line()
})
}
# Create a Shiny app object
shinyApp(ui = ui, server = server)
Upvotes: 2