2017-09-06 109 views
0

我已經寫下述R代碼表達得到在使用字符串的繪圖標題的等式:ř光澤:通過UI

remove(list = ls()) 
library(shiny) 
library(ggplot2) 

ui <- pageWithSidebar(
    headerPanel('Plot a function'), 
    sidebarPanel(
    textInput("func", "Function to plot:", value = "x"), 
    numericInput("xMin", "Set the minimum of x: ", value = 0), 
    numericInput("xMax", "Set the maximum of x: ", value = 10), 
    submitButton("Plot") 
), 
    mainPanel(plotOutput("plot")) 
) 

server <- function(input, output) { 
    output$plot <- renderPlot({ 
    x <- seq(input$xMin, input$xMax, 0.01) 
    f <- function(x) { 
     eval(parse(text = input$func)) 
    } 
    y <- f(x) 
    df <- data.frame(x, y) 
    theme_set(theme_bw(base_size = 12)) 
    figure <- ggplot(df, aes(x, y)) + 
     geom_line() + 
     labs(x = "x", 
      y = "y", 
      title = parse(text = input$func)) + 
     theme(plot.margin = unit(c(.6, .6, .6, .6), "cm"), 
      plot.title = element_text(hjust = 0.5)) 
    plot(figure) 
    filename <- "plotFunction.jpeg" 
    ggsave(filename, 
      width = 6, 
      height = 4, 
      dpi = 200) 
    }) 
} 

shinyApp(ui = ui, server = server) 

當用戶輸入x^2 - 2指定函數,我得到以下輸出:

Output of the code

我想情節的標題開始y =(使整個標題將成爲y = x^2 - 2x^2正確渲染),但我一直UNA可以這樣做。例如,如果我將用戶輸入字符串連接到y =,則標題將變爲(= y, x^2 - 2)(儘管x^2已正確呈現)。

+1

只需要修改'在ggplot作爲title''解析(文=膏( 「Y =」,輸入$ FUNC)))' – parth

+0

@parth它不工作。隨着變化,標題變成=(y,x^2 - 2)(其中x^2正確呈現)。 – mozartbeethovenbrahms

+0

@parth'==':-) –

回答

0

你可以這樣做:

labs(title = parse(text=paste0("y == ", input$func))) 
+0

Laurent它的工作原理。謝謝!你能給我一些參考嗎? – mozartbeethovenbrahms