rCharts中的反应性数据框

时间:2016-04-21 08:14:49

标签: r shiny rcharts

我会让我的rChart函数根据UI中选择的参数来考虑一个被动数据帧子集。不幸的是,输出总是返回相同的图,忽略了反应数据帧dt3。 这里是一个代表通常输入的屏幕,以及它应该考虑的子集化数据帧的一个例子。完整的数据集是here on GitHub

enter image description here

正如您所看到的,数据框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),但由于它永远不会改变,我不知道如何采取行动,即使有{{1}的帮助和问题3}}

以下是会话信息:

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

1 个答案:

答案 0 :(得分:2)

如果声明与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')
    }