【发布时间】:2015-01-23 03:48:19
【问题描述】:
我需要根据从侧边栏中选择的输入更改 mydb(string) 值。
ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Shiny App"),
sidebarLayout(
sidebarPanel( selectInput("site",
label = "Choose a site for Analysis",
choices = c("abc", "def",
"ghi", "jkl"),
selected = "abc")
),
mainPanel(
textOutput("text"),
)
))
服务器.R
library(shiny)
library(ggplot2)
library(RMySQL)
shinyServer(function(input, output) {
if(input$site=="abc"){
mydb<-"testdb_abc"}
else if(input$site=="def"){
mydb<-"testdb_def"}
con <- dbConnect(MySQL(),dbname=mydb, user="root", host="127.0.0.1", password="root")
query <- function(...) dbGetQuery(con, ...)
output$text <- renderText({
paste("You have selected:",input$site)
})
})
在上面的 server.R 中,我需要根据选定的输入为 mydb 分配字符串值。我收到此错误:
Error in .getReactiveEnvironment()$currentContext() :
Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
我怎样才能在闪亮的反应中做到这一点?
【问题讨论】:
-
您必须将所有 if 语句放入反应式表达式或观察。所以在粘贴之前将 if 和 else if 放入 renderText
-
@pops 我应该在 con