在闪亮的R中使用反应数据集并绘制图

时间:2019-01-21 21:49:07

标签: r shinydashboard shiny-server

当我从selectInput中选择项目时,我想使用闪亮的R绘制图。问题是从select输入中选择项目,我已经尝试过 plot(mydata $ input $ getCol1,mydata $ input $ getCol2],type =“ l”,col =“ blue”)但我得到另一个错误提示,需要有限的“ xlim”值。我还检查了数据集中没有NA的NA。

我的 UI.R代码如下

library(shiny)
library(shinydashboard)
library(DT)
library(ggplot2)
library(plotly)
ui <- dashboardPage(
dashboardHeader(title="Engagement and Passionate",titleWidth = 350),

# Sidebar ######################################
dashboardSidebar(
width = 150,
sidebarMenu(id = "mysidebar",
            menuItem("Engagement", 
                     menuSubItem("Bittorent", tabName = 
"subItemOne"),
                     menuSubItem("Twitter", tabName = "subItemTwo"),
                     menuSubItem("Tumblr", tabName = 
 "subItemThree"),
                     menuSubItem("Youtube", tabName = 
 "subItemFour"),
                    menuSubItem("Dailymotion", tabName = 
 "subItemFive")),
            menuItem("Charts",
                     menuSubItem("AP", tabName = "APC"),
                     menuSubItem("Anp", tabName = "ANPC"))
)),
  # Body #######################################


 dashboardBody(
 fluidRow(box(width = 3,h2("Upload file"),fileInput("file2", "Choose 
  CSV File",
                                                    accept = NULL)),
         box(width=8,h2("Raw Data"),DT::dataTableOutput("pltable")
         )
    ),
   tabItems(
   tabItem(tabName = "subItemOne",
         h2("Bittorent Insight"),
         box(title="select the variable",width=3 
 ,uiOutput("getCol1"),uiOutput("getCol2"),status = 
 "warning",solidheader=TRUE),
         plotOutput("graph"))
  ,

  tabItem(tabName = "subItemTwo",
          h2("Twitter Insight")
  ),

  tabItem(tabName = "subItemThree",
          h2("Tumblr Insight")
  ),

  tabItem(tabName = "subItemFour",
          h2("Youtube Insigth")
  ),
  tabItem(tabName = "subItemFive",
          h2("Daily motion Insigth")
  ))))

Server.R

  options(shiny.maxRequestSize=30*1024^2)
  server <- function(input, output) { 

  #Function to read table 
  rawdata <<- reactive({
  file1 <- input$file2
if(is.null(file1)){return()}
read.csv(file=file1$datapath,1,stringsAsFactors=FALSE)
 })
 output$getCol1<-renderUI({
 selectInput("Variable1","Choose 
 Option:",as.list(colnames(rawdata())))
  })
  output$getCol2<-renderUI({
selectInput("Variable2","Choose 
 Option:",as.list(colnames(rawdata())))
})
 #raw Datatable for platform

   output$pltable<-DT::renderDataTable({
  if(is.null(rawdata)){return()}
  DT::datatable(rawdata(),
 extensions="Responsive",options=list(pageLength=3),class='cell- 
  border strip',selection='single')
  })
  #access rawdata 
 output$graph <- renderPlot({
 mydata <- as.data.frame(rawdata())
 # p <- ggplot(mydata, 


 aes(mydata$input$getCol1,mydata$input$getCol2,
color=factor(mydata[3]))) + geom_point()
#ggplotly(p)
plot(x=mydata[input$getCol1],
y=mydata[input$getCol2],type="l",col="blue")
#plot(mydata[2])
 })

 }

我收到的错误如下: “ pairs”参数中只有一列 我还尝试了以下代码

output$graph <- renderPlot({
    mydata <- as.data.frame(rawdata())
    ggplot(mydata, aes_string(x = input$getCol1, y = input$getCol2))

     })

它在图中没有显示任何线,但是也没有显示任何错误。 您能否让我知道这是什么问题。

1 个答案:

答案 0 :(得分:0)

您需要调用选择输入的ID(即Variable1和Variable2),而不是调用getCol1和getCol2 试试这个:

output$graph <- renderPlot({
   df<- rawdata()
   plot(df[,input$Variable1],df[,input$Variable2])

 })

或者您可以尝试以下代码:

ggplot(df, aes_string(input$Variable1,input$Variable2))+geom_point(colour=‘blue’)