R 闪亮:将选定列从数据集打印到图形

R 闪亮:将选定列从数据集打印到图形,r,shiny,shiny-server,R,Shiny,Shiny Server,仅仅是发现闪亮的应用程序,但这让我发疯……我已经看了大量的server.R和ui.R代码示例,无法找出我做错了什么。如果是非常基本的事情,提前道歉 以iris数据集为例,我想用qplot或ggplot绘制一列与另一列的对比图 但是,使用qplot,我得到以下结果: 使用ggplot2,我得到了错误: 我不认为我需要反应式函数,因为我没有对数据集进行子集设置,只是提取要绘制的列 服务器.R代码 library(shiny) library(shinyapps) library(ggplot2)

仅仅是发现闪亮的应用程序,但这让我发疯……我已经看了大量的server.R和ui.R代码示例,无法找出我做错了什么。如果是非常基本的事情,提前道歉

以iris数据集为例,我想用qplot或ggplot绘制一列与另一列的对比图

但是,使用qplot,我得到以下结果:

使用ggplot2,我得到了错误:

我不认为我需要反应式函数,因为我没有对数据集进行子集设置,只是提取要绘制的列

服务器.R代码

library(shiny)
library(shinyapps)
library(ggplot2)

shinyServer(function(input, output, session) {

    output$text1 <- renderText({input$id1})

    output$text2 <- renderText({input$select1})

    output$plot1 <- renderPlot({
            g <- qplot(Sepal.Length, input$select1, data = iris)
            print(g)
    })
库(闪亮)
图书馆(shinyapps)
图书馆(GG2)
shinyServer(功能(输入、输出、会话){

输出$text1将您的
aes
语句更改为
aes\u string
,并将
x
设置为字符串。这应该可以解决问题

g <- ggplot(iris, aes_string(x = "Sepal.Length", y = input$select1)) 
g <- g + geom_line(col = "green", lwd =1) +
     labs(x = "Date", y = "Ranking") + 
     theme_bw() + scale_y_reverse()

g谢谢Charles,这解决了我的问题。我假设需要aes_字符串,因为输入$select1的返回值是一个字符串?我曾尝试使用as.name将输入$select1定义为非字符,但这对我不起作用。@laoisman,老实说,我不完全确定其内部是什么。这个答案来自经验和w文档向我展示了什么。如果您感到好奇,我建议联系Shiny软件包的维护人员。很抱歉,我无法回答您的后续问题。
library(shiny)
library(shinyapps)
data(iris)
opts <- unique(colnames(iris))
opts <- opts[-1] ## want to keep Sepal.Length as the x values

shinyUI(pageWithSidebar(
    headerPanel('test with iris database'),
    sidebarPanel(
            selectInput(inputId = "select1", label = "select", 
                        choices = opts),
            textInput(inputId = "id1", label = "Input Text", "")
    ),
    mainPanel(
            p('Output text1'),
            textOutput('text1'),
            textOutput('text2'),
            plotOutput('plot1')
    )
))
g <- ggplot(iris, aes_string(x = "Sepal.Length", y = input$select1)) 
g <- g + geom_line(col = "green", lwd =1) +
     labs(x = "Date", y = "Ranking") + 
     theme_bw() + scale_y_reverse()