以下代码不起作用。我需要改变什么?
具体: 当勾选复选框时,我希望X轴更改为logaritmic scale。
ui <- shinyUI(fluidPage(
...
checkboxInput("logarithmicX", "show x-axis in log10", FALSE),
checkboxInput("logarithmicY", "show y-axis in log10", FALSE),
...
))
server <- shinyServer(function(input, output) {
output$distPlot <- renderPlot({
...
xlog <- reactive({
switch(input$logarithmicX,
TRUE == TRUE,
FALSE == FALSE)
})
ylog <- reactive({
switch(input$logarithmicY,
TRUE == TRUE,
FALSE == FALSE)
})
ggplot(datasetInput(), aes(size)) + geom_histogram(bins = biins) + theme_bw() + scale_x_log10("logarithmicX") +scale_y_log10("logarithmicY")
})
答案 0 :(得分:4)
这样的事情:
output$distPlot <- renderPlot({
mygg <- ggplot(datasetInput(), aes(size)) +
geom_histogram(bins = biins) +
theme_bw()
if(input$logarithmicX)
mygg <- mygg + scale_x_log10()
if(input$logarithmicY)
mygg <- mygg + scale_y_log10()
return(mygg)
})
编辑:工作示例。
library(shiny)
library(ggplot2)
runApp(
shinyApp(
ui = {
pageWithSidebar(
headerPanel('http://stackoverflow.com/questions/38327254'),
sidebarPanel(
checkboxInput("logarithmicX", "show x-axis in log10", FALSE),
checkboxInput("logarithmicY", "show y-axis in log10", FALSE)
),
mainPanel(
plotOutput('distPlot')
)
)
},
server = function(input, output) {
output$distPlot <- renderPlot({
mygg <- ggplot(mtcars, aes(mpg)) +
geom_histogram()
if(input$logarithmicX)
mygg <- mygg + scale_x_log10()
if(input$logarithmicY)
mygg <- mygg + scale_y_log10()
return(mygg)
})
}
)
)