我想仅在用户点击操作按钮时更改颜色参数的值,但在shinyApp的开头我想要默认颜色。
我明白了:
library(shiny)
library(ggplot2)
#data change only once at the begenin
dt <- data.frame(x=runif(100), y=runif(100))
ui <- fluidPage(
sliderInput("slider1","slider1", value=5, min=1, max=10),
sliderInput("slider2","slider2", value=0.8, min=0, max=1),
radioButtons("check1","check1",choices = c("red","blue","green")),
actionButton("refreshColours","refreshColours"),
plotOutput("rys")
)
server <- function(input,output){
col1 <- eventReactive(input$refreshColours,{input$check1})
pp <- reactive({ ggplot(dt, aes(x,y)) +
geom_point(size=input$slider1,
alpha=input$slider2, colour=col1()) })
output$rys <- renderPlot({ pp() }) }
shinyApp(ui=ui, server=server)
我只是想看看shinyApp开头的情节,而没有点击动作按钮,所以我需要默认值在&#34;颜色&#34;在begening变量。
答案 0 :(得分:1)
你可以简单地在ggplot中为初始状态设置一个默认值:
pp <- reactive({ ggplot(dt, aes(x,y)) +
geom_point(size=input$slider1,
alpha=input$slider2,
colour= if(input$refreshColours == 0) "red" else col1() )
})
答案 1 :(得分:1)
得到你想要的东西你可以完全消除第一个col()
,我将默认颜色设置为橙色
library(shiny)
library(ggplot2)
#data change only once at the begenin
dt <- data.frame(x=runif(100), y=runif(100))
defaultcolor <- "orange"
ui <- fluidPage(
sliderInput("slider1","slider1", value = 5, min=1, max=10),
sliderInput("slider2","slider2", value = 0.8, min=0, max=1),
radioButtons("check1","check1",choices = c("red","blue","green")),
actionButton("refreshColours","refreshColours"),
plotOutput("rys")
)
server <- function(input,output){
pp <- reactive({
input$refreshColours
if(input$refreshColours == 0){
return(ggplot(dt, aes(x,y)) + geom_point(size=input$slider1, alpha=input$slider2, colour= defaultcolor))
}
ggplot(dt, aes(x,y)) + geom_point(size=input$slider1, alpha=input$slider2, colour=input$check1)
})
output$rys <- renderPlot({ pp() }) }
shinyApp(ui=ui, server=server)
示例2 :如果您还想使用col()
被动,也可以这样做:
library(shiny)
library(ggplot2)
#data change only once at the begenin
dt <- data.frame(x=runif(100), y=runif(100))
defaultcolor <- "orange"
ui <- fluidPage(
sliderInput("slider1","slider1", value = 5, min=1, max=10),
sliderInput("slider2","slider2", value = 0.8, min=0, max=1),
radioButtons("check1","check1",choices = c("red","blue","green")),
actionButton("refreshColours","refreshColours"),
plotOutput("rys")
)
server <- function(input,output){
col1 <- reactive({
input$refreshColours
if(input$refreshColours ==0){
return(defaultcolor)
}
input$check1})
pp <- reactive({
ggplot(dt, aes(x,y)) + geom_point(size=input$slider1, alpha=input$slider2, colour=col1())
})
output$rys <- renderPlot({ pp() }) }
shinyApp(ui=ui, server=server)
答案 2 :(得分:0)
除了这里给出的答案是 refreshColors
的数值条件,您可以在调用中将 ignoreNULL = FALSE
设置为 eventReactive
,并指定 selected = "blue"
(或您喜欢的任何颜色)在对单选按钮的调用中。
#this is basically the code from the question with the above changes added
library(shiny)
library(ggplot2)
#data change only once at the begenin
dt <- data.frame(x=runif(100), y=runif(100))
ui <- fluidPage(
sliderInput("point_size","Point Size", value=5, min=1, max=10),
sliderInput("point_alpha","Point Alpha", value=0.8, min=0, max=1),
#here is where you select the default value with selected
radioButtons("point_color","Point Color",choices = c("red","blue","green"),selected = "blue"),
actionButton("refreshColours","Refresh Colors"),
plotOutput("plot_out")
)
server <- function(input,output){
#set igoreNULL = FALSE here
col1 <- eventReactive( input$refreshColours,{input$point_color},ignoreNULL = FALSE)
pp <- reactive({ ggplot(dt, aes(x,y)) +
geom_point(size=input$point_size,
alpha=input$point_alpha, colour=col1()) })
output$plot_out <- renderPlot({ pp() }) }
shinyApp(ui=ui, server=server)
您还可以在对 radioButtons 的调用中使用 selected = character(0)
以便不选择任何单选按钮来启动,并使用一个函数(下面的color_with_default
)返回您选择的默认颜色,如果radioButtons 中的值为 null,因为它是在没有选择时。该默认颜色用于应用启动时呈现的初始绘图中,因为 ignoreNULL
在调用 eventReactive
时为假,就像在前面的示例中一样。
library(shiny)
library(ggplot2)
#data change only once at the begenin
dt <- data.frame(x=runif(100), y=runif(100))
ui <- fluidPage(
sliderInput("point_size","Point Size", value=5, min=1, max=10),
sliderInput("point_alpha","Point Alpha", value=0.8, min=0, max=1),
#use selected = character(0) to start with nothing selected
radioButtons("point_color","Point Color",choices = c("red","blue","green"),selected = character(0)),
actionButton("refreshColours","Refresh Colors"),
plotOutput("plot_out")
)
server <- function(input,output){
#here is the function that returns a default grey color if no button is selected
color_with_default <- function( color_in ){
if(is.null(color_in)){
color_in <- "grey"
}
return(color_in)
}
col1 <- eventReactive(eventExpr = input$refreshColours, valueExpr = {input$point_color},ignoreNULL = FALSE)
pp <- reactive({
ggplot(dt, aes(x,y)) +
geom_point(size=input$point_size,
#the call to the color_with_default function is here
alpha=input$point_alpha, colour= color_with_default(col1()))
})
output$plot_out <- renderPlot({ pp() }) }
shinyApp(ui=ui, server=server)