Reputation: 4037
I would make my rChart functions consider a reactive dataframe subsetted according to the parameters selected in the UI. Unfortunately, the output returns always the very same plot, ignoring the reactive dataframe dt3
.
Here a screen representing the usual input and an example of the subsetted dataframe it should consider. The complete dataset is here on GitHub.
As you can see, the dataframe dt3
is correctly subsetted, but rCharts ignores it, returning always that very same plot.
ui.R
library(shiny)
library(rCharts) # rCharts_0.4.2
options(RCHART_LIB = 'nvd3')
shinyUI(fluidPage(
titlePanel("Alpine weather stations"),
sidebarLayout(
sidebarPanel(
selectInput(inputId = 'station', lab = "Station",
choices = sort(as.character(dt$Weather_station)),
selected = NULL),
sliderInput(inputId = "year", lab = "Years:",
min = 1980,
max = 2011,
value = c(1980, 1985)),
checkboxGroupInput(inputId = "data", lab = "Select",
choices = c("Mean temperature" = "meanTemp",
"Precipitation" = "precipitation"))
),
mainPanel(
showOutput("chart1", "nvd3")
#dataTableOutput("dt3") #Used for the example screenshot
)
)
))
server.R
library(shiny)
library(rCharts) # rCharts_0.4.2
options(RCHART_LIB = 'nvd3')
dt <- read.table("precipitation_temperature_data.txt", header = TRUE, dec = ".", stringsAsFactors = FALSE)
shinyServer(function(input, output) {
#output$dt3 <- renderDataTable({ # Used to create the screenshot subset
dt3 <- reactive({ # Used to create reactive dataframe
range <- input$year[1] : input$year[2]
station2 <- input$station
dt2 <- dt[dt$Weather_station == unlist(station2) & dt$Year %in% range, ]
dt2 <- as.data.frame(dt2)
dt2
})
output$chart1 <- renderChart({
validate(
need(input$data != "", "Please select your data.")
)
if(input$data == "Precipitation"){
n2 <- nPlot(Precipitation ~ Year, data = dt3(), group = "Weather_station", type = 'multiBarChart')
}else if(input$data == "Mean_temperature"){
n2 <- nPlot(Mean_temperature ~ Year, data = dt3(), group = "Weather_station", type = 'multiBarChart')
}
n2$set(dom = 'chart1')
n2
})
})
I am aware of the usual problems with this kind of chart (such the use of renderChart2
) but since it never changes, I do not know how to act, even with the help and question from the rCharts page.
Here is the session info:
sessionInfo()
R version 3.2.4 (2016-03-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
locale:
[1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252 LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C LC_TIME=English_United Kingdom.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] plyr_1.8.3 tools_3.2.4 whisker_0.3-2 yaml_2.1.13 Rcpp_0.12.4 rCharts_0.4.2 RJSONIO_1.3-0
[8] grid_3.2.4 lattice_0.20-33
Upvotes: 1
Views: 175
Reputation: 56004
If statement doesn't match input from ui, change if() else if()
to below:
if(input$data == "precipitation"){
n2 <- nPlot(Precipitation ~ Year, data = dt3(), group = "Weather_station", type = 'multiBarChart')
} else if(input$data == "meanTemp"){
n2 <- nPlot(Mean_temperature ~ Year, data = dt3(), group = "Weather_station", type = 'multiBarChart')
}
Upvotes: 2