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
指定函數,我得到以下輸出:
我想情節的標題開始y =
(使整個標題將成爲y = x^2 - 2
與x^2
正確渲染),但我一直UNA可以這樣做。例如,如果我將用戶輸入字符串連接到y =
,則標題將變爲(= y, x^2 - 2)
(儘管x^2
已正確呈現)。
只需要修改'在ggplot作爲title''解析(文=膏( 「Y =」,輸入$ FUNC)))' – parth
@parth它不工作。隨着變化,標題變成=(y,x^2 - 2)(其中x^2正確呈現)。 – mozartbeethovenbrahms
@parth'==':-) –