ShinyのRのプロット位置を対話的に選択する
質問
これをどのように尋ねるかは確かではありませんが、ここでそれが行く:
私はRで光沢のあるパッケージを使っています。現在は大丈夫です。
それらの間の違いを視覚的に比較するために、もう一方のグラフをプロットしたいのですが。
理想的には、ラジオボタンでプロット位置(上または下)を選択できるようにしたいと思います。入力を変更するために入力を変更するとすでに今すぐになっているプロットを生成しているので、上/下のラジオボタンでどの位置が選択されているかに表示されます。
trim_down<-function(LAB,TYPE,FORM,CLASS,AMI,DATE){
ma<<-dft
if (is.nan(TYPE)==FALSE){ma<<-subset(ma, type %in% TYPE)}
if (is.nan(FORM)==FALSE){ma<<-subset(ma, form %in% FORM)}
if (is.nan(CLASS)==FALSE){ma<<-subset(ma, class %in% CLASS)}
if (is.nan(AMI)==FALSE){ma<<-subset(ma, ami %in% AMI)}
ma<<-subset(ma, as.Date(dateStarted,"%m/%d/%Y")>=DATE[1] )
ma<<-subset(ma, as.Date(dateStarted,"%m/%d/%Y")<=DATE[2] )
dim(ma)
ma<<-ma[,-(1:length(test_factors))]
all_test_names<<-names(ma)
ma<<-as.matrix(ma)
ma<<-t(apply(ma, 1,as.numeric,na.rm=TRUE))
aa<<-1-colMeans(ma,na.rm=TRUE)
b<<-colSums(!is.na(ma))
active_test_names<<-all_test_names[!is.nan(aa)]
x<<-rbind(aa,b)
graph.me(x,all_test_names,active_test_names,trimmed_up=FALSE)
}
graph.me<-function(x,all_test_names,active_test_names,trimmed_up=TRUE){
library(reshape2)
aa<<-x[1,]
b<<-x[2,]
aa[aa==0]=-.1
aa[is.na(aa)]=0
XAXIS<<-all_test_names
success <- as.data.frame(aa)
rownames(success)<-XAXIS
samples <- as.data.frame(b)
data.long <- cbind(melt(success,id=1), melt(samples, id=1))
names(data.long) <- c("success", "count")
rownames(data.long)<-XAXIS
threshold <- 25
data.long$fill <- with(data.long,ifelse(count>threshold,max(count),count))
data.long$fill[data.long$fill>threshold]<-threshold
library(ggplot2)
library(RColorBrewer)
print(ggplot(data.long) +
geom_bar(aes(x=XAXIS, y=success, fill=fill),colour="grey70",stat="identity")+
scale_fill_gradientn(colours=brewer.pal(9,"RdYlGn")) +
theme(axis.text.x=element_text(angle=-90,hjust=0,vjust=0.4)))
}
ui.r
library(shiny)
# Define UI for miles per gallon application
shinyUI(pageWithSidebar(
# Application title
headerPanel("Example"),
sidebarPanel(
# checkboxGroupInput("_lab", "lab:",unique(dft$lab)),
checkboxGroupInput("type", "Type:",unique(dft$type),selected=unique(dft$type)),
checkboxGroupInput("form", "Form:",unique(dft$form),selected=unique(dft$form)),
checkboxGroupInput("class", "Class:",unique(dft$class),selected=unique(dft$class)),
checkboxGroupInput("ami", "AMI:",unique(dft$ami),selected=unique(dft$ami)),
dateRangeInput("daterange", "Date range:",
start = min(as.Date(dft$date,"%m/%d/%Y")),
end = max(as.Date(dft$date,"%m/%d/%Y")))
),
mainPanel(
h3(textOutput("caption")),
plotOutput("Plot")
)
))
server.r
library(shiny)
shinyServer(function(input, output) {
# Compute the forumla text in a reactive expression since it is
# shared by the output$caption and output$mpgPlot expressions
formulaText <- reactive({
paste(input$type,input$form,input$class,input$ami)
})
# Return the formula text for printing as a caption
output$caption <- renderText({
formulaText()
})
# Generate a plot of the requested variable against mpg and only
# include outliers if requested
output$Plot <- renderPlot(function(){
print(trim_down(NA,input$type,input$form,input$class,input$ami,input$daterange))
})
})
.
助けてくれてありがとう...そんなに多くのコードをお詫び申し上げます。しかし、私は誰のレビューのために省略しても安全なのかわからない。それが役立つならば、私はいくつかのレイアウトグリッドをプロットするためにGGPLOTに対処することによって問題を解決することができます。
応答に基づいて、私はこれを試してみました:
ui.r
library(shiny)
shinyUI(pageWithSidebar(
# Application title
headerPanel("Example"),
sidebarPanel(
radioButtons("plotSpot", "Position", c(1,2)),
checkboxGroupInput("type", "Type:",unique(dft$type),selected=unique(dft$type)),
checkboxGroupInput("form", "Form:",unique(dft$form),selected=unique(dft$form)),
checkboxGroupInput("class", "Class:",unique(dft$class),selected=unique(dft$class)),
checkboxGroupInput("ami", "AMI:",unique(dft$ami),selected=unique(dft$ami)),
dateRangeInput("daterange", "Date range:",
start = min(as.Date(dft$date,"%m/%d/%Y")),
end = max(as.Date(dft$date,"%m/%d/%Y")))
),
mainPanel(
plotOutput("topPlot"),
plotOutput("bottomPlot")
)
))
server.r
library(shiny)
p<-list()
output$Plot <- renderPlot({
p[input$plotSpot]<-trim_down(NA,input$type,input$form,input$class,input$ami,input$daterange)
output$topPlot <- renderPlot(ifelse(input$position == "Top", print(p[1]), print(p[2])))
output$bottomPlot <- renderPlot(ifelse(input$position == "Top", print(p[2]), print(p[1])))
})
})
.
しかし、それは1つのグラフのみを作るだけです。私はGGPLOTを保持しているリストのインデックスを切り替えてから、同じ順序をグラフ化された順序を同じにしてくださいが、運がありませんが、運がありません。
解決
OK、私はそのすべてのコードを通して仕事をするつもりはなかったが、これはあなたが望むものをするかもしれない例です。誤解した場合は、最小限のの例で再投稿してみてください。
ui.r
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Plot position"),
sidebarPanel(
radioButtons("position", "Position", c("Top", "Bottom"))),
mainPanel(
plotOutput("topPlot"),
plotOutput("bottomPlot"))))
.
server.r
library(shiny)
library(ggplot2)
dat <- data.frame(A = 1:10, B = rnorm(10))
shinyServer(function(input, output) {
p1 <- ggplot(dat, aes(A, B)) + geom_point(colour = "red")
p2 <- ggplot(dat, aes(A, B)) + geom_path(colour = "blue")
output$topPlot <- renderPlot(ifelse(input$position == "Top", print(p1), print(p2)))
output$bottomPlot <- renderPlot(ifelse(input$position == "Top", print(p2), print(p1)))
})
.
編集
問題の新しい説明に従って、次のことが役立ちます。私はそれを複雑すぎると思います - 設定が異なるプロットに対して異なる予定がある場合は、さまざまなプロットごとに入力してください。一組のセットを持つことは可能ですが、劇的に複雑さを増やすでしょう。
ui.r
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Plot position"),
sidebarPanel(
h2("Top plot settings"),
radioButtons("topPlotColour", "Colour:",
list("Blue" = "blue",
"Red" = "red")),
radioButtons("topPlotGeom", "Geom:",
list("Point" = "point",
"Line" = "line")),
h2("Bottom plot settings"),
radioButtons("bottomPlotColour", "Colour:",
list("Blue" = "blue",
"Red" = "red")),
radioButtons("bottomPlotGeom", "Geom:",
list("Point" = "point",
"Line" = "line"))),
mainPanel(
plotOutput("topPlot"),
plotOutput("bottomPlot"))))
.
server.r
library(shiny)
library(ggplot2)
dat <- data.frame(A = 1:10, B = rnorm(10))
shinyServer(function(input, output) {
p1_geom <-reactive({
geom <- switch(input$topPlotGeom,
point = geom_point(colour = input$topPlotColour),
line = geom_line(colour = input$topPlotColour))
})
p2_geom <-reactive({
geom <- switch(input$bottomPlotGeom,
point = geom_point(colour = input$bottomPlotColour),
line = geom_line(colour = input$bottomPlotColour))
})
p1_colour <- reactive({input$topPlotColour})
output$topPlot <- renderPlot({print(ggplot(dat, aes(A, B), colour = p1_colour()) + p1_geom())})
output$bottomPlot <- renderPlot(print(ggplot(dat, aes(A, B), colour = toString(quote(input$bottomPlotColour))) + p2_geom()))
})
.