我正在尝试绘制存储在MySQL Server中的表,并提供选项以在变量之间进行选择。但是当我试图绘制它时,我收到一个错误:
eval(expr,envir,enclos)出错:找不到对象'x'。
我是R的新手,因此无法解决问题。
我的代码是:
library(shiny)
library(ggvis)
ui<-fluidPage(
div(),
titlePanel("Hello User"),
sidebarLayout(
sidebarPanel(
selectInput('x', 'x:' ,'x'),
selectInput('y', 'y:', 'y'),
uiOutput("plot_ui")
),
mainPanel(
ggvisOutput("plot")
)
)
)
server<-function(input, output, session) {
library(RMySQL)
options(mysql = list(
"host" = "sub**********",
"port" = 3306,
"user" = "bss",
"password" = "m******"
))
databaseName <- "niitmathlogic"
table <- "ILS_Analysis"
saveData <- function(data) {
# Connect to the database
db <- dbConnect(MySQL(), dbname = databaseName, host = options()$mysql$host,
port = options()$mysql$port, user = options()$mysql$user,
password = options()$mysql$password)
# Construct the update query by looping over the data fields
query <- sprintf(
"INSERT INTO %s (%s) VALUES ('%s')",
table,
paste(names(data), collapse = ", "),
paste(data, collapse = "', '")
)
# Submit the update query
dbGetQuery(db, query)
}
loadData <- function() {
# Connect to the database
db <- dbConnect(MySQL(), dbname = databaseName, host = options()$mysql$host,
port = options()$mysql$port, user = options()$mysql$user,
password = options()$mysql$password)
# Construct the fetching query
query <- sprintf("select case when city_dd = 'Bangalore' then 'Bangalore'
when city_dd = 'New Delhi' then 'New Delhi'
when city_dd = 'Chennai' then 'Chennai'
when city_dd = 'Mumbai' then 'Mumbai'
when city_dd = 'Kolkata' then 'Kolkata'
else 'Others' end as type, count(*) as leads, sum(conversion_flag) as conversion
from %s where duration_2flag =1 group by type", table)
# Submit the fetch query
data <- dbGetQuery(db, query)
data
}
observe({
data<-loadData()
updateSelectInput(session, 'x', choices = names(data))
updateSelectInput(session, 'y', choices = names(data))
}) # end observe
#gets the y variable name, will be used to change the plot legends
yVarName<-reactive({
input$y
})
#gets the x variable name, will be used to change the plot legends
xVarName<-reactive({
input$x
})
filteredData<-reactive({
data<-isolate(loadData())
#if there is no input, make a dummy dataframe
if(input$x=="x" && input$y=="y"){
if(is.null(data)){
data<-data.frame(x=0,y=0)
}
}else{
data<-data[,c(input$x,input$y)]
names(data)<-c("x","y")
}
data
})
vis<-reactive({
plotData<-filteredData()
plotData %>%
ggvis(~x, ~y) %>%
layer_points() %>%
add_axis("y", title = yVarName()) %>%
add_axis("x", title = xVarName()) %>%
add_tooltip(function(df) format(sqrt(df$x),digits=2))
})
vis%>%bind_shiny("plot", "plot_ui")
}
shinyApp(ui,server)
真的很感激任何帮助吗?