によって得られた文字列を使用してプロットタイトルに式を発現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
に入ると、私は、次の出力を得ます。
私は(全体のタイトルが正しくレンダリングされたx^2
とy = x^2 - 2
になりますように)y =
を開始するプロットのタイトルが欲しい、私はUNAされていますそうすることができます。たとえば、ユーザー入力文字列をy =
に連結すると、タイトルは(= y, x^2 - 2)
になります(ただし、x^2
は正しく表示されます)。
はちょうど '変更などggplotでtitle''解析(テキスト=ペースト( "Y ="、入力$ funcを))) ' – parth
それは動作しません@parth。この変更によって、タイトルは=(y、x^2 - 2)になります(x^2は適切にレンダリングされます)。 – mozartbeethovenbrahms
@parth '==' :-) –