我最近开始研究闪亮,对于我的学校项目,我正在开发一个闪亮的应用程序,它使用 ggplot 在选定的输入上绘制图形。
现在,我的要求之一是,在前端选择某些输入后,我必须使用某些数据集绘制图形。假设我有 3 个不同的数据集,然后有 4 个输入,
if(input$A = 1 && input$B = 2 && input$C = 3 && input$D = 4){
//Plot using Dataset1
}
else if(input$A = 1 && input$B = 2 && input$C = 3 && input$D = 4){
//Plot using Dataset2
}
else if(input$A = 2 && input$B = 3 && input$C = 4 && input$D = 4){
//Plot using Dataset3
}
我发现要使这种逻辑起作用,我需要表单中的提交按钮,我已经在网上检查了几个示例,但是正确理解了如何在我的情况下应用它。
现在,当我尝试使用 if 条件使用如下反应式绘制数据时,我得到了Error:could not find function "g"
我在这里做错了什么?以及如何在提交按钮上绘制此信息?
我将不胜感激,如果您可以查看我的代码并帮助我,我将不胜感激。
这是可用于重现示例的示例数据集-
用户界面
library(shiny)
library("RMySQL")
library(ggplot2)
library(plotly)
# Database Connection and the fetch
dataset <- read.csv("dataset.csv", header=TRUE)
dataset$X <- NULL
dataset$sex <- sub("^$", "Unknown", dataset$sex)
fluidPage(
fluidRow(
tags$head(
tags$style(HTML("
.shiny-output-error-validation {
color: #48ca3b;
font-size: 14pt;
}
body {
-moz-transform: scale(0.9, 0.9); /* Moz-browsers */
zoom: 0.9; /* Other non-webkit browsers */
zoom: 90%; /* Webkit browsers */
}
"))
),
titlePanel("Define census"),
sidebarPanel(
dateRangeInput('dateRange',
label = 'Date Input',
start = as.Date("1967-01-01"), end = Sys.Date()),
selectInput("region", label = "Region",
choices = c("All",levels(dataset$region)),
selected = "ANI"),
selectInput("species", label = "Species",
choices = c("All",levels(dataset$species)),
selected = "ANI"),
selectInput("sex", label = "Sex",
choices = unique(dataset$sex), multiple = TRUE,
selected = unique(dataset$sex)),
radioButtons(
"standard_cat_options",
label="Standard Category",
choices=list(
"All",
"Multiple Select"), selected="All"),
conditionalPanel(
condition = "input.standard_cat_options != 'All'",
selectInput(
'standard_cat',
label = "Select categories", multiple = TRUE,
choices=unique(dataset$standard_cat)
)
),
radioButtons(
"age_cat_options",
label="Age Category",
choices=list(
"All",
"Multiple Select"), selected="All"),
conditionalPanel(
condition = "input.age_cat_options != 'All'",
selectInput(
'broad_cat',
label = "Select age category", multiple = TRUE,
choices=c("adult", "adjuv", "juv", "pup", "orphan", "W", "YOY", "SA1", "SA2", "SA3", "SA4", "SA5", "SA", "mature", levels(dataset$broad_cat))
)
),
selectInput('x', 'X', names(dataset), names(dataset)[[2]]),
selectInput('y', 'Y', names(dataset), names(dataset)[[8]]),
submitButton("Submit")
),
mainPanel(
column(12, plotlyOutput("plot1")),
hr(),
column(12, plotlyOutput("plot2"))
)
)
)
更新的 Server.R
library(ggplot2)
library("RMySQL")
library("mgcv")
library(plotly)
function(input, output) {
# Database Connection and the fetch
dataset <- read.csv("dataset.csv", header=TRUE)
dataset$X <- NULL
dataset$sex <- sub("^$", "Unknown", dataset$sex)
# dataset1 <- read.csv("dataset1.csv", header = TRUE, fill = TRUE)
# dataset2 <- read.csv("dataset2.csv", header = TRUE, fill = TRUE)
# dataset3 <- read.csv("dataset3.csv", header = TRUE, fill = TRUE)
# DataBase disconnected
# Using the datafram created with name data
# as in the given data Date is of String type so converted to "Date" type
dataset$date <- as.Date(dataset$date)
#reactive variable initiation for the various inputsinstall.packages('rsconnect')
reactive({
#if(input$region == "ANI" && input$species == "Ej" && input$sex == "Unknown" && input$standard_cat == "All" && input$broad_cat == "YOY"){
if(input$region == "ANI"){
l <- subset(dataset, region %in% input$region)
k <- subset(l(), date >= as.Date(input$dateRange[1]) & date <= as.Date(input$dateRange[2]))
m <- subset(k(), species %in% input$species)
n <- subset(m(), sex %in% input$sex)
o <- subset(n(), standard_cat %in% input$standard_cat)
g <- subset(o(), broad_cat %in% input$broad_cat)
}
})
#output plots
output$plot1 <- renderPlotly({
p <- ggplot(g(), aes_string(x=input$x, y=input$y)) + geom_point(alpha=0.4)
ggplotly(p)
})
output$plot2 <- renderPlotly({
q <- ggplot(g(), aes_string(x=input$x, y=input$y)) + geom_smooth()
ggplotly(q)
})
}
谢谢你。