2016-10-10 70 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()})