rCharts 中的反应式数据框
Reactive dataframe in rCharts
我会让我的 rChart 函数考虑根据 UI 中选择的参数子集化的反应性数据框。不幸的是,输出 returns 总是完全相同的图,忽略了反应性数据帧 dt3
。
这里是一个代表通常输入的屏幕和一个它应该考虑的子集数据帧的例子。完整的数据集是 here on GitHub.
如您所见,数据帧 dt3
已正确子集化,但 rCharts 忽略它,始终返回相同的图。
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
})
})
我知道这种图表的常见问题(例如 renderChart2
的使用),但由于它永远不会改变,我不知道如何操作,即使有来自rCharts page.
这是会话信息:
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
如果语句与 ui 的输入不匹配,请将 if() else if()
更改为以下内容:
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')
}
我会让我的 rChart 函数考虑根据 UI 中选择的参数子集化的反应性数据框。不幸的是,输出 returns 总是完全相同的图,忽略了反应性数据帧 dt3
。
这里是一个代表通常输入的屏幕和一个它应该考虑的子集数据帧的例子。完整的数据集是 here on GitHub.
如您所见,数据帧 dt3
已正确子集化,但 rCharts 忽略它,始终返回相同的图。
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
})
})
我知道这种图表的常见问题(例如 renderChart2
的使用),但由于它永远不会改变,我不知道如何操作,即使有来自rCharts page.
这是会话信息:
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
如果语句与 ui 的输入不匹配,请将 if() else if()
更改为以下内容:
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')
}