Loading online xml data to slider in R shiny dashboard - r

My friend and I built an R shiny dashboard using downloaded data. The code is as follows:
library(shiny)
library(shinydashboard)
library(dplyr)
library(tidyverse)
library(reshape)
library(scales)
ecd <- read.csv("ecd-figures.csv")
c(
"No of case" = "no_of_case",
"Minor Case" = "minor_case",
"All Non Fatal Case" = "all_non_fatal_case",
"Fatal Case" = "fatal_case"
) -> vec
ui <- fluidPage(sidebarLayout(
sidebarPanel
(
checkboxGroupInput("feature",
"Feature",
vec),
sliderInput(
"year",
"Year",
min = min(ecd$year),
max = max(ecd$year),
value = range(ecd$year),
sep = "",
step = 1
)
),
mainPanel(tabsetPanel(
tabPanel("Plot", plotOutput("correlation_plot")),
tabPanel("Table", tableOutput("ecd"))
))
))
server <- function(input, output) {
yearrange <- reactive({
ecd %>%
subset(year %in% input$year[1]:input$year[2]) %>%
select(c(year, input$feature))
})
output$correlation_plot <- renderPlot({
ecdsubset <- yearrange()
ecdsubset <- melt(ecdsubset, id = "year")
validate(need(input$feature, 'Check at least one item.'))
ggplot(ecdsubset, aes(x = year, y = value, color = variable)) + geom_line(size = 1) + scale_x_continuous(breaks =
seq(input$year[1], input$year[2], by = 1))
})
output$ecd <- renderTable({
yearrange()
})
}
shinyApp(ui, server)
The simple data file is here: https://drive.google.com/file/d/1pZQe89wxw14lirW2mRIgi9h29yPyc7Fs/view?usp=sharing
Then, I want to make everything online, i.e. calling api and not to download the csv file. It seems ok to read the contents rather simply, as follows:
library(xml2)
ecd_xml <-"https://www.labour.gov.hk/datagovhk/resource/ecd/ecd-figures.xml"
read_ecd <- read_xml(ecd_xml)
xml_find_all(read_ecd, ".//year")
{xml_nodeset (5)}
[1] <year>2015</year>
[2] <year>2016</year>
[3] <year>2017</year>
[4] <year>2018</year>
[5] <year>2019</year>
The problem is: how to parse every piece of information from the xml contents onto the dashboard?
Take the slidebar as an example. How to display the slidebar labels (i.e. 2015 and 2019) from parsing the <year> tag and selecting the max and min values?
And, can you recommend some reading materials for me to learn the whole process from xml to dashboard? Many many thanks in advance.
(P.S. I've tried to use xml package instead, since there are some standard arguments to find the max, min, and avg values of attributes. But I ran into another big error -
Error in function (type, msg, asError = TRUE) :
error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version
What should I do? My R version is 4.0.5 (2021-03-31).)

You can use xml2::as_list to create a tibble from the XML tree.
Moreover, you can use shiny::updateSliderInput to update UI slider ranges:
library(shiny)
library(shinydashboard)
library(dplyr)
library(tidyverse)
library(reshape)
library(scales)
library(xml2)
vec <- c(
"No of case" = "no_of_case",
"Minor Case" = "minor_case",
"All Non Fatal Case" = "all_non_fatal_case",
"Fatal Case" = "fatal_case"
)
ui <- fluidPage(sidebarLayout(
sidebarPanel(
checkboxGroupInput(
"feature",
"Feature",
vec
),
sliderInput(
"year",
"Year",
min = 2000,
max = 2001,
value = c(2000, 2001),
sep = "",
step = 1
)
),
mainPanel(tabsetPanel(
tabPanel("Plot", plotOutput("correlation_plot")),
tabPanel("Table", tableOutput("table"))
))
))
server <- function(input, output, session) {
xml <-
"https://www.labour.gov.hk/datagovhk/resource/ecd/ecd-figures.xml" %>%
read_xml()
table <-
xml %>%
xml_find_all(".//item") %>%
map(as_list) %>%
map(~ .x %>% as_tibble()) %>%
bind_rows() %>%
unnest(everything()) %>%
type_convert()
years <- table$year %>% unique()
updateSliderInput(
session = session,
inputId = "year",
min = min(years),
max = max(years),
value = c(min(years), max(years)) # current selected range
)
sub_table <- reactive({
table %>%
filter(year %in% input$year[1]:input$year[2]) %>%
select(c(year, input$feature))
})
output$correlation_plot <- renderPlot({
validate(need(input$feature, "Check at least one item."))
sub_table() %>%
pivot_longer(-year) %>%
ggplot(aes(x = year, y = value, color = name)) +
geom_line(size = 1) +
scale_x_continuous(
breaks = seq(input$year[1], input$year[2], by = 1)
)
})
output$table <- renderTable({
sub_table()
})
}
shinyApp(ui, server)

Related

Slider with years for barplot

I am trying to get a slider within my barplot page to make the data interactive per year.
#library
library(dplyr)
library(shiny)
library(shinythemes)
library(ggplot2)
#Source
dataset <- read.csv("Wagegap.csv")
SFWage <- dataset %>%
group_by(gender,JobTitle, Year) %>%
summarise(averageBasePay = mean(BasePay, na.rm=TRUE)) %>%
select(gender, JobTitle, averageBasePay, Year)
clean <- SFWage %>% filter(gender != "")
#UI
ui <- fluidPage(
theme = shinytheme("united"),
navbarPage("San Fransisco Wages",
tabPanel("Barplot",
mainPanel(
plotOutput("barplot")
)) ,
tabPanel("Table",
mainPanel(
dataTableOutput("table")
))
)
)
#server
server <- function(input, output){
output$barplot <- renderPlot({
ggplot(clean, aes(x = JobTitle, y = averageBasePay ))+
geom_bar(stat="Identity", width = 0.3, fill="orange")+
labs(x= "Jobs", y = "Wage", title = "Wage per job")
})
output$table <- renderDataTable({
clean
})
}
#Run App
shinyApp(ui = ui, server = server)
I don't fully understand it yet how to put this input in.
I have tried sliding it into the navbarpage but I can't figure out how it works.
I also tried making year reactive but with no success.
It's not the year that has to be reactive; it's the whole data frame. Therefore, in your ui, you can do:
[...]
tabPanel("Barplot",
mainPanel(
sliderInput("year", label = "Which year should be displayed?", min = 1900, max = 2020, step = 5, value = 2000) # new
plotOutput("barplot")
)) ,
[...]
I put it there for convenience; the layout is yours. I tried do change as little as possible.
The server would then have:
server <- function(input, output){
# NEW ########################################
clean <- reactive({
SFWage <- dataset %>%
group_by(gender,JobTitle, Year) %>%
summarise(averageBasePay = mean(as.numeric(BasePay), na.rm=TRUE)) %>% # Notice the as.numeric()
select(gender, JobTitle, averageBasePay, Year)
SFWage %>% filter(gender != "" & Year == input$year)
})
# OLD ########################################
output$barplot <- renderPlot({
ggplot(clean(), aes(x = JobTitle, y = averageBasePay ))+ # Parenthesis
geom_bar(stat="Identity", width = 0.3, fill="orange")+
labs(x= "Jobs", y = "Wage", title = "Wage per job")
})
output$table <- renderDataTable({
clean() # Parenthesis
})
}
Don't forget to add the parenthesis, as I did here.
This should work, but I might have mistyped something or got it completely wrong. Since I don't have your data, I can't test it.
EDIT: Due to your comment, I added the as.numeric() term, as you can see above. However, if your data is not only not numeric but also with ,, you can do:
[...]
summarise(averageBasePay = mean(as.numeric(gsub(",", ".", BasePay)), na.rm=TRUE)) %>% # Notice the as.numeric() and the gsub()
[...]

Select columname through selectinput in shiny appliaction

I'm trying to build a shiny app, it is good to go but I am trying to put a column from my dataframe in selectinput, but so far didn't found a solution. I have a column with 505 factors, called AAPL, AAL, etc.. I want these factors in my selectinput, so that you can choose from these 505 factors, This is my code right now, and the column name that I'm trying to get in selectinput is bcl-data$Name.
library(shiny)
library(tidyverse)
library(shinythemes)
library(ggplot2)
library(dplyr)
bcl <- read.csv("bcl-data.csv", stringsAsFactors = FALSE)
# Define UI for application that draws a histogram
ui <- fluidPage(theme = shinytheme("darkly"),
# Application title
titlePanel("Overzicht S&P 500 Aandelen"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput(inputId = "priceInput", label = "close", min = 0, max = 2050, value = c(0,300), pre = "$"),
selectInput(inputId = "typeInput", label = "Name", choices = (bcl-data$Name)),
dateRangeInput(inputId = "dateInput",
label = "date",
start = "2013/02/08",
end = "2013/03/08",
format = "yy/mm/dd")
),
# Show a plot of the generated distribution
mainPanel(
tabsetPanel(
tabPanel("Plot", plotOutput("Plot")),
tabPanel("Datatable", tableOutput("Datatable"))
)
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$Plot <- renderPlot({
filtered <- bcl %>%
filter(close >= input$priceInput[1]) %>%
filter(close <= input$priceInput[2]) %>%
filter(date >= input$dateInput[1] & date <= input$dateInput[2]) %>%
filter(bcl-data$Name == input$typeInput)
filtered
ggplot(filtered, aes(x = date, y = close, color = Name)) +
geom_point()
})
output$Datatable <- renderTable({
filtered <-
bcl %>%
filter(close >= input$priceInput[1]) %>%
filter(close <= input$priceInput[2]) %>%
filter(date >= input$dateInput[1] & date <= input$dateInput[2]) %>%
filter(bcl-data$Name == input$typeInput)
filtered
})
}
# Run the application
shinyApp(ui = ui, server = server)
Comment from above: I think you're error is with bcl-data$Name. While bcl-data.csv is the file you loaded, you saved it as the object bcl - meaning it should simply be bcl$Name. selectInput(inputId = "typeInput", label = "Name", choices = bcl$Name) In your filters, you can also simply have filter(Name == because you're already feeding the bcl data/object through the pipe.
To make sure we remove duplicate values, we can include unique.
Here's what I think should work (cannot test because no data).
library(shiny)
library(tidyverse)
library(shinythemes)
library(ggplot2)
library(dplyr)
bcl <- read.csv("bcl-data.csv", stringsAsFactors = FALSE)
# Define UI for application that draws a histogram
ui <- fluidPage(theme = shinytheme("darkly"),
# Application title
titlePanel("Overzicht S&P 500 Aandelen"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput(inputId = "priceInput", label = "close", min = 0, max = 2050, value = c(0,300), pre = "$"),
selectInput(inputId = "typeInput", label = "Name", choices = unique(bcl$Name)),
dateRangeInput(inputId = "dateInput",
label = "date",
start = "2013/02/08",
end = "2013/03/08",
format = "yy/mm/dd")
),
# Show a plot of the generated distribution
mainPanel(
tabsetPanel(
tabPanel("Plot", plotOutput("Plot")),
tabPanel("Datatable", tableOutput("Datatable"))
)
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$Plot <- renderPlot({
filtered <- bcl %>%
filter(close >= input$priceInput[1]) %>%
filter(close <= input$priceInput[2]) %>%
filter(date >= input$dateInput[1] & date <= input$dateInput[2]) %>%
filter(Name == input$typeInput)
filtered
ggplot(filtered, aes(x = date, y = close, color = Name)) +
geom_point()
})
output$Datatable <- renderTable({
filtered <-
bcl %>%
filter(close >= input$priceInput[1]) %>%
filter(close <= input$priceInput[2]) %>%
filter(date >= input$dateInput[1] & date <= input$dateInput[2]) %>%
filter(Name == input$typeInput)
filtered
})
}
# Run the application
shinyApp(ui = ui, server = server)

Highcharter and Shiny with reactive dataset/mutated dataset within server function not working

When I try to produce a highcharter barplot within Shiny using a dataset that is grouped and summarized based on the selectInput values and these same values are referenced within hcaes() I get the error "object 'input' not found"
I have also tried hcaes_string() and then I get "object 'My.Variable' not found" but when I just put in My.Variable, it will produce the hchart so it can interact with the dataset being created within the server function. Obviously I'd like to switch between My.Variable and My.Variable2 with the dropdown. I've tried assigning the summarized dataset to a reactive object, but then I get the error "Objects of class/type reactiveExpr/reactive/function are not supported by hchart (yet)."
I've been at this for hours and this is my first question on StackOverflow. I rigged up a sample dataset so that the code is reproducible and I've updated R and RStudio to the latest versions.
library('highcharter')
library('plyr')
library('dplyr')
library('tidyr')
library('lubridate')
library('stringr')
library('tools')
library('shiny')
#demo <- read.csv("data/name-change-analysis.csv",stringsAsFactors = FALSE)
indiv <- rep(c('p1','p2','p3','p4','p5'),4)
Name.Change <- rep(c('yes','yes','no','yes','no'),4)
Overall.Category <- rep(c('against','support','support','neutral','against'),4)
Race <- rep(c('Black','White','White','Asian','White'),4)
Gender <- rep(c('Male','Male','Male','Female','Male'),4)
demo <- as.data.frame(cbind(indiv,Name.Change,Overall.Category,Race,Gender))
ui <-
navbarPage(
"Responses by demographics",
tabPanel(
"Manual labels",
fluidPage(
fluidRow(
column(
selectInput(
"category",
label = "Select a demographic category:",
choices = c("Race",
"Gender" = "gender")
),
width = 6
),
column(
selectInput(
"name_or_overall",
label = "Response Category",
choices = c(
"Name Change" = "Name.Change",
"Overall Category" = "Overall.Category"
),
width = "100%"
),
width = 6
)
),
highchartOutput("hcontainer")
)
),
collapsible = TRUE
)
server <- function(input, output, session) {
output$hcontainer <- renderHighchart({
demo %>%
group_by(input$category,input$name_or_overall) %>%
summarise(count = n()) %>%
hchart(type = "bar",
hcaes(y = "count",
x = as.name(input$category),
group = as.name(input$name_or_overall))) %>%
hc_plotOptions(bar = list(stacking = "percent")) %>%
hc_tooltip(shared = TRUE)
})
}
shinyApp(ui,server)
Try this
output$hcontainer <- renderHighchart({
df1 <- demo %>% mutate(var1=demo[[as.name(input$category)]], var2=demo[[as.name(input$name_or_overall)]])
df <- df1 %>% group_by(var1,var2) %>% summarise(count = n())
highchart() %>%
hc_add_series(df, type = "bar",
hcaes(y = "count",
x = "var1",
group = "var2")) %>%
hc_plotOptions(bar = list(stacking = "percent")) %>%
hc_tooltip(shared = FALSE)
})
You will get the following output:

Using validate in Shiny to hide plot without relevant data when using reactive function (R)

I have created an app using Shiny that displays data dependent on two different inputs. I'm filtering the data in a reactive function and then passing this through to the plots.
I can't work out how to simply hide the plots (and ideally show a helpful explanation) when there is no relevant data based on the inputs. I could do this if my data was in a dataframe, but as I have filtered it using a reactive function, this doesn't work.
I currently have the validate function nested in the renderPlot function, referencing the dataframe that is filtered by the reactive function...
Does anybody have any thoughts?
Reproducible code (if you select "Bristol" with the default date range, that demonstrates the issue):
library("tidyverse")
location <- as.character(c("London", "London", "Birmingham", "Bristol", "Birmingham", "Birmingham", "London", "Birmingham"))
dog_birthday <- as.POSIXct(c("01-01-2016", "02-02-2016", "03-03-2016", "04-04-2017", "05-05-2017", "06-06-2017", "08-08-2018", "07-07-2018"), format = "%d-%m-%Y")
dog_type <- as.character(c("Poodle", "Pug", "Labrador", "Poodle", "Poodle", "Labrador", "Pug", "Pug"))
dog_data <- data.frame(location, dog_birthday, dog_type)
ui<-
fluidPage(
sidebarLayout(
sidebarPanel(
dateRangeInput(
"dates", label = h3("Birthdate range"), start = ("01-06-2018"),
format = "dd-mm-yyyy", startview = "year"
),
selectInput(
"location", label = h3("Location"), choices = unique(dog_data$location),
multiple = T, selectize = T
)
),
mainPanel(
plotOutput(outputId = "dog_type")
)
)
)
server <- function(input, output) {
city_selection <- reactive({
req(input$location)
choose_city <- subset(dog_data, dog_data$location %in% input$location)
choose_city <- droplevels(choose_city)
return(choose_city)
})
output$dog_type <- renderPlot({
validate(
need(nrow(dog_data) > 0, "No data for this selection.")
)
dog_type_plot <- city_selection() %>%
filter(dog_birthday >= input$dates[1] & dog_birthday <= input$dates[2]) %>%
count(dog_type) %>%
arrange(-n) %>%
mutate(dog_type = factor(dog_type, dog_type)) %>%
ggplot(aes(dog_type, n)) +
geom_bar(stat = "identity")
dog_type_plot
})
}
shinyApp(ui, server)
You need to move the dates filter to the city_selection reactive and update the need condition in validate -
server <- function(input, output) {
city_selection <- reactive({
req(input$location)
choose_city <- subset(dog_data, dog_data$location %in% input$location) %>%
filter(dog_birthday >= input$dates[1] & dog_birthday <= input$dates[2])
choose_city <- droplevels(choose_city)
return(choose_city)
})
output$dog_type <- renderPlot({
validate(
need(nrow(city_selection()) > 0, "No data for this selection.")
)
dog_type_plot <- city_selection() %>%
count(dog_type) %>%
arrange(-n) %>%
mutate(dog_type = factor(dog_type, dog_type)) %>%
ggplot(aes(dog_type, n)) +
geom_bar(stat = "identity")
dog_type_plot
})
}
I also got an error trying to run the code:
Warning: Error in count: Argument 'x' must be a vector: list
A few other things that I noticed:
For me, choose_city <- droplevels(choose_city) doesn't do anything, I think you need choose_city$location <- droplevels(choose_city$location) if you're trying to remove the un-selected factor levels from location
I think #Shree's suggestion will help, but this method still only checks for the location, not the dates. (The reason your version doesn't do anything is because dog_data is your reference data.frame, and it doesn't get changed by your subsetting) #Shree's updated answer moved the date subset and now is probably better than this one :)
I changed your code a decent amount to get it to work for me (just because I don't use pipes and am most familiar with data.table). Obviously you can just remove the data.table dependency and filter with pipes!
The main thing is just that you want to check what dog_type_plot looks like right before making the plot. I added a reactiveVal to hold a message that's output in the sidebar:
library("tidyverse")
library("data.table")
location <- as.character(c("London", "London", "Birmingham", "Bristol", "Birmingham", "Birmingham", "London", "Birmingham"))
dog_birthday <- as.POSIXct(c("01-01-2016", "02-02-2016", "03-03-2016", "04-04-2017", "05-05-2017", "06-06-2017", "08-08-2018", "07-07-2018"), format = "%d-%m-%Y")
dog_type <- as.character(c("Poodle", "Pug", "Labrador", "Poodle", "Poodle", "Labrador", "Pug", "Pug"))
dog_data <- data.frame(location, dog_birthday, dog_type)
ui<-
fluidPage(
sidebarLayout(
sidebarPanel(
dateRangeInput(
"dates", label = h3("Birthdate range"), start = ("01-06-2018"),
format = "dd-mm-yyyy", startview = "year"
),
selectInput(
"location", label = h3("Location"), choices = unique(dog_data$location),
multiple = T, selectize = T
),
textOutput(outputId = "noDataMsg")
),
mainPanel(
plotOutput(outputId = "dog_type")
)
)
)
server <- function(input, output) {
## Subset base data.frame by user-selected location(s)
city_selection <- reactive({
req(input$location)
choose_city <- subset(dog_data, dog_data$location %in% input$location)
choose_city$location <- droplevels(choose_city$location)
return(choose_city)
})
## Value to hold message
message_v <- reactiveVal(); message_v("blank")
## Make Histogram
output$dog_type <- renderPlot({
print("city_selection():")
print(city_selection())
cat("\n")
## Change to data.table
data_dt <- as.data.table(city_selection())
print("original data_dt:")
print(data_dt)
cat("\n")
## Subset by birthday
dog_type_plot <- data_dt[dog_birthday >= input$dates[1] &
dog_birthday <= input$dates[2],]
print("subset by birthday")
print(dog_type_plot)
cat("\n")
## Get counts and sort
dog_type_plot[, N := .N, by = dog_type]
dog_type_plot <- dog_type_plot[order(-N)]
print("add count:")
print(dog_type_plot)
cat("\n")
## Change dog type to factor
dog_type_plot$dog_type <- factor(dog_type_plot$dog_type, levels = unique(dog_type_plot$dog_type))
print("refactor of dog_type:")
print(dog_type_plot$dog_type)
cat("\n")
## Check for data to plot
if (nrow(dog_type_plot) == 0) {
message_v("No dogs to plot using these parameters")
return(NULL)
} else {
## Make plot
plot_gg <- ggplot(data = dog_type_plot, aes(x = dog_type, y = N)) +
geom_bar(stat = "identity")
## Return
return(plot_gg)
} # fi
}) # renderPlot
## Message to user
output$noDataMsg <- renderText({ if (message_v() == "blank") { return(NULL) } else { message_v() } })
}
shinyApp(ui, server)

R Shiny ggvis does not react on dynamic input

I try to build a shiny dashboard with ggvis barcharts. The idea is to display barcharts with top 10 costs for a particular month in a year. The user may choose a year and a month. The choice of months is dependend on the chosen year, so if choosing 2016 only the months to date can be selected.
Somehow I can't pass the value of the dynamic input field to the barchart. Though it works as long as both input fields are static (exchanging input$month1 with input$monthtest in the first lines of the server script shows you how it is supposed to look like). I have tried about everything I can think of from isolating() the input month to using observe() instead of reactive().
The error returned is: Warning: Error in eval: incorrect length (0), expecting: 12; i.e. somehow the value in the dynamic field is not passed to the reactive environment
Many thanks in advance!
Here is a "minimal" working example close enough to what my actual dashboard looks like:
#Minimal example Shiny dynamic field
#ggvis Barchart depending on static and dynamic dropdown
library("zoo")
library("shiny")
library("ggvis")
library("magrittr")
library("dplyr")
library("lubridate")
#data-------------
#in the actual code months are in German
month.loc <- c("January","February","March","April","Mai", "June",
"July","August","September","October","November", "December")
#generate data
Databc1M<-data.frame("date"=seq(as.Date("2015-01-01"),
as.Date("2016-03-01"), by="months"), "cost1"=runif(15,min=0,max=100),
"cost2"=runif(15,min=0,max=100), "cost3"=runif(15,min=0,max=100),
"cost4"=runif(15,min=0,max=100),
"cost5"=runif(15,min=0,max=100), "cost6"=runif(15,min=0,max=100),
"cost7"=runif(15,min=0,max=100),
"cost8"=runif(15,min=0,max=100), "cost9"=runif(15,min=0,max=100),
"cost10"=runif(15,min=0,max=100))
#ui-------------
ui <- fluidPage(
ggvisOutput(plot_id = "Barcha_Abw"),
selectInput(inputId = "Year1", label = h3("Choose a year"),
choices = c(2015,2016), selected=2015
),
#Rendering plot works with static input field
selectInput(inputId = "monthtest", label = h3("Choose a month"),
choices = month.loc[1:3], selected=month.loc[1]
),
uiOutput("bc1month")
)
#server-------------
server <- function(input, output, session) {
output$bc1month <- renderUI({
#filter available months in a given year
outmonths<- Databc1M %>% dplyr::filter(format(Databc1M$date,"%Y")==
as.character(input$Year1)) %>%
.[[1]] %>% month() %>% month.loc[.]
#dynamic dropdown; latest month selected
selectInput(inputId = 'month1', label = h3("Choose a month
(dynamic drop down)"), choices = outmonths,
#choose latest month in dataset
selected= Databc1M[NROW(Databc1M),"date"] %>% month() %>%
month.loc[.])
})
#structure data and render barchart
TopAbw1 <- reactive({
Dataaux <- Databc1M %>%
dplyr::filter(format(Databc1M$date,"%Y")==input$Year1)
#!!!!!! not working: match(input$month1,...); input$monthtest works
Dataaux<-
dplyr::filter(Dataaux,month(Dataaux$date)==match(input$month1,
month.loc)) #input$month1
Dataaux <- gather(Dataaux,key="cost_id",value="Abweichung")
Dataaux <- Dataaux[-1,]
Dataaux <- Dataaux[order(Dataaux$Abweichung,decreasing=TRUE),] %>%
head(10)
Dataaux$cost_id <- factor(Dataaux$cost_id, levels = Dataaux$cost_id)
#render barchart
Dataaux %>%
ggvis(y=~abs(Abweichung),x=~cost_id) %>%
layer_text(text:=~round(abs(Abweichung),2)) %>%
layer_bars( stack =FALSE) %>%
add_axis("x", title = "", properties = axis_props(labels =
list(angle= 45, align = "left", fontSize = 12))) %>%
add_axis("y", title = "Mio. EUR")
})
TopAbw1 %>% bind_shiny("Barcha_Abw")
}
shinyApp(ui = ui, server = server)
I had a german version of the question. For the documentation purpose (and if anyone is curious about the answer:
#Minimal example Shiny dynamic field
#ggis Barchart soll nach Nutzereingabe in 2 Dropdowns neu gerendert werden
#Ein Dropdown ist dynamisch
library("zoo")
library("shiny")
library("ggvis")
library("magrittr")
library("dplyr")
library("lubridate")
#data-------------
month.loc <- c("Januar","Februar","März","April","Mai", "Juni",
"Juli","August","September","Oktober","November", "Dezember")
currdate <- as.Date("2015-01-01",format="%Y-%m-%d")
Databc1M<-data.frame("date"=seq(as.Date("2015-01-01"), as.Date("2016-03-01"), by="months"), "cost1"=runif(15,min=0,max=100),
"cost2"=runif(15,min=0,max=100), "cost3"=runif(15,min=0,max=100), "cost4"=runif(15,min=0,max=100),
"cost5"=runif(15,min=0,max=100), "cost6"=runif(15,min=0,max=100), "cost7"=runif(15,min=0,max=100),
"cost8"=runif(15,min=0,max=100), "cost9"=runif(15,min=0,max=100), "cost10"=runif(15,min=0,max=100))
#ui-------------
ui <- fluidPage(
ggvisOutput(plot_id = "Barcha_Abw"),
selectInput(inputId = "Year1", label = h3("Wählen Sie ein Jahr"),
choices = c(2015,2016), selected=2015
),
#Test mit fixem dropdown funktioniert der Plot
uiOutput("bc1month"),
plotOutput("TopAbw1")
)
#server-------------
server <- function(input, output, session) {
output$bc1month <- renderUI({
outmonths<- Databc1M %>% dplyr::filter(format(Databc1M$date,"%Y")== input$Year1) %>%
.[[1]] %>% month() %>% month.loc[.]
#input$Year1
selectInput(inputId = 'month1', label = h3("Wählen Sie einen Monat (dynamisch)"), choices = outmonths)
})
TopAbw1 <- reactive({
Dataaux <- Databc1M[which(year(Databc1M$date) %in% input$Year1),]
Dataaux <- Dataaux[which(months(Dataaux$date) %in% input$month1),]
Dataaux <- gather(Dataaux,key="cost_id",value="Abweichung")
Dataaux <- Dataaux[-1,]
Dataaux <- Dataaux[order(Dataaux$Abweichung,decreasing=TRUE),] %>%
head(10)
Dataaux$cost_id <- factor(Dataaux$cost_id, levels = Dataaux$cost_id)
Dataaux %>%
ggvis(y=~abs(Abweichung),x=~cost_id) #%>%
#layer_text(text:=~round(abs(Abweichung),2)) %>%
#layer_bars( stack =FALSE) %>%
#add_axis("x", title = "", properties = axis_props(labels = list(angle = 45, align = "left", fontSize = 12))) %>%
#add_axis("y", title = "Mio. EUR")
})
TopAbw1 %>% bind_shiny("Barcha_Abw")
}
shinyApp(ui = ui, server = server)

Resources