I am working on a flexdashboard project and I'm using Shiny to gather user input and create a dynamic histogram. The dataset I am working with has for each date some numeric values associated to a specific country. I want the user to select the country and the starting/ending date between which he would like to compute and plot the histogram. For this I wrote the following code:
fr_gb <- dataset_impexp[,c(1,3,4)]
fr_cwe <- dataset_impexp[,c(1,5,6)]
fr_ch <- dataset_impexp[,c(1,7,8)]
fr_it <- dataset_impexp[,c(1,9,10)]
fr_es <- dataset_impexp[,c(1,11,12)]
selectInput("pays", label = "Pays à visualiser:",
choices = c("Royaume Uni", "CWE", "Suisse", "Italie", "Espagne","France"))
dateInput(
"startdateee",
"Date de début: ",
value = min(dataset_impexp$date, na.rm = TRUE),
min = min(dataset_impexp$date, na.rm = TRUE),
max = max(dataset_impexp$date, na.rm = TRUE),
language = "fr"
)
dateInput(
"enddateee",
"Date de fin: ",
value = max(dataset_impexp$date, na.rm = TRUE),
min = min(dataset_impexp$date, na.rm = TRUE),
max = max(dataset_impexp$date, na.rm = TRUE),
language = "fr"
)
As you may I have guessed, my dataset is dataset_impexp and the first column is the date, the other two columns that I select each time are the import export associated to each country, so the two columns of interest.
I then wrote the following code:
dstt <- reactive({if (input$pays=="Royaume-Uni"){
fr_gb <- subset(fr_gb, (fr_gb$date>as.Date(input$startdateee)&fr_gb$date<as.Date(input$enddateee)))
fr_gb <- fr_gb[,c(2,3)]
exp <- names(fr_gb)[1]
imp <- names(fr_gb)[2]
fill_colors <- c("blue", "red")
names(fill_colors) <- c(exp, imp)
dat <- gather(fr_gb, key = "Category", value = "Value")
dat$Value <- as.numeric(dat$Value)
vect <- c(dat,fill_colors)
return(vect)
}
})
renderPlot({ggplot(dstt()[1], aes(x = Value, fill = Category)) +
geom_histogram(
binwidth = 200,
position = "identity",
alpha = 0.7,
color = "black",
boundary = 0
) +
labs(title = glue("Histogramme imports/exports France et {input$pays}"),
x = "Quantité (MWh)",
y = "Frequence") +
scale_fill_manual(values = dstt()[2]) +
theme_minimal()
})
The goal here is to plot in different colors the histograms associated to each of the two columns. I only wrote the case for 'Royaume-Uni' to test if everything was working fine but I run into an issue, my error is the following:
Input to asJSON(keep_vec_names=TRUE) is a named vector. In a future version of jsonlite, this option will not be supported, and named vectors will be translated into arrays instead of objects. If you want JSON object output, please use a named list instead. See ?toJSON. Warning: Error in geom_histogram: Problem while computing aesthetics. ℹ Error occurred in the 1st layer. Caused by error: ! object 'Value' not found
It looks like what my first function return is an empty dataset, however I do not understand why it's the case?