[英]How to proceed reactive function of dataset in Shiny - line chart R
How to proceed reactive function of dataset in Shiny - line chart Especially don't understand how apply dataset into reactive()
function to react the selected input如何在 Shiny - 折线图中处理数据集的反应函数尤其不明白如何将数据集应用到
reactive()
函数中以对选定的输入作出反应
# sample data
df <- economics %>%
select(date, psavert, uempmed) %>%
gather(key = "variable", value = "value", -date) %>%
as.data.table()
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Football Analysis"),
sidebarLayout(
sidebarPanel(
selectizeInput("variable_names", "Names of Variable", choices = unique(df$variable), selected = unique(df$variable)[1])
),
mainPanel(
highchartOutput("plot")
)
)
)
server <- function(input, output) {
reactivedf <- reactive({ df[variable == input$variable_names,]
})
output$plot <- renderHighchart({
df %>%
hchart('line',hcaes(x = reactivedf()$date,y = reactivedf()$value))
})
}
# Run the application
shinyApp(ui = ui, server = server)
My Goal is simply to choose 1 type of series then s how that type with line chart , in aim to learnt the usage of shiny.我的目标只是选择一种类型的系列,然后用折线图选择该类型,目的是学习闪亮的用法。 I can't figure out how to apply in normal ggplot as well.
我也不知道如何在普通的 ggplot 中应用。
ggplot(df, aes(x = date, y = value)) +
geom_line(aes(color = variable), size = 1) +
scale_color_manual(values = c("#00AFBB", "#E7B800")) +
theme_minimal()
This should bring you close这应该让你接近
# load packages
library(shiny)
library(highcharter)
library(ggplot2)
library(dplyr)
library(tidyr)
library(data.table)
# sample data
df <- economics %>%
select(date, psavert, uempmed) %>%
gather(key = "variable", value = "value", -date) %>%
as.data.table()
# define color mapping
color_mapping <- c("psavert" = "#00AFBB", "uempmed" = "#E7B800")
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Football Analysis"),
sidebarLayout(
sidebarPanel(
selectizeInput(inputId = "variable_names",
label = "Names of Variable",
choices = unique(df$variable),
selected = unique(df$variable)[1])
),
mainPanel(
# use plotOutput for ggplot
plotOutput("plot")
)
)
)
server <- function(input, output, session) {
# use renderPlot for ggplot
output$plot <- renderPlot({
df %>%
# you can use the input like a regular string
filter(variable == input$variable_names) %>%
ggplot(aes(x = date, y = value)) +
geom_line(aes(color = variable), size = 1) +
# show different colors for different values
scale_color_manual(values = color_mapping[input$variable_names]) +
theme_minimal()
})
}
# Run the application
shinyApp(ui = ui, server = server)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.