2017-09-06 59 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