R Shiny 上的 ggplot,vs RStudio
ggplot on R Shiny, vs RStudio
出于某种原因,"R Shiny" 中的 ggplot 与 RStudio 中的普通 R 不同
可运行的闪亮代码是
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)
生成正确图形的 RStudio 代码是
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
闪亮的代码中的渲染行一定有问题吗?
output$lineplot <- renderPlot({
ggplot(subds, aes(x = datetime, y = !!as.name(input$y))) + geom_line()
})
Shiny
图形出来的是一个黑块,看不到任何线条。
如有任何帮助,我们将不胜感激。
谢谢杰克!!帮助下面这个成功了
#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()
在 RStudio 或 Shiny 中查看时,ggplot2
图形没有区别。 您用于制作两者的代码存在差异。
- 您的子集中没有逻辑语句(您使用了
=
而您打算使用 ==
)
- 对于 1,您的数据集需要包装在
reactive
- 虽然您可以使用
as.name
,但如果您要使用 tidyeval
,您也可以使用 sym
的正确匹配。
下面的代码有效,但您可能需要在输入条件中检查要过滤的 ID。不确定以后是否会添加这些,但它们在您的数据中并不存在。
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)
出于某种原因,"R Shiny" 中的 ggplot 与 RStudio 中的普通 R 不同
可运行的闪亮代码是
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)
生成正确图形的 RStudio 代码是
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
闪亮的代码中的渲染行一定有问题吗?
output$lineplot <- renderPlot({
ggplot(subds, aes(x = datetime, y = !!as.name(input$y))) + geom_line()
})
Shiny
图形出来的是一个黑块,看不到任何线条。
如有任何帮助,我们将不胜感激。
谢谢杰克!!帮助下面这个成功了
#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()
在 RStudio 或 Shiny 中查看时,ggplot2
图形没有区别。 您用于制作两者的代码存在差异。
- 您的子集中没有逻辑语句(您使用了
=
而您打算使用==
) - 对于 1,您的数据集需要包装在
reactive
- 虽然您可以使用
as.name
,但如果您要使用tidyeval
,您也可以使用sym
的正确匹配。
下面的代码有效,但您可能需要在输入条件中检查要过滤的 ID。不确定以后是否会添加这些,但它们在您的数据中并不存在。
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)