2017-09-06 12 views
0

によって得られた文字列を使用してプロットタイトルに式を発現Iは、以下のRコードを書かれている: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

私は(全体のタイトルが正しくレンダリングされたx^2y = x^2 - 2になりますように)y =を開始するプロットのタイトルが欲しい、私は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

関連する問題