我遇到了一些问题,将ggplot2应用到闪亮的应用程序
我无法在下面给出的示例中获取代码。由于某些原因,当我使用aes_string
时,它找不到factor1
,当我使用aes
时,它找不到params()
。在文件中,使用我的数据,此步骤有效。对不起。
我想用qqplot2绘制我的数据 项目。当我获取数据并将它们绘制在闪亮的应用程序之外时, 一切都好。当我 在闪亮的应用程序中实现代码,它从一个阶乘中改变我的轴 一个有争议的。
我正在改变因素
x轴根据用户输入带有“radioButtons”。当我
使用stripchart(),它完美地运行。当我使用ggplot代替它时
首先工作正常,但是当我更改因子(更改radioButton的值)时,它找不到输入对象(input$strain
)。
对不起,很长的帖子。非常感激您的帮忙。
这是我的代码:
ui.R
library(shiny)
# Define UI for dataset viewer application
ui<-(fluidPage(
titlePanel("My project"),
sidebarLayout(
sidebarPanel(
selectInput("dataset", "Choose a dataset:",
choices = c("exp1","exp2", "exp3"),selected="exp1"),
selectInput(inputId = "param",choices = c("Repsonse A","Response B"),label = "Choose repsonse",selected="Repsonse A"),
selectInput("factors", "Choose grouping factor:",
choices = c("No grouping","Block"),selected="No grouping"),
radioButtons(inputId = "fac",choice=c("factor1 on x-axis","factor2 on x-axis"), "Choose type of x-axis")
),
mainPanel(
h3(textOutput("caption", container = span)),
plotOutput("strip")
)
)
))
和 server.R
library(shiny)
library(datasets)
library(ggplot2)
server<-(function(input, output,clientData, session) {
df<-data.frame("respondA"=runif(20, min=0, max=0.4),"respondB"=runif(20, min=0, max=0.4),"factor1"="A","factor2"="B","replicate"=as.factor(c(1:20)))
df2<-data.frame("respondA"=runif(20, min=0, max=0.4),"respondB"=runif(20, min=0, max=0.4),"factor1"="A","factor2"="B","replicate"=as.factor(c(1:20)))
df3<-data.frame("respondA"=runif(20, min=0, max=0.4),"respondB"=runif(20, min=0, max=0.4),"factor1"="A","factor2"="B","replicate"=as.factor(c(1:20)))
datasetInput <- reactive({
switch(input$dataset,
"exp1" = df,
"exp2" = df2,
"exp3" = df3)
})
params <- reactive({
switch(input$param,
"Repsonse A" = datasetInput()$respondA,
"Response B" = datasetInput()$respondB)
})
factor <- reactive({
switch(input$factors,
"No grouping"=NULL,
"Block" = "replicate")
})
output$strip <- renderPlot({
if (input$fac == "factor1 on x-axis") {
p<-ggplot(datasetInput(), aes_string(x = factor1, y = params())) +
ylab("Mylabel1")+
xlab("Mylabel2")+
geom_jitter(position = position_jitter(width = .2),shape=19,size=5,aes_string(colour = factor()))+
labs(title = "Mytitle")+
theme_bw() +
theme(axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank())
print(p)
} else if(input$fac == "factor2 on x-axis") {
q<-ggplot(datasetInput(), aes_string(x = factor2, y = params())) +
ylab("Mylabel1")+
xlab("Mylabel2")+
geom_jitter(position = position_jitter(width = .2),shape=19,size=5,aes_string(colour = factor()))+
labs(title = "Mytitle")+
theme_bw() +
theme(axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank())
print(q)
}
})
})
shinyApp(ui=ui,server=server)
Ps:我想使用ggplot,因为我可以很容易地根据一个因素改变数据的颜色。对于某些因素,我有&gt; 20个因子级别,并且stripchart()
和add=T
答案 0 :(得分:1)
由于你使用因子1和因子2的aes_string,它们必须是字符串。这对我有用:
library(shiny)
# Define UI for dataset viewer application
ui<-(fluidPage(
titlePanel("My project"),
sidebarLayout(
sidebarPanel(
selectInput("dataset", "Choose a dataset:",
choices = c("exp1","exp2", "exp3"),selected="exp1"),
selectInput(inputId = "param",choices = c("Repsonse A","Response B"),label = "Choose repsonse",selected="Repsonse A"),
selectInput("factors", "Choose grouping factor:",
choices = c("No grouping","Block"),selected="No grouping"),
radioButtons(inputId = "fac",choice=c("factor1 on x-axis","factor2 on x-axis"), "Choose type of x-axis")
),
mainPanel(
h3(textOutput("caption", container = span)),
plotOutput("strip")
)
)
))
library(shiny)
library(datasets)
library(ggplot2)
server<-(function(input, output,clientData, session) {
df<-data.frame("respondA"=runif(20, min=0, max=0.4),"respondB"=runif(20, min=0, max=0.4),"factor1"="A","factor2"="B","replicate"=as.factor(c(1:20)))
df2<-data.frame("respondA"=runif(20, min=0, max=0.4),"respondB"=runif(20, min=0, max=0.4),"factor1"="A","factor2"="B","replicate"=as.factor(c(1:20)))
df3<-data.frame("respondA"=runif(20, min=0, max=0.4),"respondB"=runif(20, min=0, max=0.4),"factor1"="A","factor2"="B","replicate"=as.factor(c(1:20)))
datasetInput <- reactive({
switch(input$dataset,
"exp1" = df,
"exp2" = df2,
"exp3" = df3)
})
params <- reactive({
switch(input$param,
"Repsonse A" = datasetInput()$respondA,
"Response B" = datasetInput()$respondB)
})
factor <- reactive({
switch(input$factors,
"No grouping"=NULL,
"Block" = "replicate")
})
output$strip <- renderPlot({
if (input$fac == "factor1 on x-axis") {
p<-ggplot(datasetInput(), aes_string(x = 'factor1', y = params())) +
ylab("Mylabel1")+
xlab("Mylabel2")+
geom_jitter(position = position_jitter(width = .2),shape=19,size=5,aes_string(colour = factor()))+
labs(title = "Mytitle")+
theme_bw() +
theme(axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank())
print(p)
} else if(input$fac == "factor2 on x-axis") {
q<-ggplot(datasetInput(), aes_string(x = 'factor2', y = params())) +
ylab("Mylabel1")+
xlab("Mylabel2")+
geom_jitter(position = position_jitter(width = .2),shape=19,size=5,aes_string(colour = factor()))+
labs(title = "Mytitle")+
theme_bw() +
theme(axis.line = element_line(colour = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank())
print(q)
}
})
})
shinyApp(ui=ui,server=server)