2016-07-12 13 views
1

Der folgende Code funktioniert nicht. Was muss ich ändern?Wie setze ich scale_x_log10 (FALSE/TRUE) durch Checkbox in glänzend?

Spezifisch: Ich möchte die X-Achse auf logarithmische Skala ändern, wenn ein Kontrollkästchen aktiviert ist.

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") 
}) 

Antwort

3

Etwas wie folgt aus:

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

Edit: Arbeitsbeispiel.

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

    } 
) 
)