R中的反应性的新手。尝试反应性读取csv,然后在给定的输入范围内生成3个图进行过滤。
我尝试将其添加为可响应的,再次为每个ggplot调用reactablefilereader data()。我对如何对这些代码行进行分层感到困惑,以便可以读取反应式文件,获取输入范围和过滤器,然后将其反馈给renderplot / ggplot。我可以得到1个积,但是尝试全部3个,我只能得到最后一个积。然后,如果我更改了内容,通常会遇到递归或反应错误,或者我设法获得全部3个图,但是在日期输入更改方面失去了反应性。
这是我具有的代码,可以对日期更改做出反应,但是只能绘制最后一个图。
csv文件如下所示:
temp_h humidity pressure pitch roll yaw mag_x mag_y mag_z accel_x accel_y accel_z gyro_x gyro_y gyro_z timestamp
------------- ------------- ------------- ------------- ------------- ------------- -------------- -------------- ------------- ------------- -------------- ------------- -------- -------- -------- ----------------
36.93448639 33.67306137 0 2.052537159 344.9172962 189.5288875 -24.15678406 -2.991427183 26.07371902 0.000484892 -0.262453765 0.948711813 ['x'] ['y'] ['z'] 5/9/2019 11:57
37.00978851 34.73247528 1002.021484 359.9863889 343.752597 190.284607 -66.8992157 -8.57483387 71.15454865 0 -0.281751841 0.966257989 ['x'] ['y'] ['z'] 5/9/2019 11:58
.app文件
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
library(tools)
library(lubridate)
library(ggplot2)
library(dplyr)
theme_set(theme_bw())
ui <- fluidPage(
titlePanel("Growth Chamber 109"),
column(4, wellPanel(
dateRangeInput(
'dateRange',
label = 'Filter results by date',
start = as.Date('2019-01-01') ,
end = NULL
)
)),
plotOutput("temp"),
tags$head(
tags$script(src = "https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/3.5.16/iframeResizer.contentWindow.min.js",
type = "text/javascript")
),
HTML('<div data-iframe-height></div>')
)
#The reactive file reader reads in the Raspberry Pi Python generated file SenseLog.csv and returns as data()
server <- function(input, output, session) {
data <- reactiveFileReader(
intervalMillis = 5000,
session = session,
filePath = "SenseLog.csv",
readFunc = read.csv)
#Server call for rendering the plot output
output$temp <- renderPlot({
plot(data())
#Change the function output data() to gc109. Reactive expressions/functions and the () mess me up sometimes
gc109 <- data()
#Parse time out in proper format
gc109$timestamp <-
strptime(gc109$timestamp, "%Y-%m-%d %H:%M")
#Filter data from logger based on date range input from session
try1 <- subset(gc109, timestamp >= input$dateRange[1])
try2 <- subset(try1, timestamp <= input$dateRange[2])
#Fix column header names
colnames(try2)[colnames(try1) == "timestamp"] <- "Date"
colnames(try2)[colnames(try1) == "temp_h"] <- "Temperature"
colnames(try2)[colnames(try1) == "humidity"] <- "Humidity"
colnames(try2)[colnames(try1) == "pressure"] <- "Pressure"
#Fix dates/maintain time to plot properly
try2$Date <- as.POSIXct(try2$Date)
#Generate temperature plot
ggplot(aes(x = Date, y = Temperature), data = try2) + geom_point() +
theme(text = element_text(size = 20))
ggplot(aes(x = Date, y = Humidity), data = try2) + geom_point() +
theme(text = element_text(size = 20))
ggplot(aes(x = Date, y = Pressure), data = try2) + geom_point() +
theme(text = element_text(size = 20))
})
}
shinyApp(ui, server)
答案 0 :(得分:0)
谢谢,但我确实希望找到一种更优雅的方法-我只是担心效率,希望我知道如何从1个数据处理事件中调用3个地块。这就是我解决的问题,似乎已经足够。
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
library(tools)
library(lubridate)
library(ggplot2)
library(dplyr)
theme_set(theme_bw())
ui <- fluidPage(
titlePanel("Growth Chamber 109"),
column(4, wellPanel(
dateRangeInput(
'dateRange',
label = 'Filter results by date',
start = as.Date('2019-05-10') ,
end = NULL
)
)),
plotOutput("Temperature"),
tags$head(
tags$script(src = "https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/3.5.16/iframeResizer.contentWindow.min.js",
type = "text/javascript")
),
HTML('<div data-iframe-height></div>'),
plotOutput("Humidity"),
tags$head(
tags$script(src = "https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/3.5.16/iframeResizer.contentWindow.min.js",
type = "text/javascript")
),
HTML('<div data-iframe-height></div>'),
plotOutput("Pressure"),
tags$head(
tags$script(src = "https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/3.5.16/iframeResizer.contentWindow.min.js",
type = "text/javascript")
),
HTML('<div data-iframe-height></div>')
)
#The reactive file reader reads in the Raspberry Pi Python generated file SenseLog.csv and returns as data()
server <- function(input, output, session) {
data <- reactiveFileReader(
intervalMillis = 5000,
session = session,
filePath = "SenseLog.csv",
readFunc = read.csv)
#Server call for rendering the temperature plot
output$Temperature <- renderPlot({
#Change the function output data() to gc109. Reactive expressions/functions and the () mess me up sometimes
gc109 <- data()
#Parse time out in proper format
gc109$timestamp <- strptime(gc109$timestamp, "%Y-%m-%d %H:%M")
#Filter data from logger based on date range input from session
try1 <- subset(gc109, timestamp >= input$dateRange[1])
try2 <- subset(try1, timestamp <= input$dateRange[2])
#Fix column header names
colnames(try2)[colnames(try1) == "timestamp"] <- "Date"
colnames(try2)[colnames(try1) == "temp_h"] <- "Temperature"
colnames(try2)[colnames(try1) == "humidity"] <- "Humidity"
colnames(try2)[colnames(try1) == "pressure"] <- "Pressure"
#Fix dates/maintain time to plot properly
try2$Date <- as.POSIXct(try2$Date)
#Generate temperature plot
ggplot(aes(x = Date, y = Temperature), data = try2) + geom_point() +
theme(text = element_text(size = 20))
})
data <- reactiveFileReader(
intervalMillis = 5000,
session = session,
filePath = "SenseLog.csv",
readFunc = read.csv)
#Server call for rendering the humidity plot
output$Humidity <- renderPlot({
#Change the function output data() to gc109. Reactive expressions/functions and the () mess me up sometimes
gc109 <- data()
#Parse time out in proper format
gc109$timestamp <- strptime(gc109$timestamp, "%Y-%m-%d %H:%M")
#Filter data from logger based on date range input from session
try1 <- subset(gc109, timestamp >= input$dateRange[1])
try2 <- subset(try1, timestamp <= input$dateRange[2])
#Fix column header names
colnames(try2)[colnames(try1) == "timestamp"] <- "Date"
colnames(try2)[colnames(try1) == "temp_h"] <- "Temperature"
colnames(try2)[colnames(try1) == "humidity"] <- "Humidity"
colnames(try2)[colnames(try1) == "pressure"] <- "Pressure"
#Fix dates/maintain time to plot properly
try2$Date <- as.POSIXct(try2$Date)
#Generate temperature plot
ggplot(aes(x = Date, y = Humidity), data = try2) + geom_point() +
theme(text = element_text(size = 20))
})
data <- reactiveFileReader(
intervalMillis = 5000,
session = session,
filePath = "SenseLog.csv",
readFunc = read.csv)
#Server call for rendering the pressure plot
output$Pressure <- renderPlot({
#Change the function output data() to gc109. Reactive expressions/functions and the () mess me up sometimes
gc109 <- data()
#Parse time out in proper format
gc109$timestamp <- strptime(gc109$timestamp, "%Y-%m-%d %H:%M")
#Filter data from logger based on date range input from session
try1 <- subset(gc109, timestamp >= input$dateRange[1])
try2 <- subset(try1, timestamp <= input$dateRange[2])
#Fix column header names
colnames(try2)[colnames(try1) == "timestamp"] <- "Date"
colnames(try2)[colnames(try1) == "temp_h"] <- "Temperature"
colnames(try2)[colnames(try1) == "humidity"] <- "Humidity"
colnames(try2)[colnames(try1) == "pressure"] <- "Pressure"
#Fix dates/maintain time to plot properly
try2$Date <- as.POSIXct(try2$Date)
#Generate temperature plot
ggplot(aes(x = Date, y = Pressure), data = try2) + geom_point() +
theme(text = element_text(size = 20))
})
}
shinyApp(ui, server)