【问题标题】:use reactive dataset in shiny R and draw a plot在闪亮的 R 中使用反应数据集并绘制图
【发布时间】:2019-06-15 07:54:54
【问题描述】:

当我从 selectInput 中选择项目时,我想使用闪亮的 R 绘制一个图。问题在于从选择输入中选择项目,我已经尝试过 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")
  ))))

服务器.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))

     })

它没有在图表中显示任何线条,但它也没有显示任何错误。 能否请您告诉我这是什么问题。

【问题讨论】:

    标签: r shinydashboard shiny-server


    【解决方案1】:

    您需要调用选择输入的 id 变量 1 和变量 2 而不是调用 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’)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-09
      • 1970-01-01
      • 1970-01-01
      • 2019-05-20
      • 2023-04-01
      • 1970-01-01
      • 2015-06-22
      相关资源
      最近更新 更多