selectInput 变量影响图表输出 - 组参数?最好使用 highcharter 输出 [Shiny app]
selectInput variable to influence chart output - group argument? Preferably using highcharter output [Shiny app]
总的来说,我是 shiny 和 R 的新手,一直在思考如何让我的应用程序更具交互性。我的功能的其他部分可以正常工作,但是我所追求的交互性并没有达到目的,而且碰壁了。
我有一个数据集,我希望用户能够通过选定的分组选项进行查看,它是对给定 KeyDates 的观察的集合,为了我的目的,我希望看到这个图表显示:Year/Quarter 或月份。
> head(myData)
KeyDate Area SubArea MetricGroup Metric Count Amount Year Quarter MonthNo WeekNo
1 2019-11-30 Billing Billing Billing Documents Bill Documents Created 0 NA 2019 1 11 48
2 2019-11-30 Billing Billing Billing Outsorts Bill Outsorts 0 NA 2019 1 11 48
3 2019-11-30 Device Management Meter Reading Meter Reading Implausible Reads Uploaded 0 NA 2019 1 11 48
4 2019-11-30 Device Management Meter Reading Meter Reading No of Implausible reads - Backlog 847 NA 2019 1 11 48
5 2019-12-01 Billing Billing Billing Documents Bill Documents Created 0 NA 2019 2 12 48
6 2019-12-01 Billing Billing Billing Outsorts Bill Outsorts 0 NA 2019 2 12 48
ui.R
# Load the relevant libraries
library(shiny)
library(highcharter)
# Use a fluid Bootstrap layout
fluidPage(
# Give the page a title
titlePanel("Demo of dynamic chart selection?"),
# Generate a row with a sidebar
sidebarLayout(
# Define the sidebar with two input variables?
sidebarPanel(
selectInput("chartGroup", "Chart Grouping:",
choices=c("Year", "Quarter", "MonthNo")),
# hr(),
helpText("How would you like to display the data?"),
selectInput("chartMetric", "Chart Metric:",
choices=c("Bill Documents Created", "Bill Outsorts",
"Implausible Reads Uploaded", "No of Implausible reads - Backlog")),
# hr(),
helpText("Which dimension would you like to view?")
),
# Create a spot for the barplot
mainPanel(
highchartOutput("testPlot")
)
)
)
server.R
# Load the relevant libraries
library(dplyr)
library(highcharter)
library(lubridate)
# This setting may help???
options(stringsAsFactors = FALSE)
# Read in the dataset from csv file
testData <- read.csv("./data/testData.csv", header = TRUE, sep = ",")
# Hack the Date into an actual Date field in R (because I have no idea how else to do it)
tempdate <- as.Date(testData$Date, "%d.%m.%Y")
# Bind the two datsets
temp_data <- cbind(KeyDate = tempdate, testData)
# Now I have dataset to use for the charting ...
myData <- select(temp_data, -Date)
# Take a peek at the dataset
head(myData)
# Define a server for the Shiny app
function(input, output) {
# >> Start of testPlot
output$testPlot <- renderHighchart({
# Show the selection from th UI input
si_choice <- input$chartGroup
print(si_choice)
si_metric <- input$chartMetric
print(si_metric)
# >> This is where I would like to be able to 'select' what is used for grouping
# >> User could select from Year/Quarter/MonthNo
# Output the chart using highcharter
hchart(myData, "column", hcaes(x = KeyDate, y = Count, group = Year)) %>%
hc_plotOptions(column = list(pointWidth = 10),
line = list(
color = "blue",
marker = list(
fillColor = "white",
lineWidth = 2,
lineColor = NULL
)
))
})
# << End of testPlot
}
任何人都可以告诉我我必须做什么才能让 "group = Year" 响应用户选择输入。
"group = input$chartMetric" ... 使用正确的语法,以便实现我正在寻找的结果?
# Output the chart using highcharter
hchart(myData, "column", hcaes(x = KeyDate, y = Count, group = Year)) %>%
非常感谢这个新手问题
所需的语法是:
group = !!sym(input$chartGroup)
将文本转换为数据集中的列名
# Output the chart using highcharter
hchart(myData, "column", hcaes(x = KeyDate, y = Count, group = !!sym(input$chartGroup))) %>%
总的来说,我是 shiny 和 R 的新手,一直在思考如何让我的应用程序更具交互性。我的功能的其他部分可以正常工作,但是我所追求的交互性并没有达到目的,而且碰壁了。
我有一个数据集,我希望用户能够通过选定的分组选项进行查看,它是对给定 KeyDates 的观察的集合,为了我的目的,我希望看到这个图表显示:Year/Quarter 或月份。
> head(myData)
KeyDate Area SubArea MetricGroup Metric Count Amount Year Quarter MonthNo WeekNo
1 2019-11-30 Billing Billing Billing Documents Bill Documents Created 0 NA 2019 1 11 48
2 2019-11-30 Billing Billing Billing Outsorts Bill Outsorts 0 NA 2019 1 11 48
3 2019-11-30 Device Management Meter Reading Meter Reading Implausible Reads Uploaded 0 NA 2019 1 11 48
4 2019-11-30 Device Management Meter Reading Meter Reading No of Implausible reads - Backlog 847 NA 2019 1 11 48
5 2019-12-01 Billing Billing Billing Documents Bill Documents Created 0 NA 2019 2 12 48
6 2019-12-01 Billing Billing Billing Outsorts Bill Outsorts 0 NA 2019 2 12 48
ui.R
# Load the relevant libraries
library(shiny)
library(highcharter)
# Use a fluid Bootstrap layout
fluidPage(
# Give the page a title
titlePanel("Demo of dynamic chart selection?"),
# Generate a row with a sidebar
sidebarLayout(
# Define the sidebar with two input variables?
sidebarPanel(
selectInput("chartGroup", "Chart Grouping:",
choices=c("Year", "Quarter", "MonthNo")),
# hr(),
helpText("How would you like to display the data?"),
selectInput("chartMetric", "Chart Metric:",
choices=c("Bill Documents Created", "Bill Outsorts",
"Implausible Reads Uploaded", "No of Implausible reads - Backlog")),
# hr(),
helpText("Which dimension would you like to view?")
),
# Create a spot for the barplot
mainPanel(
highchartOutput("testPlot")
)
)
)
server.R
# Load the relevant libraries
library(dplyr)
library(highcharter)
library(lubridate)
# This setting may help???
options(stringsAsFactors = FALSE)
# Read in the dataset from csv file
testData <- read.csv("./data/testData.csv", header = TRUE, sep = ",")
# Hack the Date into an actual Date field in R (because I have no idea how else to do it)
tempdate <- as.Date(testData$Date, "%d.%m.%Y")
# Bind the two datsets
temp_data <- cbind(KeyDate = tempdate, testData)
# Now I have dataset to use for the charting ...
myData <- select(temp_data, -Date)
# Take a peek at the dataset
head(myData)
# Define a server for the Shiny app
function(input, output) {
# >> Start of testPlot
output$testPlot <- renderHighchart({
# Show the selection from th UI input
si_choice <- input$chartGroup
print(si_choice)
si_metric <- input$chartMetric
print(si_metric)
# >> This is where I would like to be able to 'select' what is used for grouping
# >> User could select from Year/Quarter/MonthNo
# Output the chart using highcharter
hchart(myData, "column", hcaes(x = KeyDate, y = Count, group = Year)) %>%
hc_plotOptions(column = list(pointWidth = 10),
line = list(
color = "blue",
marker = list(
fillColor = "white",
lineWidth = 2,
lineColor = NULL
)
))
})
# << End of testPlot
}
任何人都可以告诉我我必须做什么才能让 "group = Year" 响应用户选择输入。 "group = input$chartMetric" ... 使用正确的语法,以便实现我正在寻找的结果?
# Output the chart using highcharter
hchart(myData, "column", hcaes(x = KeyDate, y = Count, group = Year)) %>%
非常感谢这个新手问题
所需的语法是:
group = !!sym(input$chartGroup)
将文本转换为数据集中的列名
# Output the chart using highcharter
hchart(myData, "column", hcaes(x = KeyDate, y = Count, group = !!sym(input$chartGroup))) %>%