2016-10-10 69 views
0

我想在闪亮,但反应状态的动态SQL查询使用selectInput使用[R闪亮selectInput似乎出差错:不允许在动态查询

操作,而不积极反应的情况下。

我实际使用RODBC与SQL查询,但是这是一个重复的例子,试图(你试图做一些事情,只能从被动式或观察者内完成)。

服务器:

data(citytemp, package = "highcharter") 

function(input, output) { 
    getCityData <- function(selectedCity) { 
    return(citytemp[[selectedCity]]) 

    # hardcoded : 
    # return(citytemp$tokyo) 

    # dynamic sql query 
    # sql <- paste0("select * from cities where name = ", selectedCity) 
    } 

    cityData <- getCityData(input$cityFilter) 

    #render highchart with cityData 

} 

UI:

library("shiny") 
library("shinydashboard") 

selectInput("cityFilter", label = "City", choices = list("Tokyo" = "tokyo", "London" = "london", "Berlin" = "berlin")) 
box(width = 6, highchartOutput("highchart")) 

回答

1

基本示例,请务必注明reactive({getCityData(input$cityFilter)})

library(shiny) 
library(shinydashboard) 
library(highcharter) 
ui <- dashboardBody(
    selectInput("cityFilter", label = "City", choices = list("Tokyo" = "tokyo", "London" = "london", "Berlin" = "berlin")), 
    box(width = 6, highchartOutput("highchart")) 
) 
server = function(input, output){ 
    data(citytemp, package = "highcharter") 
    getCityData <- function(selectedCity) { 
    return(citytemp[[selectedCity]]) 
    # hardcoded : 
    # return(citytemp$tokyo) 
    # dynamic sql query 
    # sql <- paste0("select * from cities where name = ", selectedCity) 
    } 
    cityData <- reactive({getCityData(input$cityFilter)}) 
    output$highchart <- renderHighchart({ 
    selectedCityData <- cityData() 
    print("selected city data is") 
    print(selectedCityData) 
    hc <- highchart(type = "chart") %>% 
     hc_add_series(name = input$cityFilter, 
        data = selectedCityData ) 
    theme <- hc_theme_null() 
    hc <- hc %>% hc_add_theme(theme) 
    return(hc) 
    }) 
} 
shinyApp(ui=ui, server=server) 
+0

感谢nilsole,那selectedCityData引用是紧紧的;-) – dataphile

3

由于您使用他们必须要么在reactive表达或observer客户端输入,试试这个:

cityData <- reactive({getCityData(input$cityFilter)}) 
    output$highchart <- renderHighchart({cityData()})