我正在尝试在Shiny的动态sql查询中使用selectInput但反应状态似乎出错了:
没有活动的反应上下文时,不允许操作。 (你试图做一些只能在反应式表达式或观察者内部完成的事情。)
我实际上正在使用带有sql查询的RODBC,但这是尝试重现的例子。
服务器:
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
}
用户界面:
library("shiny")
library("shinydashboard")
selectInput("cityFilter", label = "City", choices = list("Tokyo" = "tokyo", "London" = "london", "Berlin" = "berlin"))
box(width = 6, highchartOutput("highchart"))
答案 0 :(得分:3)
由于您使用的是客户输入,因此必须使用reactive
表达式或observer
,请尝试以下操作:
cityData <- reactive({getCityData(input$cityFilter)})
output$highchart <- renderHighchart({cityData()})
答案 1 :(得分: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)
答案 2 :(得分:0)
如果有人在寻找,那么这就是工作解决方案。