在动态查询中使用R-selectInput

在动态查询中使用R-selectInput,r,shiny,R,Shiny,我尝试在动态sql查询中使用selectInput,但反应状态似乎出错: 如果没有活动-反应上下文,则不允许操作。(您试图做的事情只能从被动表达式或观察者内部完成。) 我实际上是在使用RODBC进行sql查询,但这是一个可复制的示例 服务器: data(citytemp, package = "highcharter") function(input, output) { getCityData <- function(selectedCity) { return(cityt

我尝试在动态sql查询中使用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

}

由于您使用的是客户端输入,因此它们必须位于
反应式表达式或
观察者表达式中,请尝试以下操作:

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

cityData由于您使用的是客户端输入,因此它们必须在
反应式表达式中或在
观察者中,请尝试以下操作:

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

cityData基本示例,确保声明
reactive({getCityData(input$cityFilter)})

库(闪亮)
图书馆(shinydashboard)
图书馆(高级特许)

ui基本示例,确保声明
被动({getCityData(input$cityFilter)})

库(闪亮)
图书馆(shinydashboard)
图书馆(高级特许)

ui如果有人在寻找,这里有一个有效的解决方案


如果有人在寻找,这里有一个有效的解决方案


感谢nilsole,selectedCityData引用是关键;-)感谢nilsole,selectedCityData引用是关键;-)
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)