I'm trying to allow the user to select which column of data (selecty) they would like to make a plot of. Everything else in my app works except for this part. Here are the relevant portions of my code:
我正在尝试让用户选择他们想要绘制的数据列(selecty)。除了这个部分,我的应用程序中其他的东西都是有用的。以下是我的代码的相关部分:
#ui.R
library(shiny)
library(ggplot2)
shinyUI(fluidPage(h1("Title 1"),
# Sidebar
sidebarLayout(
sidebarPanel(
h3("Title 2"),
selectInput("pnum",
label = "Select the patient number",
choices = unique(pnums), selected = pnums[1]),
selectInput("selecty",
label = "Choose a variable to display on the y-axis",
choices = list('Var1', 'Var2', 'Var3', 'Var4')),
dateRangeInput("dates", label= "Date Range"),
submitButton("Create Graph")
),
# Show a plot
mainPanel(
plotOutput('makeplot')
)
)
))
#server.R
#relevant portion only - ggplot
library(shiny)
require(RODBC)
library(ggplot2)
library(quantmod)
library(reshape)
shinyServer(function(input, output) {
output$makeplot <- renderPlot({
p <- ggplot(pdata(), aes(x = Time_Stamp, y = input$yselect)) + geom_point()
print(p)
})
})
The data is brought from an online database and is a reactive function. My main issue is that in the user select for the column, it has to be in single quotes, although I would like to transfer it over to the ggplot function without the quotes so it acts as the column name. I've also tried to make a switch statement within the 'y = ' but I get the same error.
数据来自一个在线数据库,是一个反应性的函数。我的主要问题是,在用户选择的列中,它必须在单引号中,尽管我想把它转换到没有引号的ggplot函数,所以它充当列名。我还尝试在“y =”中做一个switch语句,但是我得到了相同的错误。
1 个解决方案
#1
1
First, in your ggplot call, you should have y = input$selecty
to be consistent with your naming in ui.R
.
首先,在您的ggplot调用中,您应该有y =输入$selectyto与您在ui.R中的命名一致。
Then, if I understood correctly, you should take a look at the aes_string
function in ggplot
because aes
uses non-standard evaluation which allows you to specify variable names directly and not as characters. Here, input$yselect
is passed as a character so you need to replace in server.R
:
然后,如果我理解正确,您应该看一下ggplot中的aes_string函数,因为aes使用非标准的评估,它允许您直接指定变量名,而不是作为字符。在这里,输入$yselect作为字符传递,因此您需要在服务器中替换。接待员:
p <- ggplot(pdata(), aes(x = Time_Stamp, y = input$yselect)) + geom_point()
with
与
p <- ggplot(pdata(), aes_string(x = "Time_Stamp", y = input$selecty)) + geom_point()
#1
1
First, in your ggplot call, you should have y = input$selecty
to be consistent with your naming in ui.R
.
首先,在您的ggplot调用中,您应该有y =输入$selectyto与您在ui.R中的命名一致。
Then, if I understood correctly, you should take a look at the aes_string
function in ggplot
because aes
uses non-standard evaluation which allows you to specify variable names directly and not as characters. Here, input$yselect
is passed as a character so you need to replace in server.R
:
然后,如果我理解正确,您应该看一下ggplot中的aes_string函数,因为aes使用非标准的评估,它允许您直接指定变量名,而不是作为字符。在这里,输入$yselect作为字符传递,因此您需要在服务器中替换。接待员:
p <- ggplot(pdata(), aes(x = Time_Stamp, y = input$yselect)) + geom_point()
with
与
p <- ggplot(pdata(), aes_string(x = "Time_Stamp", y = input$selecty)) + geom_point()