aes_string() 是否更改了 R 中的任何默认设置? R Shiny 和 ggplot input$ 交互的问题
Does aes_string() change any default settings in R? A problem with R Shiny and ggplot input$ interaction
我正在 Rstudio 中创建一个 Shiny 应用程序,但我无法将 input$char 插入到 ggplot 箱线图中,其中 input$char 是 y 轴的变量,而 x 轴不是输入变量,而是来自数据框。问题是数据不会出现在图表中,但下拉菜单的输入功能仍然会在加载应用程序时更改图表中的 y 轴。
示例代码:
g = reactive({ggplot(data=iris, aes(x=Species, y=input$ybox))+ theme(legend.position = "top")})
output$Boxplot = renderPlot(g() + geom_boxplot(aes(fill=Species)) + scale_fill_brewer(palette
= "Set2"))
我通过这里的另一个 post 发现 aes_string() 可以解决问题,而且确实如此。 aes 在 ggplot 中的位置,我用 aes_string() 修复了错误并让函数中的 x="Species"....
除了现在我的所有其他变量在我闪亮的应用程序的其余部分中都无法识别!在任何有 Species 或其他 input$char 变量的地方,它们都无法识别,我会收到错误消息。未找到对象 'Species',或未使用的参数 'input$char;,...,'Species'.
给出错误的示例代码:
selection = reactive({iris[, aes(c(input$xcol, input$ycol, 'Species'))]})
fit = reactive({knn3(Species ~ ., data=selection(), k = input$K)})
output$KNNplot = renderPlot({decisionplot(fit(), selection(), class=Species,main=paste('Decision Boundary for KNN (',input$K,')', sep=' '))})
有什么方法既可以解决 ggplot 错误,又可以让我的应用程序的其余部分继续工作?
编辑(可重现代码):
library(shiny)
library(shinyWidgets)
library(lattice)
library(ggplot2)
library(RColorBrewer)
library(nnet)
library(naivebayes)
library(e1071)
library(randomForest)
library(MASS)
# function for decisionplot.R
decisionplot <- function(model, data, class = NULL, predict_type = "class",
resolution = 100, showgrid = TRUE, ...) {
if(!is.null(class)) cl <- data[,class] else cl <- 1
data <- data[,1:2]
k <- length(unique(cl))
plot(data, col = as.integer(cl)+1L, pch = as.integer(cl)+1L, ...)
# make grid
r <- sapply(data, range, na.rm = TRUE)
xs <- seq(r[1,1], r[2,1], length.out = resolution)
ys <- seq(r[1,2], r[2,2], length.out = resolution)
g <- cbind(rep(xs, each=resolution), rep(ys, time = resolution))
colnames(g) <- colnames(r)
g <- as.data.frame(g)
### guess how to get class labels from predict
### (unfortunately not very consistent between models)
p <- predict(model, g, type = predict_type)
if(is.list(p)) p <- p$class
p <- as.factor(p)
if(showgrid) points(g, col = as.integer(p)+1L, pch = ".")
z <- matrix(as.integer(p), nrow = resolution, byrow = TRUE)
contour(xs, ys, z, add = TRUE, drawlabels = FALSE,
lwd = 2, levels = (1:(k-1))+.5)
invisible(z)
}
ui = fluidPage(
navlistPanel(
tabPanel("Descriptive Statistics",
fluidRow(
column(6,
wellPanel(selectInput('ybox', 'Y Variable', names(iris)[1:4], selected = names(iris)[[2]])),
plotOutput('Boxplot'))),
fluidRow(
column(12,
wellPanel(selectInput('xdens', 'X Variable', names(iris)[1:4], selected= names(iris)[2])),
plotOutput("Densplot")))),
tabPanel("Naive Bayes Classifier",
wellPanel(selectInput('xcol5', 'X Variable', names(iris)[1:4], selected = names(iris)[[1]]),
selectInput('ycol5', 'Y Variable', names(iris)[1:4], selected = names(iris)[[2]])),
plotOutput('NBplot'))
))
server = function(input, output) {
# Boxplot
g = reactive({ggplot(data=iris, aes_(x='Species', y=iris[[as.name(input$ybox)]]))+ theme(legend.position = "top")})
output$Boxplot = renderPlot(g() + geom_boxplot(aes(fill=Species)) + scale_fill_brewer(palette = "Set2") + ylab(input$ybox))
# Densityplot
c = reactive({ggplot(iris, aes_(iris[[as.name(input$xdens)]]))})
mu = reactive({ddply(iris, "Species", summarise, grp.mean=mean(c()))})
dc = reactive({c() + geom_density(kernel="gaussian", aes(color=Species, fill=Species), alpha=0.4) +
geom_vline(data=mu(), aes(xintercept=mu()$grp.mean, color=Species),linetype="dashed") +
theme(legend.position = "top") + xlab(input$xdens)})
output$Densplot = renderPlot({dc() + scale_color_brewer(palette="Dark2") + scale_fill_brewer(palette = "Dark2")})
#example model
selection5 = reactive({iris[, c(input$xcol5, input$ycol5, 'Species')]})
fit5 = reactive({naive_bayes(Species ~ ., data=selection5(), usekernel = T)})
output$NBplot = renderPlot({decisionplot(fit5(), selection5(), class = "Species",
main = "Decision Boundary for Naive Bayes Classifier")})
}
shinyApp(ui = ui, server = server)
你的 selection5
是个问题。下面的代码给出了一个响应式数据框。
#example model
selection5 <- reactive({
# df <- iris[, c(input$xcol5, input$ycol5, 'Species')] ## this call does not work
df <- data.frame(x=iris[[input$xcol5]], y=iris[[input$ycol5]], Species=iris[, "Species"])
})
我正在 Rstudio 中创建一个 Shiny 应用程序,但我无法将 input$char 插入到 ggplot 箱线图中,其中 input$char 是 y 轴的变量,而 x 轴不是输入变量,而是来自数据框。问题是数据不会出现在图表中,但下拉菜单的输入功能仍然会在加载应用程序时更改图表中的 y 轴。 示例代码:
g = reactive({ggplot(data=iris, aes(x=Species, y=input$ybox))+ theme(legend.position = "top")})
output$Boxplot = renderPlot(g() + geom_boxplot(aes(fill=Species)) + scale_fill_brewer(palette
= "Set2"))
我通过这里的另一个 post 发现 aes_string() 可以解决问题,而且确实如此。 aes 在 ggplot 中的位置,我用 aes_string() 修复了错误并让函数中的 x="Species"....
除了现在我的所有其他变量在我闪亮的应用程序的其余部分中都无法识别!在任何有 Species 或其他 input$char 变量的地方,它们都无法识别,我会收到错误消息。未找到对象 'Species',或未使用的参数 'input$char;,...,'Species'.
给出错误的示例代码:
selection = reactive({iris[, aes(c(input$xcol, input$ycol, 'Species'))]})
fit = reactive({knn3(Species ~ ., data=selection(), k = input$K)})
output$KNNplot = renderPlot({decisionplot(fit(), selection(), class=Species,main=paste('Decision Boundary for KNN (',input$K,')', sep=' '))})
有什么方法既可以解决 ggplot 错误,又可以让我的应用程序的其余部分继续工作?
编辑(可重现代码):
library(shiny)
library(shinyWidgets)
library(lattice)
library(ggplot2)
library(RColorBrewer)
library(nnet)
library(naivebayes)
library(e1071)
library(randomForest)
library(MASS)
# function for decisionplot.R
decisionplot <- function(model, data, class = NULL, predict_type = "class",
resolution = 100, showgrid = TRUE, ...) {
if(!is.null(class)) cl <- data[,class] else cl <- 1
data <- data[,1:2]
k <- length(unique(cl))
plot(data, col = as.integer(cl)+1L, pch = as.integer(cl)+1L, ...)
# make grid
r <- sapply(data, range, na.rm = TRUE)
xs <- seq(r[1,1], r[2,1], length.out = resolution)
ys <- seq(r[1,2], r[2,2], length.out = resolution)
g <- cbind(rep(xs, each=resolution), rep(ys, time = resolution))
colnames(g) <- colnames(r)
g <- as.data.frame(g)
### guess how to get class labels from predict
### (unfortunately not very consistent between models)
p <- predict(model, g, type = predict_type)
if(is.list(p)) p <- p$class
p <- as.factor(p)
if(showgrid) points(g, col = as.integer(p)+1L, pch = ".")
z <- matrix(as.integer(p), nrow = resolution, byrow = TRUE)
contour(xs, ys, z, add = TRUE, drawlabels = FALSE,
lwd = 2, levels = (1:(k-1))+.5)
invisible(z)
}
ui = fluidPage(
navlistPanel(
tabPanel("Descriptive Statistics",
fluidRow(
column(6,
wellPanel(selectInput('ybox', 'Y Variable', names(iris)[1:4], selected = names(iris)[[2]])),
plotOutput('Boxplot'))),
fluidRow(
column(12,
wellPanel(selectInput('xdens', 'X Variable', names(iris)[1:4], selected= names(iris)[2])),
plotOutput("Densplot")))),
tabPanel("Naive Bayes Classifier",
wellPanel(selectInput('xcol5', 'X Variable', names(iris)[1:4], selected = names(iris)[[1]]),
selectInput('ycol5', 'Y Variable', names(iris)[1:4], selected = names(iris)[[2]])),
plotOutput('NBplot'))
))
server = function(input, output) {
# Boxplot
g = reactive({ggplot(data=iris, aes_(x='Species', y=iris[[as.name(input$ybox)]]))+ theme(legend.position = "top")})
output$Boxplot = renderPlot(g() + geom_boxplot(aes(fill=Species)) + scale_fill_brewer(palette = "Set2") + ylab(input$ybox))
# Densityplot
c = reactive({ggplot(iris, aes_(iris[[as.name(input$xdens)]]))})
mu = reactive({ddply(iris, "Species", summarise, grp.mean=mean(c()))})
dc = reactive({c() + geom_density(kernel="gaussian", aes(color=Species, fill=Species), alpha=0.4) +
geom_vline(data=mu(), aes(xintercept=mu()$grp.mean, color=Species),linetype="dashed") +
theme(legend.position = "top") + xlab(input$xdens)})
output$Densplot = renderPlot({dc() + scale_color_brewer(palette="Dark2") + scale_fill_brewer(palette = "Dark2")})
#example model
selection5 = reactive({iris[, c(input$xcol5, input$ycol5, 'Species')]})
fit5 = reactive({naive_bayes(Species ~ ., data=selection5(), usekernel = T)})
output$NBplot = renderPlot({decisionplot(fit5(), selection5(), class = "Species",
main = "Decision Boundary for Naive Bayes Classifier")})
}
shinyApp(ui = ui, server = server)
你的 selection5
是个问题。下面的代码给出了一个响应式数据框。
#example model
selection5 <- reactive({
# df <- iris[, c(input$xcol5, input$ycol5, 'Species')] ## this call does not work
df <- data.frame(x=iris[[input$xcol5]], y=iris[[input$ycol5]], Species=iris[, "Species"])
})