如何通过闪亮的复选框设置scale_x_log10(FALSE / TRUE)?

时间:2016-07-12 11:12:56

标签: r shiny

以下代码不起作用。我需要改变什么?

具体: 当勾选复选框时,我希望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")
})

1 个答案:

答案 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)
      })

    }
  )
)